You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.6 KiB

  1. 'use strict';
  2. import { TextDocument, TextEditor, Uri } from 'vscode';
  3. abstract class Comparer<T> {
  4. abstract equals(lhs: T, rhs: T): boolean;
  5. }
  6. class UriComparer extends Comparer<Uri> {
  7. equals(lhs: Uri, rhs: Uri) {
  8. if (!lhs && !rhs) return true;
  9. if ((lhs && !rhs) || (!lhs && rhs)) return false;
  10. return lhs.scheme === rhs.scheme && lhs.fsPath === rhs.fsPath;
  11. }
  12. }
  13. class TextDocumentComparer extends Comparer<TextDocument> {
  14. equals(lhs: TextDocument, rhs: TextDocument) {
  15. if (!lhs && !rhs) return true;
  16. if ((lhs && !rhs) || (!lhs && rhs)) return false;
  17. return uriComparer.equals(lhs.uri, rhs.uri);
  18. }
  19. }
  20. class TextEditorComparer extends Comparer<TextEditor> {
  21. equals(lhs: TextEditor, rhs: TextEditor, options: { useId: boolean, usePosition: boolean } = { useId: false, usePosition: false }) {
  22. if (!lhs && !rhs) return true;
  23. if ((lhs && !rhs) || (!lhs && rhs)) return false;
  24. if (options.usePosition && (lhs.viewColumn !== rhs.viewColumn)) return false;
  25. if (options.useId && (!lhs.document || !rhs.document)) {
  26. if ((lhs as any)._id !== (rhs as any)._id) return false;
  27. return true;
  28. }
  29. return textDocumentComparer.equals(lhs.document, rhs.document);
  30. }
  31. }
  32. const textDocumentComparer = new TextDocumentComparer();
  33. const textEditorComparer = new TextEditorComparer();
  34. const uriComparer = new UriComparer();
  35. export {
  36. textDocumentComparer as TextDocumentComparer,
  37. textEditorComparer as TextEditorComparer,
  38. uriComparer as UriComparer
  39. };