UNPKG

4.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.TextRange = void 0;
4/**
5 * Efficiently references a range of text from a string buffer.
6 */
7var TextRange = /** @class */ (function () {
8 function TextRange(buffer, pos, end) {
9 this.buffer = buffer;
10 this.pos = pos;
11 this.end = end;
12 this._validateBounds();
13 }
14 /**
15 * Constructs a TextRange that corresponds to an entire string object.
16 */
17 TextRange.fromString = function (buffer) {
18 return new TextRange(buffer, 0, buffer.length);
19 };
20 /**
21 * Constructs a TextRange that corresponds to an entire string object.
22 */
23 TextRange.fromStringRange = function (buffer, pos, end) {
24 return new TextRange(buffer, pos, end);
25 };
26 Object.defineProperty(TextRange.prototype, "length", {
27 /**
28 * Returns the length of the text range.
29 * @remarks
30 * This value is calculated as the `end` property minus the `pos` property.
31 */
32 get: function () {
33 return this.end - this.pos;
34 },
35 enumerable: false,
36 configurable: true
37 });
38 /**
39 * Constructs a TextRange that corresponds to a different range of an existing buffer.
40 */
41 TextRange.prototype.getNewRange = function (pos, end) {
42 return new TextRange(this.buffer, pos, end);
43 };
44 /**
45 * Returns true if the length of the range is zero. Note that the object reference may not
46 * be equal to `TextRange.empty`, and the buffer may be different.
47 */
48 TextRange.prototype.isEmpty = function () {
49 return this.pos === this.end;
50 };
51 /**
52 * Returns the range from the associated string buffer.
53 */
54 TextRange.prototype.toString = function () {
55 return this.buffer.substring(this.pos, this.end);
56 };
57 /**
58 * Returns a debugging dump of the range, indicated via custom delimiters.
59 * @remarks
60 * For example if the delimiters are "[" and "]", and the range is 3..5 inside "1234567",
61 * then the output would be "12[345]67".
62 */
63 TextRange.prototype.getDebugDump = function (posDelimiter, endDelimiter) {
64 return (this.buffer.substring(0, this.pos) +
65 posDelimiter +
66 this.buffer.substring(this.pos, this.end) +
67 endDelimiter +
68 this.buffer.substring(this.end));
69 };
70 /**
71 * Calculates the line and column number for the specified offset into the buffer.
72 *
73 * @remarks
74 * This is a potentially expensive operation.
75 *
76 * @param index - an integer offset
77 * @param buffer - the buffer
78 */
79 TextRange.prototype.getLocation = function (index) {
80 if (index < 0 || index > this.buffer.length) {
81 // No match
82 return { line: 0, column: 0 };
83 }
84 // TODO: Consider caching or optimizing this somehow
85 var line = 1;
86 var column = 1;
87 var currentIndex = 0;
88 while (currentIndex < index) {
89 var current = this.buffer[currentIndex];
90 ++currentIndex;
91 if (current === '\r') {
92 // CR
93 // Ignore '\r' and assume it will always have an accompanying '\n'
94 continue;
95 }
96 if (current === '\n') {
97 // LF
98 ++line;
99 column = 1;
100 }
101 else {
102 // NOTE: For consistency with the TypeScript compiler, a tab character is assumed
103 // to advance by one column
104 ++column;
105 }
106 }
107 return { line: line, column: column };
108 };
109 TextRange.prototype._validateBounds = function () {
110 if (this.pos < 0) {
111 throw new Error('TextRange.pos cannot be negative');
112 }
113 if (this.end < 0) {
114 throw new Error('TextRange.end cannot be negative');
115 }
116 if (this.end < this.pos) {
117 throw new Error('TextRange.end cannot be smaller than TextRange.pos');
118 }
119 if (this.pos > this.buffer.length) {
120 throw new Error('TextRange.pos cannot exceed the associated text buffer length');
121 }
122 if (this.end > this.buffer.length) {
123 throw new Error('TextRange.end cannot exceed the associated text buffer length');
124 }
125 };
126 /**
127 * Used to represent an empty or unknown range.
128 */
129 TextRange.empty = new TextRange('', 0, 0);
130 return TextRange;
131}());
132exports.TextRange = TextRange;
133//# sourceMappingURL=TextRange.js.map
\No newline at end of file