79f5cb71537f5c070724504ac09ec43cbf29bb965f1b7755d8675575ddd373948dbd44129853f53c415321ae0e1f4e62854cb7da538d6856b3c593dabccc17 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { Position } from './position.js';
  6. import { Range } from './range.js';
  7. /**
  8. * A selection in the editor.
  9. * The selection is a range that has an orientation.
  10. */
  11. export class Selection extends Range {
  12. constructor(selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn) {
  13. super(selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn);
  14. this.selectionStartLineNumber = selectionStartLineNumber;
  15. this.selectionStartColumn = selectionStartColumn;
  16. this.positionLineNumber = positionLineNumber;
  17. this.positionColumn = positionColumn;
  18. }
  19. /**
  20. * Transform to a human-readable representation.
  21. */
  22. toString() {
  23. return '[' + this.selectionStartLineNumber + ',' + this.selectionStartColumn + ' -> ' + this.positionLineNumber + ',' + this.positionColumn + ']';
  24. }
  25. /**
  26. * Test if equals other selection.
  27. */
  28. equalsSelection(other) {
  29. return (Selection.selectionsEqual(this, other));
  30. }
  31. /**
  32. * Test if the two selections are equal.
  33. */
  34. static selectionsEqual(a, b) {
  35. return (a.selectionStartLineNumber === b.selectionStartLineNumber &&
  36. a.selectionStartColumn === b.selectionStartColumn &&
  37. a.positionLineNumber === b.positionLineNumber &&
  38. a.positionColumn === b.positionColumn);
  39. }
  40. /**
  41. * Get directions (LTR or RTL).
  42. */
  43. getDirection() {
  44. if (this.selectionStartLineNumber === this.startLineNumber && this.selectionStartColumn === this.startColumn) {
  45. return 0 /* SelectionDirection.LTR */;
  46. }
  47. return 1 /* SelectionDirection.RTL */;
  48. }
  49. /**
  50. * Create a new selection with a different `positionLineNumber` and `positionColumn`.
  51. */
  52. setEndPosition(endLineNumber, endColumn) {
  53. if (this.getDirection() === 0 /* SelectionDirection.LTR */) {
  54. return new Selection(this.startLineNumber, this.startColumn, endLineNumber, endColumn);
  55. }
  56. return new Selection(endLineNumber, endColumn, this.startLineNumber, this.startColumn);
  57. }
  58. /**
  59. * Get the position at `positionLineNumber` and `positionColumn`.
  60. */
  61. getPosition() {
  62. return new Position(this.positionLineNumber, this.positionColumn);
  63. }
  64. /**
  65. * Get the position at the start of the selection.
  66. */
  67. getSelectionStart() {
  68. return new Position(this.selectionStartLineNumber, this.selectionStartColumn);
  69. }
  70. /**
  71. * Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`.
  72. */
  73. setStartPosition(startLineNumber, startColumn) {
  74. if (this.getDirection() === 0 /* SelectionDirection.LTR */) {
  75. return new Selection(startLineNumber, startColumn, this.endLineNumber, this.endColumn);
  76. }
  77. return new Selection(this.endLineNumber, this.endColumn, startLineNumber, startColumn);
  78. }
  79. // ----
  80. /**
  81. * Create a `Selection` from one or two positions
  82. */
  83. static fromPositions(start, end = start) {
  84. return new Selection(start.lineNumber, start.column, end.lineNumber, end.column);
  85. }
  86. /**
  87. * Creates a `Selection` from a range, given a direction.
  88. */
  89. static fromRange(range, direction) {
  90. if (direction === 0 /* SelectionDirection.LTR */) {
  91. return new Selection(range.startLineNumber, range.startColumn, range.endLineNumber, range.endColumn);
  92. }
  93. else {
  94. return new Selection(range.endLineNumber, range.endColumn, range.startLineNumber, range.startColumn);
  95. }
  96. }
  97. /**
  98. * Create a `Selection` from an `ISelection`.
  99. */
  100. static liftSelection(sel) {
  101. return new Selection(sel.selectionStartLineNumber, sel.selectionStartColumn, sel.positionLineNumber, sel.positionColumn);
  102. }
  103. /**
  104. * `a` equals `b`.
  105. */
  106. static selectionsArrEqual(a, b) {
  107. if (a && !b || !a && b) {
  108. return false;
  109. }
  110. if (!a && !b) {
  111. return true;
  112. }
  113. if (a.length !== b.length) {
  114. return false;
  115. }
  116. for (let i = 0, len = a.length; i < len; i++) {
  117. if (!this.selectionsEqual(a[i], b[i])) {
  118. return false;
  119. }
  120. }
  121. return true;
  122. }
  123. /**
  124. * Test if `obj` is an `ISelection`.
  125. */
  126. static isISelection(obj) {
  127. return (obj
  128. && (typeof obj.selectionStartLineNumber === 'number')
  129. && (typeof obj.selectionStartColumn === 'number')
  130. && (typeof obj.positionLineNumber === 'number')
  131. && (typeof obj.positionColumn === 'number'));
  132. }
  133. /**
  134. * Create with a direction.
  135. */
  136. static createWithDirection(startLineNumber, startColumn, endLineNumber, endColumn, direction) {
  137. if (direction === 0 /* SelectionDirection.LTR */) {
  138. return new Selection(startLineNumber, startColumn, endLineNumber, endColumn);
  139. }
  140. return new Selection(endLineNumber, endColumn, startLineNumber, startColumn);
  141. }
  142. }