forked from microsoft/TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathversionCache.ts
More file actions
316 lines (262 loc) · 12.3 KB
/
versionCache.ts
File metadata and controls
316 lines (262 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
namespace ts {
function editFlat(position: number, deletedLength: number, newText: string, source: string) {
return source.substring(0, position) + newText + source.substring(position + deletedLength, source.length);
}
function lineColToPosition(lineIndex: server.LineIndex, line: number, col: number) {
return lineIndex.absolutePositionOfStartOfLine(line) + (col - 1);
}
function validateEdit(lineIndex: server.LineIndex, sourceText: string, position: number, deleteLength: number, insertString: string): void {
const checkText = editFlat(position, deleteLength, insertString, sourceText);
const snapshot = lineIndex.edit(position, deleteLength, insertString);
const editedText = snapshot.getText(0, snapshot.getLength());
assert.equal(editedText, checkText);
}
describe(`VersionCache TS code`, () => {
let validateEditAtLineCharIndex: (line: number, char: number, deleteLength: number, insertString: string) => void;
before(() => {
const testContent = `/// <reference path="z.ts" />
var x = 10;
var y = { zebra: 12, giraffe: "ell" };
z.a;
class Point {
x: number;
}
k=y;
var p:Point=new Point();
var q:Point=<Point>p;`;
const { lines } = server.LineIndex.linesFromText(testContent);
assert.isTrue(lines.length > 0, "Failed to initialize test text. Expected text to have at least one line");
const lineIndex = new server.LineIndex();
lineIndex.load(lines);
validateEditAtLineCharIndex = (line: number, char: number, deleteLength: number, insertString: string) => {
const position = lineColToPosition(lineIndex, line, char);
validateEdit(lineIndex, testContent, position, deleteLength, insertString);
};
});
after(() => {
validateEditAtLineCharIndex = undefined!;
});
it("handles empty lines array", () => {
const lineIndex = new server.LineIndex();
lineIndex.load([]);
assert.deepEqual(lineIndex.positionToLineOffset(0), { line: 1, offset: 1 });
});
it(`change 9 1 0 1 {"y"}`, () => {
validateEditAtLineCharIndex(9, 1, 0, "y");
});
it(`change 9 2 0 1 {"."}`, () => {
validateEditAtLineCharIndex(9, 2, 0, ".");
});
it(`change 9 3 0 1 {"\\n"}`, () => {
validateEditAtLineCharIndex(9, 3, 0, "\n");
});
it(`change 10 1 0 10 {"\\n\\n\\n\\n\\n\\n\\n\\n\\n\\n"}`, () => {
validateEditAtLineCharIndex(10, 1, 0, "\n\n\n\n\n\n\n\n\n\n");
});
it(`change 19 1 1 0`, () => {
validateEditAtLineCharIndex(19, 1, 1, "");
});
it(`change 18 1 1 0`, () => {
validateEditAtLineCharIndex(18, 1, 1, "");
});
});
describe(`VersionCache simple text`, () => {
let validateEditAtPosition: (position: number, deleteLength: number, insertString: string) => void;
let testContent: string;
let lines: string[];
let lineMap: number[];
before(() => {
testContent = `in this story:
the lazy brown fox
jumped over the cow
that ate the grass
that was purple at the tips
and grew 1cm per day`;
({ lines, lineMap } = server.LineIndex.linesFromText(testContent));
assert.isTrue(lines.length > 0, "Failed to initialize test text. Expected text to have at least one line");
const lineIndex = new server.LineIndex();
lineIndex.load(lines);
validateEditAtPosition = (position: number, deleteLength: number, insertString: string) => {
validateEdit(lineIndex, testContent, position, deleteLength, insertString);
};
});
after(() => {
validateEditAtPosition = undefined!;
testContent = undefined!;
lines = undefined!;
lineMap = undefined!;
});
it(`Insert at end of file`, () => {
validateEditAtPosition(testContent.length, 0, "hmmmm...\r\n");
});
it(`Unusual line endings merge`, () => {
validateEditAtPosition(lines[0].length - 1, lines[1].length, "");
});
it(`Delete whole line and nothing but line (last line)`, () => {
validateEditAtPosition(lineMap[lineMap.length - 2], lines[lines.length - 1].length, "");
});
it(`Delete whole line and nothing but line (first line)`, () => {
validateEditAtPosition(0, lines[0].length, "");
});
it(`Delete whole line (first line) and insert with no line breaks`, () => {
validateEditAtPosition(0, lines[0].length, "moo, moo, moo! ");
});
it(`Delete whole line (first line) and insert with multiple line breaks`, () => {
validateEditAtPosition(0, lines[0].length, "moo, \r\nmoo, \r\nmoo! ");
});
it(`Delete multiple lines and nothing but lines (first and second lines)`, () => {
validateEditAtPosition(0, lines[0].length + lines[1].length, "");
});
it(`Delete multiple lines and nothing but lines (second and third lines)`, () => {
validateEditAtPosition(lines[0].length, lines[1].length + lines[2].length, "");
});
it(`Insert multiple line breaks`, () => {
validateEditAtPosition(21, 1, "cr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr...\r\ncr");
});
it(`Insert multiple line breaks`, () => {
validateEditAtPosition(21, 1, "cr...\r\ncr...\r\ncr");
});
it(`Insert multiple line breaks with leading \\n`, () => {
validateEditAtPosition(21, 1, "\ncr...\r\ncr...\r\ncr");
});
it(`Single line no line breaks deleted or inserted, delete 1 char`, () => {
validateEditAtPosition(21, 1, "");
});
it(`Single line no line breaks deleted or inserted, insert 1 char`, () => {
validateEditAtPosition(21, 0, "b");
});
it(`Single line no line breaks deleted or inserted, delete 1, insert 2 chars`, () => {
validateEditAtPosition(21, 1, "cr");
});
it(`Delete across line break (just the line break)`, () => {
validateEditAtPosition(21, 22, "");
});
it(`Delete across line break`, () => {
validateEditAtPosition(21, 32, "");
});
it(`Delete across multiple line breaks and insert no line breaks`, () => {
validateEditAtPosition(21, 42, "");
});
it(`Delete across multiple line breaks and insert text`, () => {
validateEditAtPosition(21, 42, "slithery ");
});
});
describe(`VersionCache stress test`, () => {
let rsa: number[] = [];
let la: number[] = [];
let las: number[] = [];
let elas: number[] = [];
let ersa: number[] = [];
let ela: number[] = [];
const iterationCount = 20;
// const iterationCount = 20000; // uncomment for testing
let lines: string[];
let lineMap: number[];
let lineIndex: server.LineIndex;
let testContent: string;
before(() => {
// Use scanner.ts, decent size, does not change frequently
const testFileName = "src/compiler/scanner.ts";
testContent = Harness.IO.readFile(testFileName)!;
const totalChars = testContent.length;
assert.isTrue(totalChars > 0, "Failed to read test file.");
({ lines, lineMap } = server.LineIndex.linesFromText(testContent));
assert.isTrue(lines.length > 0, "Failed to initialize test text. Expected text to have at least one line");
lineIndex = new server.LineIndex();
lineIndex.load(lines);
let etotalChars = totalChars;
for (let j = 0; j < 100000; j++) {
rsa[j] = Math.floor(Math.random() * totalChars);
la[j] = Math.floor(Math.random() * (totalChars - rsa[j]));
if (la[j] > 4) {
las[j] = 4;
}
else {
las[j] = la[j];
}
if (j < 4000) {
ersa[j] = Math.floor(Math.random() * etotalChars);
ela[j] = Math.floor(Math.random() * (etotalChars - ersa[j]));
if (ela[j] > 4) {
elas[j] = 4;
}
else {
elas[j] = ela[j];
}
etotalChars += (las[j] - elas[j]);
}
}
});
after(() => {
rsa = undefined!;
la = undefined!;
las = undefined!;
elas = undefined!;
ersa = undefined!;
ela = undefined!;
lines = undefined!;
lineMap = undefined!;
lineIndex = undefined!;
testContent = undefined!;
});
it("Range (average length 1/4 file size)", () => {
for (let i = 0; i < iterationCount; i++) {
const s2 = lineIndex.getText(rsa[i], la[i]);
const s1 = testContent.substring(rsa[i], rsa[i] + la[i]);
assert.equal(s1, s2);
}
});
it("Range (average length 4 chars)", () => {
for (let j = 0; j < iterationCount; j++) {
const s2 = lineIndex.getText(rsa[j], las[j]);
const s1 = testContent.substring(rsa[j], rsa[j] + las[j]);
assert.equal(s1, s2);
}
});
it("Edit (average length 4)", () => {
for (let i = 0; i < iterationCount; i++) {
const insertString = testContent.substring(rsa[100000 - i], rsa[100000 - i] + las[100000 - i]);
const snapshot = lineIndex.edit(rsa[i], las[i], insertString);
const checkText = editFlat(rsa[i], las[i], insertString, testContent);
const snapText = snapshot.getText(0, checkText.length);
assert.equal(checkText, snapText);
}
});
it("Edit ScriptVersionCache ", () => {
const svc = server.ScriptVersionCache.fromString(testContent);
let checkText = testContent;
for (let i = 0; i < iterationCount; i++) {
const insertString = testContent.substring(rsa[i], rsa[i] + las[i]);
svc.edit(ersa[i], elas[i], insertString);
checkText = editFlat(ersa[i], elas[i], insertString, checkText);
if (0 === (i % 4)) {
assert.equal(checkText, getSnapshotText(svc.getSnapshot()));
}
}
});
it("Edit (average length 1/4th file size)", () => {
for (let i = 0; i < iterationCount; i++) {
const insertString = testContent.substring(rsa[100000 - i], rsa[100000 - i] + la[100000 - i]);
const snapshot = lineIndex.edit(rsa[i], la[i], insertString);
const checkText = editFlat(rsa[i], la[i], insertString, testContent);
const snapText = snapshot.getText(0, checkText.length);
assert.equal(checkText, snapText);
}
});
it("Line/offset from pos", () => {
for (let i = 0; i < iterationCount; i++) {
const lp = lineIndex.positionToLineOffset(rsa[i]);
const lac = computeLineAndCharacterOfPosition(lineMap, rsa[i]);
assert.equal(lac.line + 1, lp.line, "Line number mismatch " + (lac.line + 1) + " " + lp.line + " " + i);
assert.equal(lac.character, lp.offset - 1, "Character offset mismatch " + lac.character + " " + (lp.offset - 1) + " " + i);
}
});
it("Start pos from line", () => {
for (let i = 0; i < iterationCount; i++) {
for (let j = 0; j < lines.length; j++) {
assert.equal(lineIndex.absolutePositionOfStartOfLine(j + 1), lineMap[j]);
}
}
});
});
}