1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.TextRange = void 0;
|
4 |
|
5 |
|
6 |
|
7 | var TextRange = (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 |
|
16 |
|
17 | TextRange.fromString = function (buffer) {
|
18 | return new TextRange(buffer, 0, buffer.length);
|
19 | };
|
20 | |
21 |
|
22 |
|
23 | TextRange.fromStringRange = function (buffer, pos, end) {
|
24 | return new TextRange(buffer, pos, end);
|
25 | };
|
26 | Object.defineProperty(TextRange.prototype, "length", {
|
27 | |
28 |
|
29 |
|
30 |
|
31 |
|
32 | get: function () {
|
33 | return this.end - this.pos;
|
34 | },
|
35 | enumerable: false,
|
36 | configurable: true
|
37 | });
|
38 | |
39 |
|
40 |
|
41 | TextRange.prototype.getNewRange = function (pos, end) {
|
42 | return new TextRange(this.buffer, pos, end);
|
43 | };
|
44 | |
45 |
|
46 |
|
47 |
|
48 | TextRange.prototype.isEmpty = function () {
|
49 | return this.pos === this.end;
|
50 | };
|
51 | |
52 |
|
53 |
|
54 | TextRange.prototype.toString = function () {
|
55 | return this.buffer.substring(this.pos, this.end);
|
56 | };
|
57 | |
58 |
|
59 |
|
60 |
|
61 |
|
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 |
|
72 |
|
73 |
|
74 |
|
75 |
|
76 |
|
77 |
|
78 |
|
79 | TextRange.prototype.getLocation = function (index) {
|
80 | if (index < 0 || index > this.buffer.length) {
|
81 |
|
82 | return { line: 0, column: 0 };
|
83 | }
|
84 |
|
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 |
|
93 |
|
94 | continue;
|
95 | }
|
96 | if (current === '\n') {
|
97 |
|
98 | ++line;
|
99 | column = 1;
|
100 | }
|
101 | else {
|
102 |
|
103 |
|
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 |
|
128 |
|
129 | TextRange.empty = new TextRange('', 0, 0);
|
130 | return TextRange;
|
131 | }());
|
132 | exports.TextRange = TextRange;
|
133 |
|
\ | No newline at end of file |