Skip to content

Commit d2fd137

Browse files
author
Andy
authored
Add a getFullText() helper method to IScriptSnapshot (#21155)
* Add a `getFullText()` helper method to `IScriptSnapshot` * Use a function instead of a method
1 parent b529d5b commit d2fd137

13 files changed

+31
-69
lines changed

src/harness/fourslash.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,7 @@ namespace FourSlash {
170170
// This function creates IScriptSnapshot object for testing getPreProcessedFileInfo
171171
// Return object may lack some functionalities for other purposes.
172172
function createScriptSnapShot(sourceText: string): ts.IScriptSnapshot {
173-
return {
174-
getText: (start: number, end: number) => sourceText.substr(start, end - start),
175-
getLength: () => sourceText.length,
176-
getChangeRange: () => undefined
177-
};
173+
return ts.ScriptSnapshot.fromString(sourceText);
178174
}
179175

180176
export class TestState {

src/harness/harnessLanguageService.ts

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ namespace Harness.LanguageService {
8484
}
8585

8686
class ScriptSnapshotProxy implements ts.ScriptSnapshotShim {
87-
constructor(public scriptSnapshot: ts.IScriptSnapshot) {
87+
constructor(private readonly scriptSnapshot: ts.IScriptSnapshot) {
8888
}
8989

9090
public getText(start: number, end: number): string {
@@ -96,14 +96,8 @@ namespace Harness.LanguageService {
9696
}
9797

9898
public getChangeRange(oldScript: ts.ScriptSnapshotShim): string {
99-
const oldShim = <ScriptSnapshotProxy>oldScript;
100-
101-
const range = this.scriptSnapshot.getChangeRange(oldShim.scriptSnapshot);
102-
if (range === undefined) {
103-
return undefined;
104-
}
105-
106-
return JSON.stringify({ span: { start: range.span.start, length: range.span.length }, newLength: range.newLength });
99+
const range = this.scriptSnapshot.getChangeRange((oldScript as ScriptSnapshotProxy).scriptSnapshot);
100+
return range && JSON.stringify(range);
107101
}
108102
}
109103

@@ -236,12 +230,7 @@ namespace Harness.LanguageService {
236230
}
237231
readFile(path: string): string | undefined {
238232
const target = this.symlinks.get(path);
239-
if (target !== undefined) {
240-
return this.readFile(target);
241-
}
242-
243-
const snapshot = this.getScriptSnapshot(path);
244-
return snapshot.getText(0, snapshot.getLength());
233+
return target !== undefined ? this.readFile(target) : ts.getSnapshotText(this.getScriptSnapshot(path));
245234
}
246235
addSymlink(from: string, target: string) { this.symlinks.set(from, target); }
247236
realpath(path: string): string {
@@ -350,7 +339,7 @@ namespace Harness.LanguageService {
350339
fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; }
351340
readFile(fileName: string) {
352341
const snapshot = this.nativeHost.getScriptSnapshot(fileName);
353-
return snapshot && snapshot.getText(0, snapshot.getLength());
342+
return snapshot && ts.getSnapshotText(snapshot);
354343
}
355344
log(s: string): void { this.nativeHost.log(s); }
356345
trace(s: string): void { this.nativeHost.trace(s); }
@@ -654,7 +643,7 @@ namespace Harness.LanguageService {
654643
}
655644

656645
const snapshot = this.host.getScriptSnapshot(fileName);
657-
return snapshot && snapshot.getText(0, snapshot.getLength());
646+
return snapshot && ts.getSnapshotText(snapshot);
658647
}
659648

660649
writeFile = ts.noop;

src/harness/unittests/hostNewLineSupport.ts

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,10 @@ namespace ts {
44
function testLSWithFiles(settings: CompilerOptions, files: Harness.Compiler.TestFile[]) {
55
function snapFor(path: string): IScriptSnapshot {
66
if (path === "lib.d.ts") {
7-
return {
8-
dispose: noop,
9-
getChangeRange() { return undefined; },
10-
getLength() { return 0; },
11-
getText() {
12-
return "";
13-
}
14-
};
7+
return ScriptSnapshot.fromString("");
158
}
16-
const result = forEach(files, f => f.unitName === path ? f : undefined);
17-
if (result) {
18-
return {
19-
dispose: noop,
20-
getChangeRange() { return undefined; },
21-
getLength() { return result.content.length; },
22-
getText(start, end) {
23-
return result.content.substring(start, end);
24-
}
25-
};
26-
}
27-
return undefined;
9+
const result = find(files, f => f.unitName === path);
10+
return result && ScriptSnapshot.fromString(result.content);
2811
}
2912
const lshost: LanguageServiceHost = {
3013
getCompilationSettings: () => settings,

src/harness/unittests/incrementalParser.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace ts {
55
ts.disableIncrementalParsing = false;
66

77
function withChange(text: IScriptSnapshot, start: number, length: number, newText: string): { text: IScriptSnapshot; textChangeRange: TextChangeRange; } {
8-
const contents = text.getText(0, text.getLength());
8+
const contents = getSnapshotText(text);
99
const newContents = contents.substr(0, start) + newText + contents.substring(start + length);
1010

1111
return { text: ScriptSnapshot.fromString(newContents), textChangeRange: createTextChangeRange(createTextSpan(start, length), newText.length) };
@@ -105,7 +105,7 @@ namespace ts {
105105
const newTextAndChange = withDelete(oldText, index, 1);
106106
const newTree = compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, -1, oldTree).incrementalNewTree;
107107

108-
source = newTextAndChange.text.getText(0, newTextAndChange.text.getLength());
108+
source = getSnapshotText(newTextAndChange.text);
109109
oldTree = newTree;
110110
}
111111
}
@@ -118,7 +118,7 @@ namespace ts {
118118
const newTextAndChange = withInsert(oldText, index + i, toInsert.charAt(i));
119119
const newTree = compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, -1, oldTree).incrementalNewTree;
120120

121-
source = newTextAndChange.text.getText(0, newTextAndChange.text.getLength());
121+
source = getSnapshotText(newTextAndChange.text);
122122
oldTree = newTree;
123123
}
124124
}

src/harness/unittests/tsserverProjectSystem.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2116,7 +2116,7 @@ namespace ts.projectSystem {
21162116

21172117
const scriptInfo = project.getScriptInfo(file1.path);
21182118
const snap = scriptInfo.getSnapshot();
2119-
const actualText = snap.getText(0, snap.getLength());
2119+
const actualText = getSnapshotText(snap);
21202120
assert.equal(actualText, "", `expected content to be empty string, got "${actualText}"`);
21212121

21222122
projectService.openClientFile(file1.path, `var x = 1;`);
@@ -2128,8 +2128,7 @@ namespace ts.projectSystem {
21282128
projectService.closeClientFile(file1.path);
21292129

21302130
const scriptInfo2 = project.getScriptInfo(file1.path);
2131-
const snap2 = scriptInfo2.getSnapshot();
2132-
const actualText2 = snap2.getText(0, snap.getLength());
2131+
const actualText2 = getSnapshotText(scriptInfo2.getSnapshot());
21332132
assert.equal(actualText2, "", `expected content to be empty string, got "${actualText2}"`);
21342133
});
21352134

@@ -4178,7 +4177,7 @@ namespace ts.projectSystem {
41784177
// verify content
41794178
const projectServiice = session.getProjectService();
41804179
const snap1 = projectServiice.getScriptInfo(f1.path).getSnapshot();
4181-
assert.equal(snap1.getText(0, snap1.getLength()), tmp.content, "content should be equal to the content of temp file");
4180+
assert.equal(getSnapshotText(snap1), tmp.content, "content should be equal to the content of temp file");
41824181

41834182
// reload from original file file
41844183
session.executeCommand(<server.protocol.ReloadRequest>{
@@ -4190,7 +4189,7 @@ namespace ts.projectSystem {
41904189

41914190
// verify content
41924191
const snap2 = projectServiice.getScriptInfo(f1.path).getSnapshot();
4193-
assert.equal(snap2.getText(0, snap2.getLength()), f1.content, "content should be equal to the content of original file");
4192+
assert.equal(getSnapshotText(snap2), f1.content, "content should be equal to the content of original file");
41944193

41954194
});
41964195

@@ -4280,7 +4279,7 @@ namespace ts.projectSystem {
42804279

42814280
function checkScriptInfoContents(contentsOfInfo: string, captionForContents: string) {
42824281
const snap = info.getSnapshot();
4283-
assert.equal(snap.getText(0, snap.getLength()), contentsOfInfo, "content should be equal to " + captionForContents);
4282+
assert.equal(getSnapshotText(snap), contentsOfInfo, "content should be equal to " + captionForContents);
42844283
}
42854284
});
42864285
});

src/harness/unittests/versionCache.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,7 @@ and grew 1cm per day`;
284284
svc.edit(ersa[i], elas[i], insertString);
285285
checkText = editFlat(ersa[i], elas[i], insertString, checkText);
286286
if (0 === (i % 4)) {
287-
const snap = svc.getSnapshot();
288-
const snapText = snap.getText(0, checkText.length);
289-
assert.equal(checkText, snapText);
287+
assert.equal(checkText, getSnapshotText(svc.getSnapshot()));
290288
}
291289
}
292290
});

src/server/client.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ namespace ts.server {
5252
private getLineMap(fileName: string): number[] {
5353
let lineMap = this.lineMaps.get(fileName);
5454
if (!lineMap) {
55-
const scriptSnapshot = this.host.getScriptSnapshot(fileName);
56-
lineMap = computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength()));
55+
lineMap = computeLineStarts(getSnapshotText(this.host.getScriptSnapshot(fileName)));
5756
this.lineMaps.set(fileName, lineMap);
5857
}
5958
return lineMap;

src/server/scriptInfo.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,7 @@ namespace ts.server {
370370
}
371371

372372
saveTo(fileName: string) {
373-
const snap = this.textStorage.getSnapshot();
374-
this.host.writeFile(fileName, snap.getText(0, snap.getLength()));
373+
this.host.writeFile(fileName, getSnapshotText(this.textStorage.getSnapshot()));
375374
}
376375

377376
/*@internal*/

src/server/scriptVersionCache.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ namespace ts.server {
371371
}
372372

373373
getLength() {
374-
return this.index.root.charCount();
374+
return this.index.getLength();
375375
}
376376

377377
getChangeRange(oldSnapshot: IScriptSnapshot): TextChangeRange {

src/server/session.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,9 +1534,7 @@ namespace ts.server {
15341534
let mappedRenameLocation: protocol.Location | undefined;
15351535
if (renameFilename !== undefined && renameLocation !== undefined) {
15361536
const renameScriptInfo = project.getScriptInfoForNormalizedPath(toNormalizedPath(renameFilename));
1537-
const snapshot = renameScriptInfo.getSnapshot();
1538-
const oldText = snapshot.getText(0, snapshot.getLength());
1539-
mappedRenameLocation = getLocationInNewDocument(oldText, renameFilename, renameLocation, edits);
1537+
mappedRenameLocation = getLocationInNewDocument(getSnapshotText(renameScriptInfo.getSnapshot()), renameFilename, renameLocation, edits);
15401538
}
15411539
return { renameLocation: mappedRenameLocation, renameFilename, edits: this.mapTextChangesToCodeEdits(project, edits) };
15421540
}

src/services/services.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,8 +1065,7 @@ namespace ts {
10651065
}
10661066

10671067
export function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile {
1068-
const text = scriptSnapshot.getText(0, scriptSnapshot.getLength());
1069-
const sourceFile = createSourceFile(fileName, text, scriptTarget, setNodeParents, scriptKind);
1068+
const sourceFile = createSourceFile(fileName, getSnapshotText(scriptSnapshot), scriptTarget, setNodeParents, scriptKind);
10701069
setSourceFileFields(sourceFile, scriptSnapshot, version);
10711070
return sourceFile;
10721071
}
@@ -1265,7 +1264,7 @@ namespace ts {
12651264
const path = toPath(fileName, currentDirectory, getCanonicalFileName);
12661265
const entry = hostCache.getEntryByPath(path);
12671266
if (entry) {
1268-
return isString(entry) ? undefined : entry.scriptSnapshot.getText(0, entry.scriptSnapshot.getLength());
1267+
return isString(entry) ? undefined : getSnapshotText(entry.scriptSnapshot);
12691268
}
12701269
return host.readFile && host.readFile(fileName);
12711270
},

src/services/shims.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1083,7 +1083,7 @@ namespace ts {
10831083
`getPreProcessedFileInfo('${fileName}')`,
10841084
() => {
10851085
// for now treat files as JavaScript
1086-
const result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true);
1086+
const result = preProcessFile(getSnapshotText(sourceTextSnapshot), /* readImportFiles */ true, /* detectJavaScriptImports */ true);
10871087
return {
10881088
referencedFiles: this.convertFileReferences(result.referencedFiles),
10891089
importedFiles: this.convertFileReferences(result.importedFiles),
@@ -1123,9 +1123,7 @@ namespace ts {
11231123
return this.forwardJSONCall(
11241124
`getTSConfigFileInfo('${fileName}')`,
11251125
() => {
1126-
const text = sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength());
1127-
1128-
const result = parseJsonText(fileName, text);
1126+
const result = parseJsonText(fileName, getSnapshotText(sourceTextSnapshot));
11291127
const normalizedFileName = normalizeSlashes(fileName);
11301128
const configFile = parseJsonSourceFileConfigFileContent(result, this.host, getDirectoryPath(normalizedFileName), /*existingOptions*/ {}, normalizedFileName);
11311129

src/services/utilities.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,6 +1107,10 @@ namespace ts {
11071107
seen.set(key, true);
11081108
return true;
11091109
}
1110+
1111+
export function getSnapshotText(snap: IScriptSnapshot): string {
1112+
return snap.getText(0, snap.getLength());
1113+
}
11101114
}
11111115

11121116
// Display-part writer helpers

0 commit comments

Comments
 (0)