| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- /*---------------------------------------------------------------------------------------------
- * Copyright (c) Microsoft Corporation. All rights reserved.
- * Licensed under the MIT License. See License.txt in the project root for license information.
- *--------------------------------------------------------------------------------------------*/
- /**
- * A position in the editor.
- */
- export class Position {
- constructor(lineNumber, column) {
- this.lineNumber = lineNumber;
- this.column = column;
- }
- /**
- * Create a new position from this position.
- *
- * @param newLineNumber new line number
- * @param newColumn new column
- */
- with(newLineNumber = this.lineNumber, newColumn = this.column) {
- if (newLineNumber === this.lineNumber && newColumn === this.column) {
- return this;
- }
- else {
- return new Position(newLineNumber, newColumn);
- }
- }
- /**
- * Derive a new position from this position.
- *
- * @param deltaLineNumber line number delta
- * @param deltaColumn column delta
- */
- delta(deltaLineNumber = 0, deltaColumn = 0) {
- return this.with(this.lineNumber + deltaLineNumber, this.column + deltaColumn);
- }
- /**
- * Test if this position equals other position
- */
- equals(other) {
- return Position.equals(this, other);
- }
- /**
- * Test if position `a` equals position `b`
- */
- static equals(a, b) {
- if (!a && !b) {
- return true;
- }
- return (!!a &&
- !!b &&
- a.lineNumber === b.lineNumber &&
- a.column === b.column);
- }
- /**
- * Test if this position is before other position.
- * If the two positions are equal, the result will be false.
- */
- isBefore(other) {
- return Position.isBefore(this, other);
- }
- /**
- * Test if position `a` is before position `b`.
- * If the two positions are equal, the result will be false.
- */
- static isBefore(a, b) {
- if (a.lineNumber < b.lineNumber) {
- return true;
- }
- if (b.lineNumber < a.lineNumber) {
- return false;
- }
- return a.column < b.column;
- }
- /**
- * Test if this position is before other position.
- * If the two positions are equal, the result will be true.
- */
- isBeforeOrEqual(other) {
- return Position.isBeforeOrEqual(this, other);
- }
- /**
- * Test if position `a` is before position `b`.
- * If the two positions are equal, the result will be true.
- */
- static isBeforeOrEqual(a, b) {
- if (a.lineNumber < b.lineNumber) {
- return true;
- }
- if (b.lineNumber < a.lineNumber) {
- return false;
- }
- return a.column <= b.column;
- }
- /**
- * A function that compares positions, useful for sorting
- */
- static compare(a, b) {
- const aLineNumber = a.lineNumber | 0;
- const bLineNumber = b.lineNumber | 0;
- if (aLineNumber === bLineNumber) {
- const aColumn = a.column | 0;
- const bColumn = b.column | 0;
- return aColumn - bColumn;
- }
- return aLineNumber - bLineNumber;
- }
- /**
- * Clone this position.
- */
- clone() {
- return new Position(this.lineNumber, this.column);
- }
- /**
- * Convert to a human-readable representation.
- */
- toString() {
- return '(' + this.lineNumber + ',' + this.column + ')';
- }
- // ---
- /**
- * Create a `Position` from an `IPosition`.
- */
- static lift(pos) {
- return new Position(pos.lineNumber, pos.column);
- }
- /**
- * Test if `obj` is an `IPosition`.
- */
- static isIPosition(obj) {
- return (obj
- && (typeof obj.lineNumber === 'number')
- && (typeof obj.column === 'number'));
- }
- }
|