@@ -37,8 +37,13 @@ import { CompletionProvider } from "./sas/CompletionProvider";
3737import { LanguageServiceProvider , legend } from "./sas/LanguageServiceProvider" ;
3838import type { LibCompleteItem } from "./sas/SyntaxDataProvider" ;
3939
40- const servicePool : Record < string , LanguageServiceProvider > = { } ;
41- const documentPool : Record < string , TextDocument > = { } ;
40+ interface DocumentInfo {
41+ document : TextDocument ;
42+ changed : boolean ;
43+ service ?: LanguageServiceProvider ;
44+ }
45+
46+ const documentPool : Record < string , DocumentInfo > = { } ;
4247
4348let completionProvider : CompletionProvider ;
4449let connection : Connection ;
@@ -116,6 +121,7 @@ export const init = (conn: Connection): void => {
116121 connection . onInitialized ( ( ) => _pyrightLanguageProvider . onInitialized ( ) ) ;
117122
118123 connection . onRequest ( SemanticTokensRequest . type , ( params ) => {
124+ syncIfDocChange ( params . textDocument . uri ) ;
119125 const languageService = getLanguageService ( params . textDocument . uri ) ;
120126
121127 return { data : languageService . getTokens ( ) } ;
@@ -188,6 +194,7 @@ export const init = (conn: Connection): void => {
188194 } ) ;
189195
190196 connection . onDocumentSymbol ( async ( params , token ) => {
197+ syncIfDocChange ( params . textDocument . uri ) ;
191198 const languageService = getLanguageService ( params . textDocument . uri ) ;
192199 const sasSymbols = languageService . getDocumentSymbols ( ) ;
193200 const pythonSymbols =
@@ -209,6 +216,7 @@ export const init = (conn: Connection): void => {
209216
210217 // todo
211218 connection . onFoldingRanges ( ( params ) => {
219+ syncIfDocChange ( params . textDocument . uri ) ;
212220 const languageService = getLanguageService ( params . textDocument . uri ) ;
213221 return languageService . getFoldingRanges ( ) ;
214222 } ) ;
@@ -267,6 +275,7 @@ export const init = (conn: Connection): void => {
267275 } ) ;
268276
269277 connection . onDocumentFormatting ( ( params ) => {
278+ syncIfDocChange ( params . textDocument . uri ) ;
270279 const languageService = getLanguageService ( params . textDocument . uri ) ;
271280 return languageService . formatter . format ( {
272281 tabWidth : params . options . tabSize ,
@@ -281,27 +290,25 @@ export const init = (conn: Connection): void => {
281290 params . textDocument . version ,
282291 params . textDocument . text ,
283292 ) ;
284- documentPool [ doc . uri ] = doc ;
293+ documentPool [ doc . uri ] = { document : doc , changed : false } ;
285294 await _pyrightLanguageProvider . onDidOpenTextDocument ( params ) ;
286295 } ) ;
287296
288297 connection . onDidCloseTextDocument ( async ( params ) => {
289298 const uri = params . textDocument . uri ;
290299 delete documentPool [ uri ] ;
291- delete servicePool [ uri ] ;
292300 await _pyrightLanguageProvider . onDidCloseTextDocument ( params ) ;
293301 } ) ;
294302
295303 connection . onDidChangeTextDocument ( ( params ) => {
296304 const uri = params . textDocument . uri ;
297- const doc = documentPool [ uri ] ;
305+ const docInfo = documentPool [ uri ] ;
298306 TextDocument . update (
299- doc ,
307+ docInfo . document ,
300308 params . contentChanges ,
301309 params . textDocument . version ,
302310 ) ;
303- delete servicePool [ uri ] ;
304- _pyrightLanguageProvider . addContentChange ( doc ) ;
311+ docInfo . changed = true ;
305312 } ) ;
306313
307314 connection . onDidChangeConfiguration (
@@ -378,6 +385,7 @@ export const init = (conn: Connection): void => {
378385 workDoneProgress ,
379386 resultProgress ,
380387 ) => {
388+ syncAllChangedDoc ( ) ;
381389 return await _pyrightLanguageProvider . onWorkspaceSymbol (
382390 params ,
383391 token ,
@@ -399,21 +407,6 @@ export const init = (conn: Connection): void => {
399407 } ,
400408 ) ;
401409
402- connection . onSignatureHelp (
403- async ( params : SignatureHelpParams , token : CancellationToken ) => {
404- return await dispatch ( params , {
405- async python ( pyrightLanguageService ) {
406- // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
407- return ( await pyrightLanguageService . onSignatureHelp (
408- params ,
409- token ,
410- // eslint-disable-next-line @typescript-eslint/no-explicit-any
411- ) ) as any ;
412- } ,
413- } ) ;
414- } ,
415- ) ;
416-
417410 connection . onPrepareRename (
418411 async ( params : PrepareRenameParams , token : CancellationToken ) => {
419412 return await dispatch ( params , {
@@ -444,6 +437,9 @@ export const init = (conn: Connection): void => {
444437
445438 connection . onDidChangeWatchedFiles (
446439 async ( params : DidChangeWatchedFilesParams ) => {
440+ params . changes . forEach ( ( item ) => {
441+ syncIfDocChange ( item . uri ) ;
442+ } ) ;
447443 _pyrightLanguageProvider . onDidChangeWatchedFiles ( params ) ;
448444 } ,
449445 ) ;
@@ -454,6 +450,7 @@ export const init = (conn: Connection): void => {
454450 token : CancellationToken ,
455451 reporter : WorkDoneProgressReporter ,
456452 ) => {
453+ syncAllChangedDoc ( ) ;
457454 return await _pyrightLanguageProvider . onExecuteCommand (
458455 params ,
459456 token ,
@@ -500,19 +497,20 @@ export const init = (conn: Connection): void => {
500497} ;
501498
502499function getLanguageService ( uri : string ) {
503- if ( ! servicePool [ uri ] ) {
504- // re-create LanguageServer if document changed
505- servicePool [ uri ] = new LanguageServiceProvider ( documentPool [ uri ] ) ;
500+ const docInfo = documentPool [ uri ] ;
501+ // re-create LanguageServer if document changed
502+ if ( ! docInfo . service || docInfo . changed ) {
503+ docInfo . service = new LanguageServiceProvider ( docInfo . document ) ;
506504
507505 if ( supportSASGetLibList ) {
508- servicePool [ uri ] . setLibService ( ( libId , resolve ) =>
506+ docInfo . service . setLibService ( ( libId , resolve ) =>
509507 connection
510508 . sendRequest < LibCompleteItem [ ] > ( "sas/getLibList" , { libId : libId } )
511509 . then ( resolve ) ,
512510 ) ;
513511 }
514512 }
515- return servicePool [ uri ] ;
513+ return docInfo . service ! ;
516514}
517515
518516const dispatch = async < Ret > (
@@ -522,6 +520,7 @@ const dispatch = async <Ret>(
522520 python ?: ( pyrightLanguageService : PyrightLanguageProvider ) => Promise < Ret > ;
523521 } ,
524522) => {
523+ syncIfDocChange ( params . textDocument . uri ) ;
525524 const languageService = getLanguageService ( params . textDocument . uri ) ;
526525 const codeZoneManager = languageService . getCodeZoneManager ( ) ;
527526 const pos = params . position ;
@@ -567,3 +566,18 @@ const isRangeIncluded = (a: Range, b: Range) => {
567566 }
568567 return false ;
569568} ;
569+
570+ const syncIfDocChange = ( uri : string ) => {
571+ const docInfo = documentPool [ uri ] ;
572+ if ( ! docInfo . changed ) {
573+ return ;
574+ }
575+ docInfo . changed = false ;
576+ _pyrightLanguageProvider . addContentChange ( docInfo . document ) ;
577+ } ;
578+
579+ const syncAllChangedDoc = ( ) => {
580+ for ( const uri in documentPool ) {
581+ syncIfDocChange ( uri ) ;
582+ }
583+ } ;
0 commit comments