Add a getFullText() helper method to IScriptSnapshot (#21155)

* Add a `getFullText()` helper method to `IScriptSnapshot`

* Use a function instead of a method
This commit is contained in:
Andy 2018-01-12 10:44:39 -08:00 committed by GitHub
parent b529d5ba1f
commit d2fd137d88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 31 additions and 69 deletions

View File

@ -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 {

View File

@ -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 = <ScriptSnapshotProxy>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;

View File

@ -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,

View File

@ -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;
}
}

View File

@ -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(<server.protocol.ReloadRequest>{
@ -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);
}
});
});

View File

@ -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()));
}
}
});

View File

@ -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;

View File

@ -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*/

View File

@ -371,7 +371,7 @@ namespace ts.server {
}
getLength() {
return this.index.root.charCount();
return this.index.getLength();
}
getChangeRange(oldSnapshot: IScriptSnapshot): TextChangeRange {

View File

@ -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) };
}

View File

@ -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);
},

View File

@ -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);

View File

@ -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