monaco-editor
Version:
A browser based code editor
150 lines (149 loc) • 6.18 kB
JavaScript
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
}
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
import { Position } from './position.js';
import { Range } from './range.js';
/**
* A selection in the editor.
* The selection is a range that has an orientation.
*/
var Selection = /** @class */ (function (_super) {
__extends(Selection, _super);
function Selection(selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn) {
var _this = _super.call(this, selectionStartLineNumber, selectionStartColumn, positionLineNumber, positionColumn) || this;
_this.selectionStartLineNumber = selectionStartLineNumber;
_this.selectionStartColumn = selectionStartColumn;
_this.positionLineNumber = positionLineNumber;
_this.positionColumn = positionColumn;
return _this;
}
/**
* Clone this selection.
*/
Selection.prototype.clone = function () {
return new Selection(this.selectionStartLineNumber, this.selectionStartColumn, this.positionLineNumber, this.positionColumn);
};
/**
* Transform to a human-readable representation.
*/
Selection.prototype.toString = function () {
return '[' + this.selectionStartLineNumber + ',' + this.selectionStartColumn + ' -> ' + this.positionLineNumber + ',' + this.positionColumn + ']';
};
/**
* Test if equals other selection.
*/
Selection.prototype.equalsSelection = function (other) {
return (Selection.selectionsEqual(this, other));
};
/**
* Test if the two selections are equal.
*/
Selection.selectionsEqual = function (a, b) {
return (a.selectionStartLineNumber === b.selectionStartLineNumber &&
a.selectionStartColumn === b.selectionStartColumn &&
a.positionLineNumber === b.positionLineNumber &&
a.positionColumn === b.positionColumn);
};
/**
* Get directions (LTR or RTL).
*/
Selection.prototype.getDirection = function () {
if (this.selectionStartLineNumber === this.startLineNumber && this.selectionStartColumn === this.startColumn) {
return 0 /* LTR */;
}
return 1 /* RTL */;
};
/**
* Create a new selection with a different `positionLineNumber` and `positionColumn`.
*/
Selection.prototype.setEndPosition = function (endLineNumber, endColumn) {
if (this.getDirection() === 0 /* LTR */) {
return new Selection(this.startLineNumber, this.startColumn, endLineNumber, endColumn);
}
return new Selection(endLineNumber, endColumn, this.startLineNumber, this.startColumn);
};
/**
* Get the position at `positionLineNumber` and `positionColumn`.
*/
Selection.prototype.getPosition = function () {
return new Position(this.positionLineNumber, this.positionColumn);
};
/**
* Create a new selection with a different `selectionStartLineNumber` and `selectionStartColumn`.
*/
Selection.prototype.setStartPosition = function (startLineNumber, startColumn) {
if (this.getDirection() === 0 /* LTR */) {
return new Selection(startLineNumber, startColumn, this.endLineNumber, this.endColumn);
}
return new Selection(this.endLineNumber, this.endColumn, startLineNumber, startColumn);
};
// ----
/**
* Create a `Selection` from one or two positions
*/
Selection.fromPositions = function (start, end) {
if (end === void 0) { end = start; }
return new Selection(start.lineNumber, start.column, end.lineNumber, end.column);
};
/**
* Create a `Selection` from an `ISelection`.
*/
Selection.liftSelection = function (sel) {
return new Selection(sel.selectionStartLineNumber, sel.selectionStartColumn, sel.positionLineNumber, sel.positionColumn);
};
/**
* `a` equals `b`.
*/
Selection.selectionsArrEqual = function (a, b) {
if (a && !b || !a && b) {
return false;
}
if (!a && !b) {
return true;
}
if (a.length !== b.length) {
return false;
}
for (var i = 0, len = a.length; i < len; i++) {
if (!this.selectionsEqual(a[i], b[i])) {
return false;
}
}
return true;
};
/**
* Test if `obj` is an `ISelection`.
*/
Selection.isISelection = function (obj) {
return (obj
&& (typeof obj.selectionStartLineNumber === 'number')
&& (typeof obj.selectionStartColumn === 'number')
&& (typeof obj.positionLineNumber === 'number')
&& (typeof obj.positionColumn === 'number'));
};
/**
* Create with a direction.
*/
Selection.createWithDirection = function (startLineNumber, startColumn, endLineNumber, endColumn, direction) {
if (direction === 0 /* LTR */) {
return new Selection(startLineNumber, startColumn, endLineNumber, endColumn);
}
return new Selection(endLineNumber, endColumn, startLineNumber, startColumn);
};
return Selection;
}(Range));
export { Selection };