@@ -1611,7 +1611,7 @@ function getContentDocumentSelectorFromConfigFile(
16111611}
16121612
16131613class TW {
1614- private initialized = false
1614+ private initPromise : Promise < void >
16151615 private lspHandlersAdded = false
16161616 private workspaces : Map < string , { name : string ; workspaceFsPath : string } >
16171617 private projects : Map < string , ProjectService >
@@ -1631,12 +1631,15 @@ class TW {
16311631 }
16321632
16331633 async init ( ) : Promise < void > {
1634- if ( this . initialized ) return
1634+ if ( ! this . initPromise ) {
1635+ this . initPromise = this . _init ( )
1636+ }
1637+ await this . initPromise
1638+ }
16351639
1640+ private async _init ( ) : Promise < void > {
16361641 clearRequireCache ( )
16371642
1638- this . initialized = true
1639-
16401643 let base : string
16411644 if ( this . initializeParams . rootUri ) {
16421645 base = URI . parse ( this . initializeParams . rootUri ) . fsPath
@@ -2131,7 +2134,7 @@ class TW {
21312134 }
21322135 }
21332136
2134- private setupLSPHandlers ( ) {
2137+ setupLSPHandlers ( ) {
21352138 if ( this . lspHandlersAdded ) {
21362139 return
21372140 }
@@ -2147,6 +2150,10 @@ class TW {
21472150 }
21482151
21492152 private updateCapabilities ( ) {
2153+ if ( ! supportsDynamicRegistration ( this . initializeParams ) ) {
2154+ return
2155+ }
2156+
21502157 if ( this . registrations ) {
21512158 this . registrations . then ( ( r ) => r . dispose ( ) )
21522159 }
@@ -2221,30 +2228,37 @@ class TW {
22212228 }
22222229
22232230 async onDocumentColor ( params : DocumentColorParams ) : Promise < ColorInformation [ ] > {
2231+ await this . init ( )
22242232 return this . getProject ( params . textDocument ) ?. onDocumentColor ( params ) ?? [ ]
22252233 }
22262234
22272235 async onColorPresentation ( params : ColorPresentationParams ) : Promise < ColorPresentation [ ] > {
2236+ await this . init ( )
22282237 return this . getProject ( params . textDocument ) ?. onColorPresentation ( params ) ?? [ ]
22292238 }
22302239
22312240 async onHover ( params : TextDocumentPositionParams ) : Promise < Hover > {
2241+ await this . init ( )
22322242 return this . getProject ( params . textDocument ) ?. onHover ( params ) ?? null
22332243 }
22342244
22352245 async onCompletion ( params : CompletionParams ) : Promise < CompletionList > {
2246+ await this . init ( )
22362247 return this . getProject ( params . textDocument ) ?. onCompletion ( params ) ?? null
22372248 }
22382249
22392250 async onCompletionResolve ( item : CompletionItem ) : Promise < CompletionItem > {
2251+ await this . init ( )
22402252 return this . projects . get ( item . data ?. _projectKey ) ?. onCompletionResolve ( item ) ?? null
22412253 }
22422254
2243- onCodeAction ( params : CodeActionParams ) : Promise < CodeAction [ ] > {
2255+ async onCodeAction ( params : CodeActionParams ) : Promise < CodeAction [ ] > {
2256+ await this . init ( )
22442257 return this . getProject ( params . textDocument ) ?. onCodeAction ( params ) ?? null
22452258 }
22462259
2247- onDocumentLinks ( params : DocumentLinkParams ) : DocumentLink [ ] {
2260+ async onDocumentLinks ( params : DocumentLinkParams ) : Promise < DocumentLink [ ] > {
2261+ await this . init ( )
22482262 return this . getProject ( params . textDocument ) ?. onDocumentLinks ( params ) ?? null
22492263 }
22502264
@@ -2274,7 +2288,7 @@ class TW {
22742288 restart ( ) : void {
22752289 console . log ( '----------\nRESTARTING\n----------' )
22762290 this . dispose ( )
2277- this . initialized = false
2291+ this . initPromise = undefined
22782292 this . init ( )
22792293 }
22802294}
@@ -2306,9 +2320,8 @@ class DocumentService {
23062320 }
23072321}
23082322
2309- function supportsDynamicRegistration ( connection : Connection , params : InitializeParams ) : boolean {
2323+ function supportsDynamicRegistration ( params : InitializeParams ) : boolean {
23102324 return (
2311- connection . onInitialized &&
23122325 params . capabilities . textDocument . hover ?. dynamicRegistration &&
23132326 params . capabilities . textDocument . colorProvider ?. dynamicRegistration &&
23142327 params . capabilities . textDocument . codeAction ?. dynamicRegistration &&
@@ -2322,15 +2335,15 @@ const tw = new TW(connection)
23222335connection . onInitialize ( async ( params : InitializeParams ) : Promise < InitializeResult > => {
23232336 tw . initializeParams = params
23242337
2325- if ( supportsDynamicRegistration ( connection , params ) ) {
2338+ if ( supportsDynamicRegistration ( params ) ) {
23262339 return {
23272340 capabilities : {
23282341 textDocumentSync : TextDocumentSyncKind . Full ,
23292342 } ,
23302343 }
23312344 }
23322345
2333- await tw . init ( )
2346+ tw . setupLSPHandlers ( )
23342347
23352348 return {
23362349 capabilities : {
0 commit comments