From 1d8fab8168fb34c7c20b2e4c9b41843be64e8ae3 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Mon, 9 Feb 2015 10:48:59 -0800 Subject: [PATCH] Remove host cache uses in syntactic features --- src/services/services.ts | 47 ++++++++++++---------------------------- 1 file changed, 14 insertions(+), 33 deletions(-) diff --git a/src/services/services.ts b/src/services/services.ts index 56e68b2f5a1..00cd814efb4 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1548,65 +1548,46 @@ module ts { } class SyntaxTreeCache { - private hostCache: HostCache; - // For our syntactic only features, we also keep a cache of the syntax tree for the // currently edited file. - private currentFileName: string = ""; - private currentFileVersion: string = null; - private currentSourceFile: SourceFile = null; + private currentFileName: string; + private currentFileVersion: string; + private currentFileScriptSnapshot: IScriptSnapshot; + private currentSourceFile: SourceFile; constructor(private host: LanguageServiceHost) { } - private log(message: string) { - if (this.host.log) { - this.host.log(message); + public getCurrentSourceFile(fileName: string): SourceFile { + var scriptSnapshot = this.host.getScriptSnapshot(fileName); + if (!scriptSnapshot) { + // The host does not know about this file. + throw new Error("Could not find file: '" + fileName + "'."); } - } - private initialize(fileName: string) { - // ensure that both source file and syntax tree are either initialized or not initialized - var start = new Date().getTime(); - this.hostCache = new HostCache(this.host); - this.log("SyntaxTreeCache.Initialize: new HostCache: " + (new Date().getTime() - start)); - - var version = this.hostCache.getVersion(fileName); + var version = this.host.getScriptVersion(fileName); var sourceFile: SourceFile; if (this.currentFileName !== fileName) { - var scriptSnapshot = this.hostCache.getScriptSnapshot(fileName); - - var start = new Date().getTime(); + // This is a new file, just parse it sourceFile = createLanguageServiceSourceFile(fileName, scriptSnapshot, ScriptTarget.Latest, version, /*setNodeParents:*/ true); - this.log("SyntaxTreeCache.Initialize: createSourceFile: " + (new Date().getTime() - start)); } else if (this.currentFileVersion !== version) { - var scriptSnapshot = this.hostCache.getScriptSnapshot(fileName); - - var editRange = this.hostCache.getChangeRange(fileName, this.currentFileVersion, this.currentSourceFile.scriptSnapshot); - - var start = new Date().getTime(); + // This is the same file, just a newer version. Incrementally parse the file. + var editRange = scriptSnapshot.getChangeRange(this.currentFileScriptSnapshot); sourceFile = updateLanguageServiceSourceFile(this.currentSourceFile, scriptSnapshot, version, editRange); - this.log("SyntaxTreeCache.Initialize: updateSourceFile: " + (new Date().getTime() - start)); } if (sourceFile) { // All done, ensure state is up to date this.currentFileVersion = version; this.currentFileName = fileName; + this.currentFileScriptSnapshot = scriptSnapshot; this.currentSourceFile = sourceFile; } - } - public getCurrentSourceFile(fileName: string): SourceFile { - this.initialize(fileName); return this.currentSourceFile; } - - public getCurrentScriptSnapshot(fileName: string): IScriptSnapshot { - return this.getCurrentSourceFile(fileName).scriptSnapshot; - } } function setSourceFileFields(sourceFile: SourceFile, scriptSnapshot: IScriptSnapshot, version: string) {