@@ -1894,50 +1894,6 @@ namespace ts {
18941894 }
18951895 }
18961896
1897- class SyntaxTreeCache {
1898- // For our syntactic only features, we also keep a cache of the syntax tree for the
1899- // currently edited file.
1900- private currentFileName : string ;
1901- private currentFileVersion : string ;
1902- private currentFileScriptSnapshot : IScriptSnapshot ;
1903- private currentSourceFile : SourceFile ;
1904-
1905- constructor ( private host : LanguageServiceHost ) {
1906- }
1907-
1908- public getCurrentSourceFile ( fileName : string ) : SourceFile {
1909- const scriptSnapshot = this . host . getScriptSnapshot ( fileName ) ;
1910- if ( ! scriptSnapshot ) {
1911- // The host does not know about this file.
1912- throw new Error ( "Could not find file: '" + fileName + "'." ) ;
1913- }
1914-
1915- const scriptKind = getScriptKind ( fileName , this . host ) ;
1916- const version = this . host . getScriptVersion ( fileName ) ;
1917- let sourceFile : SourceFile ;
1918-
1919- if ( this . currentFileName !== fileName ) {
1920- // This is a new file, just parse it
1921- sourceFile = createLanguageServiceSourceFile ( fileName , scriptSnapshot , ScriptTarget . Latest , version , /*setNodeParents*/ true , scriptKind ) ;
1922- }
1923- else if ( this . currentFileVersion !== version ) {
1924- // This is the same file, just a newer version. Incrementally parse the file.
1925- const editRange = scriptSnapshot . getChangeRange ( this . currentFileScriptSnapshot ) ;
1926- sourceFile = updateLanguageServiceSourceFile ( this . currentSourceFile , scriptSnapshot , version , editRange ) ;
1927- }
1928-
1929- if ( sourceFile ) {
1930- // All done, ensure state is up to date
1931- this . currentFileVersion = version ;
1932- this . currentFileName = fileName ;
1933- this . currentFileScriptSnapshot = scriptSnapshot ;
1934- this . currentSourceFile = sourceFile ;
1935- }
1936-
1937- return this . currentSourceFile ;
1938- }
1939- }
1940-
19411897 function setSourceFileFields ( sourceFile : SourceFile , scriptSnapshot : IScriptSnapshot , version : string ) {
19421898 sourceFile . version = version ;
19431899 sourceFile . scriptSnapshot = scriptSnapshot ;
@@ -2919,7 +2875,7 @@ namespace ts {
29192875 export function createLanguageService ( host : LanguageServiceHost ,
29202876 documentRegistry : DocumentRegistry = createDocumentRegistry ( host . useCaseSensitiveFileNames && host . useCaseSensitiveFileNames ( ) , host . getCurrentDirectory ( ) ) ) : LanguageService {
29212877
2922- const syntaxTreeCache : SyntaxTreeCache = new SyntaxTreeCache ( host ) ;
2878+ const syntaxTreeCache = createSyntaxTreeCache ( ) ;
29232879 let ruleProvider : formatting . RulesProvider ;
29242880 let program : Program ;
29252881 let lastProjectVersion : string ;
@@ -3153,6 +3109,64 @@ namespace ts {
31533109 }
31543110 }
31553111
3112+ function createSyntaxTreeCache ( ) {
3113+ let currentFileName : string ;
3114+ let currentFileVersion : string ;
3115+ let currentFileScriptSnapshot : IScriptSnapshot ;
3116+ let currentSourceFile : SourceFile ;
3117+ let currentScriptKind : ScriptKind ;
3118+ let currentCompilerOptions : CompilerOptions ;
3119+ return {
3120+ getCurrentSourceFile : function ( fileName : string ) {
3121+ const scriptSnapshot = host . getScriptSnapshot ( fileName ) ;
3122+ if ( ! scriptSnapshot ) {
3123+ // The host does not know about this file.
3124+ throw new Error ( "Could not find file: '" + fileName + "'." ) ;
3125+ }
3126+ const version = host . getScriptVersion ( fileName ) ;
3127+ const scriptKind = ts . getScriptKind ( fileName , host ) ;
3128+ const compilerOptions = host . getCompilationSettings ( ) ;
3129+ let sourceFile : SourceFile ;
3130+ if ( currentFileName !== fileName ) {
3131+ // Release the current document
3132+ if ( currentFileName ) {
3133+ documentRegistry . releaseDocument ( currentFileName , currentCompilerOptions ) ;
3134+ }
3135+ // This is a new file, just parse it
3136+ sourceFile = documentRegistry . acquireDocument ( fileName , compilerOptions , scriptSnapshot , version , scriptKind ) ;
3137+ }
3138+ else if ( currentFileVersion !== version ) {
3139+ // This is the same file, just a newer version. Incrementally parse the file.
3140+ sourceFile = documentRegistry . updateDocument ( fileName , compilerOptions , scriptSnapshot , version , scriptKind ) ;
3141+ }
3142+ if ( sourceFile ) {
3143+ // All done, ensure state is up to date
3144+ currentFileVersion = version ;
3145+ currentFileName = fileName ;
3146+ currentScriptKind = scriptKind ;
3147+ currentFileScriptSnapshot = scriptSnapshot ;
3148+ currentSourceFile = sourceFile ;
3149+ currentCompilerOptions = compilerOptions ;
3150+ }
3151+ if ( currentSourceFile && ! currentSourceFile . locals ) {
3152+ fixupParentReferences ( currentSourceFile ) ;
3153+ }
3154+ return currentSourceFile ;
3155+ } ,
3156+ dispose : function ( ) {
3157+ if ( currentFileName ) {
3158+ documentRegistry . releaseDocument ( currentFileName , currentCompilerOptions ) ;
3159+ currentFileVersion = undefined ;
3160+ currentFileName = undefined ;
3161+ currentScriptKind = undefined ;
3162+ currentFileScriptSnapshot = undefined ;
3163+ currentSourceFile = undefined ;
3164+ currentCompilerOptions = undefined ;
3165+ }
3166+ }
3167+ } ;
3168+ }
3169+
31563170 function getProgram ( ) : Program {
31573171 synchronizeHostData ( ) ;
31583172
@@ -3170,6 +3184,7 @@ namespace ts {
31703184 documentRegistry . releaseDocumentWithKey ( file . path , key ) ;
31713185 }
31723186 }
3187+ syntaxTreeCache . dispose ( ) ;
31733188 }
31743189
31753190 /// Diagnostics
0 commit comments