@unisnips/ultisnips
Version:
Utilities for converting ultisnips in unisnips project
92 lines • 3.19 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Represents a Position in a text file: (0 based line index, 0 based column
* index) and provides methods for moving them around.
*/
var TextPosition = /** @class */ (function () {
function TextPosition(line, col, offset) {
this.line = line;
this.column = col;
this.offset = offset;
}
TextPosition.fromUnistPoint = function (point) {
return new TextPosition(point.line, point.column, point.offset);
};
/**
* A function that compares positions, useful for sorting
*/
TextPosition.compare = function (a, b) {
var aLineNumber = a.line | 0;
var bLineNumber = b.line | 0;
if (aLineNumber === bLineNumber) {
var aColumn = a.column | 0;
var bColumn = b.column | 0;
return aColumn - bColumn;
}
return aLineNumber - bLineNumber;
};
TextPosition.prototype.toString = function () {
return "(" + this.line + "," + this.column + "," + this.offset + ")";
};
TextPosition.prototype.clone = function () {
return new TextPosition(this.line, this.column, this.offset);
};
TextPosition.prototype.toUnistPosition = function () {
return {
line: this.line,
column: this.column,
offset: this.offset,
};
};
/**
* Returns the difference that the cursor must move to come from 'pos' to us
*/
TextPosition.prototype.delta = function (pos) {
if (this.line === pos.line) {
var colDiff = this.column - pos.column;
return new TextPosition(0, colDiff, colDiff);
}
return new TextPosition(this.line - pos.line, this.column);
};
/**
* Create a new position that, moved 'delta' from us.
* Slightly different than `add` method
*/
TextPosition.prototype.moveWith = function (delta) {
var newLine = this.line + delta.line;
var newCol = this.column;
if (delta.line === 0) {
newCol = this.column + delta.column;
}
else {
newCol = delta.column;
}
return new TextPosition(newLine, newCol);
};
TextPosition.prototype.add = function (pos) {
return new TextPosition(this.line + pos.line, this.column + pos.column);
};
TextPosition.prototype.substract = function (pos) {
return new TextPosition(this.line - pos.line, this.column - pos.column);
};
/**
* Create a new position from this position.
*
* @param newLineNumber new line number
* @param newColumn new column
*/
TextPosition.prototype.with = function (newLineNumber, newColumn) {
if (newLineNumber === void 0) { newLineNumber = this.line; }
if (newColumn === void 0) { newColumn = this.column; }
if (newLineNumber === this.line && newColumn === this.column) {
return this;
}
else {
return new TextPosition(newLineNumber, newColumn);
}
};
return TextPosition;
}());
exports.TextPosition = TextPosition;
//# sourceMappingURL=position.js.map