@@ -17,7 +17,6 @@ import {
1717 RenameParams ,
1818 ResultProgressReporter ,
1919 SemanticTokensRequest ,
20- SignatureHelpParams ,
2120 TextDocumentPositionParams ,
2221 TextDocumentSyncKind ,
2322 WorkDoneProgressReporter ,
@@ -37,8 +36,13 @@ import { CompletionProvider } from "./sas/CompletionProvider";
3736import { LanguageServiceProvider , legend } from "./sas/LanguageServiceProvider" ;
3837import type { LibCompleteItem } from "./sas/SyntaxDataProvider" ;
3938
40- const servicePool : Record < string , LanguageServiceProvider > = { } ;
41- const documentPool : Record < string , TextDocument > = { } ;
39+ interface DocumentInfo {
40+ document : TextDocument ;
41+ changed : boolean ;
42+ service ?: LanguageServiceProvider ;
43+ }
44+
45+ const documentPool : Record < string , DocumentInfo > = { } ;
4246
4347let completionProvider : CompletionProvider ;
4448let connection : Connection ;
@@ -116,6 +120,7 @@ export const init = (conn: Connection): void => {
116120 connection . onInitialized ( ( ) => _pyrightLanguageProvider . onInitialized ( ) ) ;
117121
118122 connection . onRequest ( SemanticTokensRequest . type , ( params ) => {
123+ syncIfDocChange ( params . textDocument . uri ) ;
119124 const languageService = getLanguageService ( params . textDocument . uri ) ;
120125
121126 return { data : languageService . getTokens ( ) } ;
@@ -188,6 +193,7 @@ export const init = (conn: Connection): void => {
188193 } ) ;
189194
190195 connection . onDocumentSymbol ( async ( params , token ) => {
196+ syncIfDocChange ( params . textDocument . uri ) ;
191197 const languageService = getLanguageService ( params . textDocument . uri ) ;
192198 const sasSymbols = languageService . getDocumentSymbols ( ) ;
193199 const pythonSymbols =
@@ -209,6 +215,7 @@ export const init = (conn: Connection): void => {
209215
210216 // todo
211217 connection . onFoldingRanges ( ( params ) => {
218+ syncIfDocChange ( params . textDocument . uri ) ;
212219 const languageService = getLanguageService ( params . textDocument . uri ) ;
213220 return languageService . getFoldingRanges ( ) ;
214221 } ) ;
@@ -267,6 +274,7 @@ export const init = (conn: Connection): void => {
267274 } ) ;
268275
269276 connection . onDocumentFormatting ( ( params ) => {
277+ syncIfDocChange ( params . textDocument . uri ) ;
270278 const languageService = getLanguageService ( params . textDocument . uri ) ;
271279 return languageService . formatter . format ( {
272280 tabWidth : params . options . tabSize ,
@@ -281,27 +289,25 @@ export const init = (conn: Connection): void => {
281289 params . textDocument . version ,
282290 params . textDocument . text ,
283291 ) ;
284- documentPool [ doc . uri ] = doc ;
292+ documentPool [ doc . uri ] = { document : doc , changed : false } ;
285293 await _pyrightLanguageProvider . onDidOpenTextDocument ( params ) ;
286294 } ) ;
287295
288296 connection . onDidCloseTextDocument ( async ( params ) => {
289297 const uri = params . textDocument . uri ;
290298 delete documentPool [ uri ] ;
291- delete servicePool [ uri ] ;
292299 await _pyrightLanguageProvider . onDidCloseTextDocument ( params ) ;
293300 } ) ;
294301
295302 connection . onDidChangeTextDocument ( ( params ) => {
296303 const uri = params . textDocument . uri ;
297- const doc = documentPool [ uri ] ;
304+ const docInfo = documentPool [ uri ] ;
298305 TextDocument . update (
299- doc ,
306+ docInfo . document ,
300307 params . contentChanges ,
301308 params . textDocument . version ,
302309 ) ;
303- delete servicePool [ uri ] ;
304- _pyrightLanguageProvider . addContentChange ( doc ) ;
310+ docInfo . changed = true ;
305311 } ) ;
306312
307313 connection . onDidChangeConfiguration (
@@ -378,6 +384,7 @@ export const init = (conn: Connection): void => {
378384 workDoneProgress ,
379385 resultProgress ,
380386 ) => {
387+ syncAllChangedDoc ( ) ;
381388 return await _pyrightLanguageProvider . onWorkspaceSymbol (
382389 params ,
383390 token ,
@@ -399,21 +406,6 @@ export const init = (conn: Connection): void => {
399406 } ,
400407 ) ;
401408
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-
417409 connection . onPrepareRename (
418410 async ( params : PrepareRenameParams , token : CancellationToken ) => {
419411 return await dispatch ( params , {
@@ -444,6 +436,9 @@ export const init = (conn: Connection): void => {
444436
445437 connection . onDidChangeWatchedFiles (
446438 async ( params : DidChangeWatchedFilesParams ) => {
439+ params . changes . forEach ( ( item ) => {
440+ syncIfDocChange ( item . uri ) ;
441+ } ) ;
447442 _pyrightLanguageProvider . onDidChangeWatchedFiles ( params ) ;
448443 } ,
449444 ) ;
@@ -454,6 +449,7 @@ export const init = (conn: Connection): void => {
454449 token : CancellationToken ,
455450 reporter : WorkDoneProgressReporter ,
456451 ) => {
452+ syncAllChangedDoc ( ) ;
457453 return await _pyrightLanguageProvider . onExecuteCommand (
458454 params ,
459455 token ,
@@ -500,19 +496,20 @@ export const init = (conn: Connection): void => {
500496} ;
501497
502498function getLanguageService ( uri : string ) {
503- if ( ! servicePool [ uri ] ) {
504- // re-create LanguageServer if document changed
505- servicePool [ uri ] = new LanguageServiceProvider ( documentPool [ uri ] ) ;
499+ const docInfo = documentPool [ uri ] ;
500+ // re-create LanguageServer if document changed
501+ if ( ! docInfo . service || docInfo . changed ) {
502+ docInfo . service = new LanguageServiceProvider ( docInfo . document ) ;
506503
507504 if ( supportSASGetLibList ) {
508- servicePool [ uri ] . setLibService ( ( libId , resolve ) =>
505+ docInfo . service . setLibService ( ( libId , resolve ) =>
509506 connection
510507 . sendRequest < LibCompleteItem [ ] > ( "sas/getLibList" , { libId : libId } )
511508 . then ( resolve ) ,
512509 ) ;
513510 }
514511 }
515- return servicePool [ uri ] ;
512+ return docInfo . service ! ;
516513}
517514
518515const dispatch = async < Ret > (
@@ -522,6 +519,7 @@ const dispatch = async <Ret>(
522519 python ?: ( pyrightLanguageService : PyrightLanguageProvider ) => Promise < Ret > ;
523520 } ,
524521) => {
522+ syncIfDocChange ( params . textDocument . uri ) ;
525523 const languageService = getLanguageService ( params . textDocument . uri ) ;
526524 const codeZoneManager = languageService . getCodeZoneManager ( ) ;
527525 const pos = params . position ;
@@ -567,3 +565,18 @@ const isRangeIncluded = (a: Range, b: Range) => {
567565 }
568566 return false ;
569567} ;
568+
569+ const syncIfDocChange = ( uri : string ) => {
570+ const docInfo = documentPool [ uri ] ;
571+ if ( ! docInfo . changed ) {
572+ return ;
573+ }
574+ docInfo . changed = false ;
575+ _pyrightLanguageProvider . addContentChange ( docInfo . document ) ;
576+ } ;
577+
578+ const syncAllChangedDoc = ( ) => {
579+ for ( const uri in documentPool ) {
580+ syncIfDocChange ( uri ) ;
581+ }
582+ } ;
0 commit comments