text-manipulation
Version:
A NPM library that assists in text range manipulation
74 lines (73 loc) • 2.33 kB
JavaScript
"use strict";
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
Object.defineProperty(exports, "__esModule", { value: true });
var utils_1 = require("./utils");
var MutableTextRange = /** @class */ (function () {
/**
* @param {[TextPosition , TextPosition]} interval
* @param {TextBuffer} textBuffer
*/
function MutableTextRange(interval, textBuffer) {
this.textBuffer = textBuffer;
var _a = __read(interval, 2), start = _a[0], end = _a[1];
this.start = start;
this.end = end;
}
/**
* Change the text of the range
*
* @param {string} text
*/
MutableTextRange.prototype.setText = function (text) {
var range = this.textBuffer.replaceRange(this, text);
this.end = range.end;
this.start = range.start;
};
/**
* Get the text of the range
* @returns {string}
*/
MutableTextRange.prototype.getText = function () {
return this.textBuffer.getRangeText(this);
};
/**
* Sort the range ensuring that start is less than or equal to end
* start <= end
*
* @returns {MutableTextRange}
*/
MutableTextRange.prototype.sort = function () {
var _a = __read(utils_1.sortRange([this.start, this.end]), 2), start = _a[0], end = _a[1];
this.start = start;
this.end = end;
return this;
};
/**
* The range from start to end exists
*
* @returns {boolean}
*/
MutableTextRange.prototype.exists = function () {
return this.textBuffer.lineExists(this.start.line) &&
this.textBuffer.columnExists(this.start.column, this.start.line) &&
this.textBuffer.lineExists(this.end.line) &&
this.textBuffer.columnExists(this.end.column, this.end.line);
};
return MutableTextRange;
}());
exports.MutableTextRange = MutableTextRange;