UNPKG

text-manipulation

Version:

A NPM library that assists in text range manipulation

261 lines (260 loc) 10.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var utils_1 = require("./utils"); var immutable_text_range_1 = require("./immutable-text-range"); var BasicTextBuffer = /** @class */ (function () { function BasicTextBuffer(originalText, lineDelimiter) { if (lineDelimiter === void 0) { lineDelimiter = '\n'; } this.originalText = originalText; this.lineDelimiter = lineDelimiter; this.init(); } BasicTextBuffer.prototype.getLineCount = function () { return this.table.length; }; BasicTextBuffer.prototype.getText = function () { var n = this.table.length; var lineBuffer = []; for (var line = 0; line < n; line++) { lineBuffer.push(this.table[line].join('')); } return lineBuffer.join(this.lineDelimiter); }; BasicTextBuffer.prototype.replaceRange = function (range, text) { range = new immutable_text_range_1.ImmutableTextRange(utils_1.sortRange([range.start, range.end])); var table = this.buildTable(text, this.lineDelimiter); var endLine = range.end.line; if (this.table[range.start.line] !== undefined && this.table[endLine] !== undefined) { var removePosition = this.removeRange(range); var lastChangedLine = removePosition.line; if (!this.lineExists(lastChangedLine)) { while (!this.lineExists(lastChangedLine) && lastChangedLine > 0) { lastChangedLine--; } if (this.lineExists(lastChangedLine)) { this.table.push([]); lastChangedLine++; } else { // If text is empty this.table[lastChangedLine] = []; } } var lastChangedCol = removePosition.column; var startPos = { column: lastChangedCol, line: lastChangedLine }; var endPos = { column: 0, line: lastChangedLine }; var n = table.length; for (var i = 0; i < n; i++) { if (n === 1) { var chars = text.split(''); for (var j = 0; j < table[i].length; j++) { endPos.column = lastChangedCol + j + 1; var ch = chars[j]; this.table[lastChangedLine + i].splice(j + lastChangedCol, 0, ch); } } else if (i === (n - 1)) { endPos.line = lastChangedLine + i; for (var j = 0; j < table[i].length; j++) { endPos.column = lastChangedCol + j + 1; var ch = table[i][j]; this.table[lastChangedLine + i].splice(j, 0, ch); } } else { if (i === 0) { this.insertText(lastChangedCol, lastChangedLine, table[i].join('') + this.lineDelimiter); } else { this.table.splice(lastChangedLine + i, 0, table[i]); } } } var newRange = new immutable_text_range_1.ImmutableTextRange(utils_1.sortRange([startPos, endPos])); return newRange; } else { return undefined; } }; BasicTextBuffer.prototype.removeRange = function (range) { range = new immutable_text_range_1.ImmutableTextRange(utils_1.sortRange([range.start, range.end])); var start = range.start; var end = range.end; var line = start.line; var startCol = start.column; var prefixChars = []; for (var i = start.line; i <= end.line; i++) { var colEnd = (i === end.line) ? end.column : this.getColumnCount(line); for (var col = startCol; col < colEnd; col++) { this.removeColumn(startCol, line); } if (!this.isLineEmpty(line) || i === end.line) { if (i !== end.line) { prefixChars = this.table[line]; this.removeLine(line); startCol = 0; } else { this.table[line] = prefixChars.concat(this.table[line]); } } else { this.removeLine(line); } } return { line: line, column: (prefixChars.length > 0) ? prefixChars.length : startCol }; }; BasicTextBuffer.prototype.getRangeText = function (range) { var linesBuffer = []; range = new immutable_text_range_1.ImmutableTextRange(utils_1.sortRange([range.start, range.end])); var start = range.start; var end = range.end; for (var line = start.line; line <= end.line; line++) { var colEnd = (line === end.line) ? end.column : this.getColumnCount(line); var chars = []; var colStart = (line === start.line) ? start.column : 0; for (var col = colStart; col < colEnd; col++) { var ch = this.charAt(col, line); if (ch !== undefined) { chars.push(ch); } else { break; } } linesBuffer.push(chars.join('')); } return linesBuffer.join('\n'); }; BasicTextBuffer.prototype.removeLineRange = function (lineStart, lineEnd) { for (var line = lineStart; line <= lineEnd; line++) { this.removeLine(lineStart); } }; BasicTextBuffer.prototype.removeFirstLine = function () { this.removeLine(0); }; BasicTextBuffer.prototype.removeLastLine = function () { this.removeLine(this.getLineCount() - 1); }; BasicTextBuffer.prototype.removeLine = function (line) { this.table.splice(line, 1); }; BasicTextBuffer.prototype.isLineEmpty = function (line) { return this.table[line] && this.table[line].length === 0; }; BasicTextBuffer.prototype.replaceTextInLine = function (line, lineText) { var table = this.buildTable(lineText, this.lineDelimiter); this.table[line] = table[0]; }; BasicTextBuffer.prototype.insertTextAtLine = function (line, text) { var table = this.buildTable(text, this.lineDelimiter); for (var i = 0; i < table.length; i++) { var chars = table[i]; this.table.splice(line + i, 0, chars); } }; BasicTextBuffer.prototype.lineExists = function (line) { return this.table[line] !== undefined; }; BasicTextBuffer.prototype.getLine = function (line) { return this.table[line].join(''); }; BasicTextBuffer.prototype.getLineRange = function (lineStart, lineEnd) { var sb = []; for (var line = lineStart; line < lineEnd; line++) { sb.push(this.getLine(line)); } return sb.join('\n'); }; BasicTextBuffer.prototype.columnExists = function (column, line) { return this.table[column][line] !== undefined; }; BasicTextBuffer.prototype.getColumnCount = function (line) { if (this.table[line] !== undefined) { return this.table[line].length; } return -1; }; BasicTextBuffer.prototype.charAt = function (column, line) { return this.table[line][column]; }; BasicTextBuffer.prototype.insertText = function (column, line, text) { var table = this.buildTable(text, this.lineDelimiter); if (table.length > 1) { var n = table.length; for (var i = 0; i < n; i++) { if (i === (n - 1)) { for (var j = 0; j < table[i].length; j++) { var ch = table[i][j]; this.table[line].splice(j, 0, ch); } } else { if (i === 0) { var chars = []; // // Build up a char buffer for columns // that may exist in text before the starting // column. // for (var j = 0; j < column; j++) { chars.push(this.table[line][0]); this.table[line].splice(0, 1); } this.table.splice(line++, 0, chars.concat(table[i])); } else { this.table.splice(line, 0, table[i]); line++; } } } } else if (this.table[line]) { var chars = text.split(''); for (var i = 0; i < chars.length; i++) { var ch = chars[i]; this.table[line].splice(column + i, 0, ch); } } }; BasicTextBuffer.prototype.removeColumn = function (column, line) { if (this.table[line]) { this.table[line].splice(column, 1); } }; BasicTextBuffer.prototype.getColumnRange = function (columnStart, columnEnd, line) { var sb = []; for (var column = columnStart; column < columnEnd; column++) { var ch = this.charAt(column, line); if (ch !== undefined) { sb.push(ch); } else { break; } } return sb.join(''); }; BasicTextBuffer.prototype.removeColumnRange = function (columnStart, columnEnd, line) { for (var column = columnStart; column < columnEnd; column++) { this.removeColumn(columnStart, line); } }; BasicTextBuffer.prototype.init = function () { this.table = this.buildTable(this.originalText, this.lineDelimiter); }; BasicTextBuffer.prototype.buildTable = function (text, lineDelimiter) { var lines = text.split(lineDelimiter); var n = lines.length; var table = []; for (var line = 0; line < n; line++) { table[line] = lines[line].split(''); } return table; }; return BasicTextBuffer; }()); exports.BasicTextBuffer = BasicTextBuffer;