monaco-editor
Version:
A browser based code editor
141 lines (140 loc) • 4.64 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* 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.
*/
var Position = /** @class */ (function () {
function Position(lineNumber, column) {
this.lineNumber = lineNumber;
this.column = column;
}
/**
* Create a new postion from this position.
*
* @param newLineNumber new line number
* @param newColumn new column
*/
Position.prototype.with = function (newLineNumber, newColumn) {
if (newLineNumber === void 0) { newLineNumber = this.lineNumber; }
if (newColumn === void 0) { 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
*/
Position.prototype.delta = function (deltaLineNumber, deltaColumn) {
if (deltaLineNumber === void 0) { deltaLineNumber = 0; }
if (deltaColumn === void 0) { deltaColumn = 0; }
return this.with(this.lineNumber + deltaLineNumber, this.column + deltaColumn);
};
/**
* Test if this position equals other position
*/
Position.prototype.equals = function (other) {
return Position.equals(this, other);
};
/**
* Test if position `a` equals position `b`
*/
Position.equals = function (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.
*/
Position.prototype.isBefore = function (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.
*/
Position.isBefore = function (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.
*/
Position.prototype.isBeforeOrEqual = function (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.
*/
Position.isBeforeOrEqual = function (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
*/
Position.compare = function (a, b) {
var aLineNumber = a.lineNumber | 0;
var bLineNumber = b.lineNumber | 0;
if (aLineNumber === bLineNumber) {
var aColumn = a.column | 0;
var bColumn = b.column | 0;
return aColumn - bColumn;
}
return aLineNumber - bLineNumber;
};
/**
* Clone this position.
*/
Position.prototype.clone = function () {
return new Position(this.lineNumber, this.column);
};
/**
* Convert to a human-readable representation.
*/
Position.prototype.toString = function () {
return '(' + this.lineNumber + ',' + this.column + ')';
};
// ---
/**
* Create a `Position` from an `IPosition`.
*/
Position.lift = function (pos) {
return new Position(pos.lineNumber, pos.column);
};
/**
* Test if `obj` is an `IPosition`.
*/
Position.isIPosition = function (obj) {
return (obj
&& (typeof obj.lineNumber === 'number')
&& (typeof obj.column === 'number'));
};
return Position;
}());
export { Position };