From d2fd137d882b66a1f1dd73be5fd3570c4654fa85 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 12 Jan 2018 10:44:39 -0800 Subject: [PATCH] Add a `getFullText()` helper method to `IScriptSnapshot` (#21155) * Add a `getFullText()` helper method to `IScriptSnapshot` * Use a function instead of a method --- src/harness/fourslash.ts | 6 +---- src/harness/harnessLanguageService.ts | 23 +++++-------------- src/harness/unittests/hostNewLineSupport.ts | 23 +++---------------- src/harness/unittests/incrementalParser.ts | 6 ++--- .../unittests/tsserverProjectSystem.ts | 11 ++++----- src/harness/unittests/versionCache.ts | 4 +--- src/server/client.ts | 3 +-- src/server/scriptInfo.ts | 3 +-- src/server/scriptVersionCache.ts | 2 +- src/server/session.ts | 4 +--- src/services/services.ts | 5 ++-- src/services/shims.ts | 6 ++--- src/services/utilities.ts | 4 ++++ 13 files changed, 31 insertions(+), 69 deletions(-) diff --git a/src/harness/fourslash.ts b/src/harness/fourslash.ts index 8c5570f730c..2e98a95831f 100644 --- a/src/harness/fourslash.ts +++ b/src/harness/fourslash.ts @@ -170,11 +170,7 @@ namespace FourSlash { // This function creates IScriptSnapshot object for testing getPreProcessedFileInfo // Return object may lack some functionalities for other purposes. function createScriptSnapShot(sourceText: string): ts.IScriptSnapshot { - return { - getText: (start: number, end: number) => sourceText.substr(start, end - start), - getLength: () => sourceText.length, - getChangeRange: () => undefined - }; + return ts.ScriptSnapshot.fromString(sourceText); } export class TestState { diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index d51517c159b..0e142debedc 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -84,7 +84,7 @@ namespace Harness.LanguageService { } class ScriptSnapshotProxy implements ts.ScriptSnapshotShim { - constructor(public scriptSnapshot: ts.IScriptSnapshot) { + constructor(private readonly scriptSnapshot: ts.IScriptSnapshot) { } public getText(start: number, end: number): string { @@ -96,14 +96,8 @@ namespace Harness.LanguageService { } public getChangeRange(oldScript: ts.ScriptSnapshotShim): string { - const oldShim = oldScript; - - const range = this.scriptSnapshot.getChangeRange(oldShim.scriptSnapshot); - if (range === undefined) { - return undefined; - } - - return JSON.stringify({ span: { start: range.span.start, length: range.span.length }, newLength: range.newLength }); + const range = this.scriptSnapshot.getChangeRange((oldScript as ScriptSnapshotProxy).scriptSnapshot); + return range && JSON.stringify(range); } } @@ -236,12 +230,7 @@ namespace Harness.LanguageService { } readFile(path: string): string | undefined { const target = this.symlinks.get(path); - if (target !== undefined) { - return this.readFile(target); - } - - const snapshot = this.getScriptSnapshot(path); - return snapshot.getText(0, snapshot.getLength()); + return target !== undefined ? this.readFile(target) : ts.getSnapshotText(this.getScriptSnapshot(path)); } addSymlink(from: string, target: string) { this.symlinks.set(from, target); } realpath(path: string): string { @@ -350,7 +339,7 @@ namespace Harness.LanguageService { fileExists(fileName: string) { return this.getScriptInfo(fileName) !== undefined; } readFile(fileName: string) { const snapshot = this.nativeHost.getScriptSnapshot(fileName); - return snapshot && snapshot.getText(0, snapshot.getLength()); + return snapshot && ts.getSnapshotText(snapshot); } log(s: string): void { this.nativeHost.log(s); } trace(s: string): void { this.nativeHost.trace(s); } @@ -654,7 +643,7 @@ namespace Harness.LanguageService { } const snapshot = this.host.getScriptSnapshot(fileName); - return snapshot && snapshot.getText(0, snapshot.getLength()); + return snapshot && ts.getSnapshotText(snapshot); } writeFile = ts.noop; diff --git a/src/harness/unittests/hostNewLineSupport.ts b/src/harness/unittests/hostNewLineSupport.ts index 95a05f101f7..e7e5c11e33e 100644 --- a/src/harness/unittests/hostNewLineSupport.ts +++ b/src/harness/unittests/hostNewLineSupport.ts @@ -4,27 +4,10 @@ namespace ts { function testLSWithFiles(settings: CompilerOptions, files: Harness.Compiler.TestFile[]) { function snapFor(path: string): IScriptSnapshot { if (path === "lib.d.ts") { - return { - dispose: noop, - getChangeRange() { return undefined; }, - getLength() { return 0; }, - getText() { - return ""; - } - }; + return ScriptSnapshot.fromString(""); } - const result = forEach(files, f => f.unitName === path ? f : undefined); - if (result) { - return { - dispose: noop, - getChangeRange() { return undefined; }, - getLength() { return result.content.length; }, - getText(start, end) { - return result.content.substring(start, end); - } - }; - } - return undefined; + const result = find(files, f => f.unitName === path); + return result && ScriptSnapshot.fromString(result.content); } const lshost: LanguageServiceHost = { getCompilationSettings: () => settings, diff --git a/src/harness/unittests/incrementalParser.ts b/src/harness/unittests/incrementalParser.ts index c71b89d3da6..cdccce418bc 100644 --- a/src/harness/unittests/incrementalParser.ts +++ b/src/harness/unittests/incrementalParser.ts @@ -5,7 +5,7 @@ namespace ts { ts.disableIncrementalParsing = false; function withChange(text: IScriptSnapshot, start: number, length: number, newText: string): { text: IScriptSnapshot; textChangeRange: TextChangeRange; } { - const contents = text.getText(0, text.getLength()); + const contents = getSnapshotText(text); const newContents = contents.substr(0, start) + newText + contents.substring(start + length); return { text: ScriptSnapshot.fromString(newContents), textChangeRange: createTextChangeRange(createTextSpan(start, length), newText.length) }; @@ -105,7 +105,7 @@ namespace ts { const newTextAndChange = withDelete(oldText, index, 1); const newTree = compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, -1, oldTree).incrementalNewTree; - source = newTextAndChange.text.getText(0, newTextAndChange.text.getLength()); + source = getSnapshotText(newTextAndChange.text); oldTree = newTree; } } @@ -118,7 +118,7 @@ namespace ts { const newTextAndChange = withInsert(oldText, index + i, toInsert.charAt(i)); const newTree = compareTrees(oldText, newTextAndChange.text, newTextAndChange.textChangeRange, -1, oldTree).incrementalNewTree; - source = newTextAndChange.text.getText(0, newTextAndChange.text.getLength()); + source = getSnapshotText(newTextAndChange.text); oldTree = newTree; } } diff --git a/src/harness/unittests/tsserverProjectSystem.ts b/src/harness/unittests/tsserverProjectSystem.ts index fe7a2095b86..565c20ac9cd 100644 --- a/src/harness/unittests/tsserverProjectSystem.ts +++ b/src/harness/unittests/tsserverProjectSystem.ts @@ -2116,7 +2116,7 @@ namespace ts.projectSystem { const scriptInfo = project.getScriptInfo(file1.path); const snap = scriptInfo.getSnapshot(); - const actualText = snap.getText(0, snap.getLength()); + const actualText = getSnapshotText(snap); assert.equal(actualText, "", `expected content to be empty string, got "${actualText}"`); projectService.openClientFile(file1.path, `var x = 1;`); @@ -2128,8 +2128,7 @@ namespace ts.projectSystem { projectService.closeClientFile(file1.path); const scriptInfo2 = project.getScriptInfo(file1.path); - const snap2 = scriptInfo2.getSnapshot(); - const actualText2 = snap2.getText(0, snap.getLength()); + const actualText2 = getSnapshotText(scriptInfo2.getSnapshot()); assert.equal(actualText2, "", `expected content to be empty string, got "${actualText2}"`); }); @@ -4178,7 +4177,7 @@ namespace ts.projectSystem { // verify content const projectServiice = session.getProjectService(); const snap1 = projectServiice.getScriptInfo(f1.path).getSnapshot(); - assert.equal(snap1.getText(0, snap1.getLength()), tmp.content, "content should be equal to the content of temp file"); + assert.equal(getSnapshotText(snap1), tmp.content, "content should be equal to the content of temp file"); // reload from original file file session.executeCommand({ @@ -4190,7 +4189,7 @@ namespace ts.projectSystem { // verify content const snap2 = projectServiice.getScriptInfo(f1.path).getSnapshot(); - assert.equal(snap2.getText(0, snap2.getLength()), f1.content, "content should be equal to the content of original file"); + assert.equal(getSnapshotText(snap2), f1.content, "content should be equal to the content of original file"); }); @@ -4280,7 +4279,7 @@ namespace ts.projectSystem { function checkScriptInfoContents(contentsOfInfo: string, captionForContents: string) { const snap = info.getSnapshot(); - assert.equal(snap.getText(0, snap.getLength()), contentsOfInfo, "content should be equal to " + captionForContents); + assert.equal(getSnapshotText(snap), contentsOfInfo, "content should be equal to " + captionForContents); } }); }); diff --git a/src/harness/unittests/versionCache.ts b/src/harness/unittests/versionCache.ts index bbd23f25dac..3062a55170c 100644 --- a/src/harness/unittests/versionCache.ts +++ b/src/harness/unittests/versionCache.ts @@ -284,9 +284,7 @@ and grew 1cm per day`; svc.edit(ersa[i], elas[i], insertString); checkText = editFlat(ersa[i], elas[i], insertString, checkText); if (0 === (i % 4)) { - const snap = svc.getSnapshot(); - const snapText = snap.getText(0, checkText.length); - assert.equal(checkText, snapText); + assert.equal(checkText, getSnapshotText(svc.getSnapshot())); } } }); diff --git a/src/server/client.ts b/src/server/client.ts index f34516ac4fd..7a64d9b528c 100644 --- a/src/server/client.ts +++ b/src/server/client.ts @@ -52,8 +52,7 @@ namespace ts.server { private getLineMap(fileName: string): number[] { let lineMap = this.lineMaps.get(fileName); if (!lineMap) { - const scriptSnapshot = this.host.getScriptSnapshot(fileName); - lineMap = computeLineStarts(scriptSnapshot.getText(0, scriptSnapshot.getLength())); + lineMap = computeLineStarts(getSnapshotText(this.host.getScriptSnapshot(fileName))); this.lineMaps.set(fileName, lineMap); } return lineMap; diff --git a/src/server/scriptInfo.ts b/src/server/scriptInfo.ts index f800a1117d0..6a722b5c3e0 100644 --- a/src/server/scriptInfo.ts +++ b/src/server/scriptInfo.ts @@ -370,8 +370,7 @@ namespace ts.server { } saveTo(fileName: string) { - const snap = this.textStorage.getSnapshot(); - this.host.writeFile(fileName, snap.getText(0, snap.getLength())); + this.host.writeFile(fileName, getSnapshotText(this.textStorage.getSnapshot())); } /*@internal*/ diff --git a/src/server/scriptVersionCache.ts b/src/server/scriptVersionCache.ts index 9650130634e..dccf4d3267b 100644 --- a/src/server/scriptVersionCache.ts +++ b/src/server/scriptVersionCache.ts @@ -371,7 +371,7 @@ namespace ts.server { } getLength() { - return this.index.root.charCount(); + return this.index.getLength(); } getChangeRange(oldSnapshot: IScriptSnapshot): TextChangeRange { diff --git a/src/server/session.ts b/src/server/session.ts index 693d1651c91..b66bd83c19f 100644 --- a/src/server/session.ts +++ b/src/server/session.ts @@ -1534,9 +1534,7 @@ namespace ts.server { let mappedRenameLocation: protocol.Location | undefined; if (renameFilename !== undefined && renameLocation !== undefined) { const renameScriptInfo = project.getScriptInfoForNormalizedPath(toNormalizedPath(renameFilename)); - const snapshot = renameScriptInfo.getSnapshot(); - const oldText = snapshot.getText(0, snapshot.getLength()); - mappedRenameLocation = getLocationInNewDocument(oldText, renameFilename, renameLocation, edits); + mappedRenameLocation = getLocationInNewDocument(getSnapshotText(renameScriptInfo.getSnapshot()), renameFilename, renameLocation, edits); } return { renameLocation: mappedRenameLocation, renameFilename, edits: this.mapTextChangesToCodeEdits(project, edits) }; } diff --git a/src/services/services.ts b/src/services/services.ts index eb5e3ae5743..90e9b64a1df 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1065,8 +1065,7 @@ namespace ts { } export function createLanguageServiceSourceFile(fileName: string, scriptSnapshot: IScriptSnapshot, scriptTarget: ScriptTarget, version: string, setNodeParents: boolean, scriptKind?: ScriptKind): SourceFile { - const text = scriptSnapshot.getText(0, scriptSnapshot.getLength()); - const sourceFile = createSourceFile(fileName, text, scriptTarget, setNodeParents, scriptKind); + const sourceFile = createSourceFile(fileName, getSnapshotText(scriptSnapshot), scriptTarget, setNodeParents, scriptKind); setSourceFileFields(sourceFile, scriptSnapshot, version); return sourceFile; } @@ -1265,7 +1264,7 @@ namespace ts { const path = toPath(fileName, currentDirectory, getCanonicalFileName); const entry = hostCache.getEntryByPath(path); if (entry) { - return isString(entry) ? undefined : entry.scriptSnapshot.getText(0, entry.scriptSnapshot.getLength()); + return isString(entry) ? undefined : getSnapshotText(entry.scriptSnapshot); } return host.readFile && host.readFile(fileName); }, diff --git a/src/services/shims.ts b/src/services/shims.ts index fda698785b3..30a836a113f 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -1083,7 +1083,7 @@ namespace ts { `getPreProcessedFileInfo('${fileName}')`, () => { // for now treat files as JavaScript - const result = preProcessFile(sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()), /* readImportFiles */ true, /* detectJavaScriptImports */ true); + const result = preProcessFile(getSnapshotText(sourceTextSnapshot), /* readImportFiles */ true, /* detectJavaScriptImports */ true); return { referencedFiles: this.convertFileReferences(result.referencedFiles), importedFiles: this.convertFileReferences(result.importedFiles), @@ -1123,9 +1123,7 @@ namespace ts { return this.forwardJSONCall( `getTSConfigFileInfo('${fileName}')`, () => { - const text = sourceTextSnapshot.getText(0, sourceTextSnapshot.getLength()); - - const result = parseJsonText(fileName, text); + const result = parseJsonText(fileName, getSnapshotText(sourceTextSnapshot)); const normalizedFileName = normalizeSlashes(fileName); const configFile = parseJsonSourceFileConfigFileContent(result, this.host, getDirectoryPath(normalizedFileName), /*existingOptions*/ {}, normalizedFileName); diff --git a/src/services/utilities.ts b/src/services/utilities.ts index 8fe12d29313..9562def31f5 100644 --- a/src/services/utilities.ts +++ b/src/services/utilities.ts @@ -1107,6 +1107,10 @@ namespace ts { seen.set(key, true); return true; } + + export function getSnapshotText(snap: IScriptSnapshot): string { + return snap.getText(0, snap.getLength()); + } } // Display-part writer helpers