@@ -63,7 +63,6 @@ import {
63
63
ConfigEmptyError ,
64
64
ConfigInvalidError ,
65
65
ConfigNotFoundError ,
66
- GraphQLExtensionDeclaration ,
67
66
LoaderNoResultError ,
68
67
ProjectNotFoundError ,
69
68
} from 'graphql-config' ;
@@ -88,24 +87,22 @@ function toPosition(position: VscodePosition): IPosition {
88
87
}
89
88
90
89
export class MessageProcessor {
91
- _connection : Connection ;
92
- _graphQLCache ! : GraphQLCache ;
93
- _graphQLConfig : GraphQLConfig | undefined ;
94
- _languageService ! : GraphQLLanguageService ;
95
- _textDocumentCache = new Map < string , CachedDocumentType > ( ) ;
96
- _isInitialized = false ;
97
- _isGraphQLConfigMissing : boolean | null = null ;
98
- _willShutdown = false ;
99
- _logger : Logger | NoopLogger ;
100
- _extensions ?: GraphQLExtensionDeclaration [ ] ;
101
- _parser : ( text : string , uri : string ) => CachedContent [ ] ;
102
- _tmpDir : string ;
103
- _tmpUriBase : string ;
104
- _tmpDirBase : string ;
105
- _loadConfigOptions : LoadConfigOptions ;
106
- _schemaCacheInit = false ;
107
- _rootPath : string = process . cwd ( ) ;
108
- _settings : any ;
90
+ private _connection : Connection ;
91
+ private _graphQLCache ! : GraphQLCache ;
92
+ private _languageService ! : GraphQLLanguageService ;
93
+ private _textDocumentCache = new Map < string , CachedDocumentType > ( ) ;
94
+ private _isInitialized = false ;
95
+ private _isGraphQLConfigMissing : boolean | null = null ;
96
+ private _willShutdown = false ;
97
+ private _logger : Logger | NoopLogger ;
98
+ private _parser : ( text : string , uri : string ) => CachedContent [ ] ;
99
+ private _tmpDir : string ;
100
+ private _tmpUriBase : string ;
101
+ private _tmpDirBase : string ;
102
+ private _loadConfigOptions : LoadConfigOptions ;
103
+ private _schemaCacheInit = false ;
104
+ private _rootPath : string = process . cwd ( ) ;
105
+ private _settings : any ;
109
106
110
107
constructor ( {
111
108
logger,
@@ -128,7 +125,6 @@ export class MessageProcessor {
128
125
} ) {
129
126
this . _connection = connection ;
130
127
this . _logger = logger ;
131
- this . _graphQLConfig = config ;
132
128
this . _parser = ( text , uri ) => {
133
129
const p = parser ?? parseDocument ;
134
130
return p ( text , uri , fileExtensions , graphqlFileExtensions , this . _logger ) ;
@@ -138,12 +134,6 @@ export class MessageProcessor {
138
134
this . _tmpUriBase = URI . file ( this . _tmpDirBase ) . toString ( ) ;
139
135
// use legacy mode by default for backwards compatibility
140
136
this . _loadConfigOptions = { legacy : true , ...loadConfigOptions } ;
141
- if (
142
- loadConfigOptions . extensions &&
143
- loadConfigOptions . extensions ?. length > 0
144
- ) {
145
- this . _extensions = loadConfigOptions . extensions ;
146
- }
147
137
148
138
if ( ! existsSync ( this . _tmpDirBase ) ) {
149
139
void mkdirSync ( this . _tmpDirBase ) ;
@@ -156,7 +146,7 @@ export class MessageProcessor {
156
146
this . _connection = connection ;
157
147
}
158
148
159
- async handleInitializeRequest (
149
+ public async handleInitializeRequest (
160
150
params : InitializeParams ,
161
151
_token ?: CancellationToken ,
162
152
configDir ?: string ,
@@ -244,7 +234,6 @@ export class MessageProcessor {
244
234
) ;
245
235
const config = this . _graphQLCache . getGraphQLConfig ( ) ;
246
236
if ( config ) {
247
- this . _graphQLConfig = config ;
248
237
await this . _cacheAllProjectFiles ( config ) ;
249
238
this . _isInitialized = true ;
250
239
this . _isGraphQLConfigMissing = false ;
@@ -312,29 +301,44 @@ export class MessageProcessor {
312
301
}
313
302
return false ;
314
303
}
315
-
316
- async handleDidOpenOrSaveNotification (
317
- params : DidSaveTextDocumentParams | DidOpenTextDocumentParams ,
318
- ) : Promise < PublishDiagnosticsParams > {
319
- /**
320
- * Initialize the LSP server when the first file is opened or saved,
321
- * so that we can access the user settings for config rootDir, etc
322
- */
323
- const isGraphQLConfigFile = await this . _isGraphQLConfigFile (
324
- params . textDocument . uri ,
325
- ) ;
304
+ private async _loadConfigOrSkip ( uri : string ) {
326
305
try {
306
+ const isGraphQLConfigFile = await this . _isGraphQLConfigFile ( uri ) ;
307
+
327
308
if ( ! this . _isInitialized ) {
328
- // don't try to initialize again if we've already tried
329
- // and the graphql config file or package.json entry isn't even there
330
309
if ( this . _isGraphQLConfigMissing === true && ! isGraphQLConfigFile ) {
331
- return { uri : params . textDocument . uri , diagnostics : [ ] } ;
310
+ return true ;
332
311
}
333
- // then initial call to update graphql config
312
+ // don't try to initialize again if we've already tried
313
+ // and the graphql config file or package.json entry isn't even there
314
+ await this . _updateGraphQLConfig ( ) ;
315
+ return isGraphQLConfigFile ;
316
+ }
317
+ // if it has initialized, but this is another config file change, then let's handle it
318
+ if ( isGraphQLConfigFile ) {
334
319
await this . _updateGraphQLConfig ( ) ;
335
320
}
321
+ return isGraphQLConfigFile ;
336
322
} catch ( err ) {
337
323
this . _logger . error ( String ( err ) ) ;
324
+ // return true if it's a graphql config file so we don't treat
325
+ // this as a non-config file if it is one
326
+ return true ;
327
+ }
328
+ }
329
+
330
+ public async handleDidOpenOrSaveNotification (
331
+ params : DidSaveTextDocumentParams | DidOpenTextDocumentParams ,
332
+ ) : Promise < PublishDiagnosticsParams > {
333
+ /**
334
+ * Initialize the LSP server when the first file is opened or saved,
335
+ * so that we can access the user settings for config rootDir, etc
336
+ */
337
+ const shouldSkip = await this . _loadConfigOrSkip ( params . textDocument . uri ) ;
338
+ // if we're loading config or the config is missing or there's an error
339
+ // don't do anything else
340
+ if ( shouldSkip ) {
341
+ return { uri : params . textDocument . uri , diagnostics : [ ] } ;
338
342
}
339
343
340
344
// Here, we set the workspace settings in memory,
@@ -361,20 +365,13 @@ export class MessageProcessor {
361
365
contents = this . _parser ( text , uri ) ;
362
366
363
367
await this . _invalidateCache ( textDocument , uri , contents ) ;
364
- } else if ( isGraphQLConfigFile ) {
365
- this . _logger . info ( 'updating graphql config' ) ;
366
- await this . _updateGraphQLConfig ( ) ;
367
- return { uri, diagnostics : [ ] } ;
368
368
}
369
369
if ( ! this . _graphQLCache ) {
370
370
return { uri, diagnostics } ;
371
371
}
372
372
try {
373
373
const project = this . _graphQLCache . getProjectForFile ( uri ) ;
374
- if (
375
- this . _isInitialized &&
376
- project ?. extensions ?. languageService ?. enableValidation !== false
377
- ) {
374
+ if ( project ?. extensions ?. languageService ?. enableValidation !== false ) {
378
375
await Promise . all (
379
376
contents . map ( async ( { query, range } ) => {
380
377
const results = await this . _languageService . getDiagnostics (
@@ -406,7 +403,7 @@ export class MessageProcessor {
406
403
return { uri, diagnostics } ;
407
404
}
408
405
409
- async handleDidChangeNotification (
406
+ public async handleDidChangeNotification (
410
407
params : DidChangeTextDocumentParams ,
411
408
) : Promise < PublishDiagnosticsParams | null > {
412
409
if (
@@ -497,7 +494,7 @@ export class MessageProcessor {
497
494
return { } ;
498
495
}
499
496
500
- handleDidCloseNotification ( params : DidCloseTextDocumentParams ) : void {
497
+ public handleDidCloseNotification ( params : DidCloseTextDocumentParams ) : void {
501
498
if ( ! this . _isInitialized ) {
502
499
return ;
503
500
}
@@ -525,11 +522,11 @@ export class MessageProcessor {
525
522
) ;
526
523
}
527
524
528
- handleShutdownRequest ( ) : void {
525
+ public handleShutdownRequest ( ) : void {
529
526
this . _willShutdown = true ;
530
527
}
531
528
532
- handleExitNotification ( ) : void {
529
+ public handleExitNotification ( ) : void {
533
530
process . exit ( this . _willShutdown ? 0 : 1 ) ;
534
531
}
535
532
@@ -541,7 +538,7 @@ export class MessageProcessor {
541
538
}
542
539
}
543
540
544
- async handleCompletionRequest (
541
+ public async handleCompletionRequest (
545
542
params : CompletionParams ,
546
543
) : Promise < CompletionList > {
547
544
if ( ! this . _isInitialized ) {
@@ -599,7 +596,9 @@ export class MessageProcessor {
599
596
return { items : result , isIncomplete : false } ;
600
597
}
601
598
602
- async handleHoverRequest ( params : TextDocumentPositionParams ) : Promise < Hover > {
599
+ public async handleHoverRequest (
600
+ params : TextDocumentPositionParams ,
601
+ ) : Promise < Hover > {
603
602
if ( ! this . _isInitialized ) {
604
603
return { contents : [ ] } ;
605
604
}
@@ -642,7 +641,7 @@ export class MessageProcessor {
642
641
} ;
643
642
}
644
643
645
- async handleWatchedFilesChangedNotification (
644
+ public async handleWatchedFilesChangedNotification (
646
645
params : DidChangeWatchedFilesParams ,
647
646
) : Promise < Array < PublishDiagnosticsParams | undefined > | null > {
648
647
if (
@@ -655,13 +654,9 @@ export class MessageProcessor {
655
654
656
655
return Promise . all (
657
656
params . changes . map ( async ( change : FileEvent ) => {
658
- if (
659
- this . _isGraphQLConfigMissing ||
660
- ! this . _isInitialized ||
661
- ! this . _graphQLCache
662
- ) {
663
- this . _logger . warn ( 'No cache available for handleWatchedFilesChanged' ) ;
664
- return ;
657
+ const shouldSkip = await this . _loadConfigOrSkip ( change . uri ) ;
658
+ if ( shouldSkip ) {
659
+ return { uri : change . uri , diagnostics : [ ] } ;
665
660
}
666
661
if (
667
662
change . type === FileChangeTypeKind . Created ||
@@ -731,7 +726,7 @@ export class MessageProcessor {
731
726
) ;
732
727
}
733
728
734
- async handleDefinitionRequest (
729
+ public async handleDefinitionRequest (
735
730
params : TextDocumentPositionParams ,
736
731
_token ?: CancellationToken ,
737
732
) : Promise < Array < Location > > {
@@ -836,7 +831,7 @@ export class MessageProcessor {
836
831
return formatted ;
837
832
}
838
833
839
- async handleDocumentSymbolRequest (
834
+ public async handleDocumentSymbolRequest (
840
835
params : DocumentSymbolParams ,
841
836
) : Promise < Array < SymbolInformation > > {
842
837
if ( ! this . _isInitialized ) {
@@ -896,7 +891,7 @@ export class MessageProcessor {
896
891
// );
897
892
// }
898
893
899
- async handleWorkspaceSymbolRequest (
894
+ public async handleWorkspaceSymbolRequest (
900
895
params : WorkspaceSymbolParams ,
901
896
) : Promise < Array < SymbolInformation > > {
902
897
if ( ! this . _isInitialized ) {
0 commit comments