f45883d75fdcf7af48008be0ba32c57330d9d579b1299bcb072ff45fd294cac6a54df5ab9bcf7a468069e5481a379353dff5ca1337a6edd3c941d7ead3ad97 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. /**
  6. * A position in the editor.
  7. */
  8. export class Position {
  9. constructor(lineNumber, column) {
  10. this.lineNumber = lineNumber;
  11. this.column = column;
  12. }
  13. /**
  14. * Create a new position from this position.
  15. *
  16. * @param newLineNumber new line number
  17. * @param newColumn new column
  18. */
  19. with(newLineNumber = this.lineNumber, newColumn = this.column) {
  20. if (newLineNumber === this.lineNumber && newColumn === this.column) {
  21. return this;
  22. }
  23. else {
  24. return new Position(newLineNumber, newColumn);
  25. }
  26. }
  27. /**
  28. * Derive a new position from this position.
  29. *
  30. * @param deltaLineNumber line number delta
  31. * @param deltaColumn column delta
  32. */
  33. delta(deltaLineNumber = 0, deltaColumn = 0) {
  34. return this.with(this.lineNumber + deltaLineNumber, this.column + deltaColumn);
  35. }
  36. /**
  37. * Test if this position equals other position
  38. */
  39. equals(other) {
  40. return Position.equals(this, other);
  41. }
  42. /**
  43. * Test if position `a` equals position `b`
  44. */
  45. static equals(a, b) {
  46. if (!a && !b) {
  47. return true;
  48. }
  49. return (!!a &&
  50. !!b &&
  51. a.lineNumber === b.lineNumber &&
  52. a.column === b.column);
  53. }
  54. /**
  55. * Test if this position is before other position.
  56. * If the two positions are equal, the result will be false.
  57. */
  58. isBefore(other) {
  59. return Position.isBefore(this, other);
  60. }
  61. /**
  62. * Test if position `a` is before position `b`.
  63. * If the two positions are equal, the result will be false.
  64. */
  65. static isBefore(a, b) {
  66. if (a.lineNumber < b.lineNumber) {
  67. return true;
  68. }
  69. if (b.lineNumber < a.lineNumber) {
  70. return false;
  71. }
  72. return a.column < b.column;
  73. }
  74. /**
  75. * Test if this position is before other position.
  76. * If the two positions are equal, the result will be true.
  77. */
  78. isBeforeOrEqual(other) {
  79. return Position.isBeforeOrEqual(this, other);
  80. }
  81. /**
  82. * Test if position `a` is before position `b`.
  83. * If the two positions are equal, the result will be true.
  84. */
  85. static isBeforeOrEqual(a, b) {
  86. if (a.lineNumber < b.lineNumber) {
  87. return true;
  88. }
  89. if (b.lineNumber < a.lineNumber) {
  90. return false;
  91. }
  92. return a.column <= b.column;
  93. }
  94. /**
  95. * A function that compares positions, useful for sorting
  96. */
  97. static compare(a, b) {
  98. const aLineNumber = a.lineNumber | 0;
  99. const bLineNumber = b.lineNumber | 0;
  100. if (aLineNumber === bLineNumber) {
  101. const aColumn = a.column | 0;
  102. const bColumn = b.column | 0;
  103. return aColumn - bColumn;
  104. }
  105. return aLineNumber - bLineNumber;
  106. }
  107. /**
  108. * Clone this position.
  109. */
  110. clone() {
  111. return new Position(this.lineNumber, this.column);
  112. }
  113. /**
  114. * Convert to a human-readable representation.
  115. */
  116. toString() {
  117. return '(' + this.lineNumber + ',' + this.column + ')';
  118. }
  119. // ---
  120. /**
  121. * Create a `Position` from an `IPosition`.
  122. */
  123. static lift(pos) {
  124. return new Position(pos.lineNumber, pos.column);
  125. }
  126. /**
  127. * Test if `obj` is an `IPosition`.
  128. */
  129. static isIPosition(obj) {
  130. return (obj
  131. && (typeof obj.lineNumber === 'number')
  132. && (typeof obj.column === 'number'));
  133. }
  134. }