@@ -23,12 +23,19 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
2323
2424 let compilerOptions : ts . CompilerOptions = { allowNonTsExtensions : true , allowJs : true , target : ts . ScriptTarget . Latest , moduleResolution : ts . ModuleResolutionKind . Classic } ;
2525 let currentTextDocument : TextDocument ;
26+ let scriptFileVersion : number = 0 ;
27+ function updateCurrentTextDocument ( doc : TextDocument ) {
28+ if ( ! currentTextDocument || doc . uri !== currentTextDocument . uri || doc . version !== currentTextDocument . version ) {
29+ currentTextDocument = jsDocuments . get ( doc ) ;
30+ scriptFileVersion ++ ;
31+ }
32+ }
2633 let host = {
2734 getCompilationSettings : ( ) => compilerOptions ,
2835 getScriptFileNames : ( ) => [ FILE_NAME , JQUERY_D_TS ] ,
2936 getScriptVersion : ( fileName : string ) => {
3037 if ( fileName === FILE_NAME ) {
31- return String ( currentTextDocument . version ) ;
38+ return String ( scriptFileVersion ) ;
3239 }
3340 return '1' ; // default lib an jquery.d.ts are static
3441 } ,
@@ -62,7 +69,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
6269 settings = options && options . javascript ;
6370 } ,
6471 doValidation ( document : TextDocument ) : Diagnostic [ ] {
65- currentTextDocument = jsDocuments . get ( document ) ;
72+ updateCurrentTextDocument ( document ) ;
6673 const diagnostics = jsLanguageService . getSyntacticDiagnostics ( FILE_NAME ) ;
6774 return diagnostics . map ( ( diag ) : Diagnostic => {
6875 return {
@@ -73,7 +80,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
7380 } ) ;
7481 } ,
7582 doComplete ( document : TextDocument , position : Position ) : CompletionList {
76- currentTextDocument = jsDocuments . get ( document ) ;
83+ updateCurrentTextDocument ( document ) ;
7784 let offset = currentTextDocument . offsetAt ( position ) ;
7885 let completions = jsLanguageService . getCompletionsAtPosition ( FILE_NAME , offset ) ;
7986 if ( ! completions ) {
@@ -100,7 +107,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
100107 } ;
101108 } ,
102109 doResolve ( document : TextDocument , item : CompletionItem ) : CompletionItem {
103- currentTextDocument = jsDocuments . get ( document ) ;
110+ updateCurrentTextDocument ( document ) ;
104111 let details = jsLanguageService . getCompletionEntryDetails ( FILE_NAME , item . data . offset , item . label ) ;
105112 if ( details ) {
106113 item . detail = ts . displayPartsToString ( details . displayParts ) ;
@@ -110,7 +117,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
110117 return item ;
111118 } ,
112119 doHover ( document : TextDocument , position : Position ) : Hover {
113- currentTextDocument = jsDocuments . get ( document ) ;
120+ updateCurrentTextDocument ( document ) ;
114121 let info = jsLanguageService . getQuickInfoAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
115122 if ( info ) {
116123 let contents = ts . displayPartsToString ( info . displayParts ) ;
@@ -122,7 +129,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
122129 return null ;
123130 } ,
124131 doSignatureHelp ( document : TextDocument , position : Position ) : SignatureHelp {
125- currentTextDocument = jsDocuments . get ( document ) ;
132+ updateCurrentTextDocument ( document ) ;
126133 let signHelp = jsLanguageService . getSignatureHelpItems ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
127134 if ( signHelp ) {
128135 let ret : SignatureHelp = {
@@ -159,7 +166,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
159166 return null ;
160167 } ,
161168 findDocumentHighlight ( document : TextDocument , position : Position ) : DocumentHighlight [ ] {
162- currentTextDocument = jsDocuments . get ( document ) ;
169+ updateCurrentTextDocument ( document ) ;
163170 let occurrences = jsLanguageService . getOccurrencesAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
164171 if ( occurrences ) {
165172 return occurrences . map ( entry => {
@@ -172,7 +179,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
172179 return null ;
173180 } ,
174181 findDocumentSymbols ( document : TextDocument ) : SymbolInformation [ ] {
175- currentTextDocument = jsDocuments . get ( document ) ;
182+ updateCurrentTextDocument ( document ) ;
176183 let items = jsLanguageService . getNavigationBarItems ( FILE_NAME ) ;
177184 if ( items ) {
178185 let result : SymbolInformation [ ] = [ ] ;
@@ -208,7 +215,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
208215 return null ;
209216 } ,
210217 findDefinition ( document : TextDocument , position : Position ) : Definition {
211- currentTextDocument = jsDocuments . get ( document ) ;
218+ updateCurrentTextDocument ( document ) ;
212219 let definition = jsLanguageService . getDefinitionAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
213220 if ( definition ) {
214221 return definition . filter ( d => d . fileName === FILE_NAME ) . map ( d => {
@@ -221,7 +228,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
221228 return null ;
222229 } ,
223230 findReferences ( document : TextDocument , position : Position ) : Location [ ] {
224- currentTextDocument = jsDocuments . get ( document ) ;
231+ updateCurrentTextDocument ( document ) ;
225232 let references = jsLanguageService . getReferencesAtPosition ( FILE_NAME , currentTextDocument . offsetAt ( position ) ) ;
226233 if ( references ) {
227234 return references . filter ( d => d . fileName === FILE_NAME ) . map ( d => {
@@ -234,7 +241,7 @@ export function getJavascriptMode(documentRegions: LanguageModelCache<HTMLDocume
234241 return null ;
235242 } ,
236243 format ( document : TextDocument , range : Range , formatParams : FormattingOptions ) : TextEdit [ ] {
237- currentTextDocument = jsDocuments . get ( document ) ;
244+ updateCurrentTextDocument ( document ) ;
238245 let initialIndentLevel = computeInitialIndent ( document , range , formatParams ) ;
239246 let formatSettings = convertOptions ( formatParams , settings && settings . format , initialIndentLevel + 1 ) ;
240247 let start = currentTextDocument . offsetAt ( range . start ) ;
0 commit comments