@@ -52,6 +52,7 @@ public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedH
52
52
private readonly CompositeDisposable _disposable = new CompositeDisposable ( ) ;
53
53
private readonly IServiceProvider _serviceProvider ;
54
54
private readonly SupportedCapabilities _supportedCapabilities ;
55
+ private Task _initializingTask ;
55
56
56
57
public static Task < ILanguageServer > From ( Action < LanguageServerOptions > optionsAction )
57
58
{
@@ -301,38 +302,26 @@ public IDisposable AddTextDocumentIdentifier<T>() where T : ITextDocumentIdentif
301
302
return _textDocumentIdentifiers . Add ( ActivatorUtilities . CreateInstance < T > ( _serviceProvider ) ) ;
302
303
}
303
304
304
- private IDisposable RegisterHandlers ( LspHandlerDescriptorDisposable handlerDisposable )
305
+ public async Task Initialize ( CancellationToken token )
305
306
{
306
- using ( var scope = _serviceProvider . CreateScope ( ) )
307
+ if ( _initializingTask != null )
307
308
{
308
- var registrations = handlerDisposable . Descriptors
309
- . Where ( d => d . AllowsDynamicRegistration )
310
- . Select ( d => new Registration ( ) {
311
- Id = d . Id . ToString ( ) ,
312
- Method = d . Method ,
313
- RegisterOptions = d . RegistrationOptions
314
- } )
315
- . ToArray ( ) ;
316
-
317
- // Fire and forget
318
- DynamicallyRegisterHandlers ( registrations ) . ToObservable ( ) . Subscribe ( ) ;
309
+ try
310
+ {
311
+ await _initializingTask ;
312
+ }
313
+ catch
314
+ {
315
+ // Swallow exceptions because the original initialization task will report errors if it fails (don't want to doubly report).
316
+ }
319
317
320
- return new ImmutableDisposable (
321
- handlerDisposable ,
322
- Disposable . Create ( ( ) => {
323
- Client . UnregisterCapability ( new UnregistrationParams ( ) {
324
- Unregisterations = registrations . ToArray ( )
325
- } ) . ToObservable ( ) . Subscribe ( ) ;
326
- } ) ) ;
318
+ return ;
327
319
}
328
- }
329
320
330
- private async Task Initialize ( CancellationToken token )
331
- {
332
- _connection . Open ( ) ;
333
321
try
334
322
{
335
- await _initializeComplete
323
+ _connection . Open ( ) ;
324
+ _initializingTask = _initializeComplete
336
325
. Select ( result => _startedDelegates . Select ( @delegate =>
337
326
Observable . FromAsync ( ( ) => @delegate ( result ) )
338
327
)
@@ -343,6 +332,8 @@ await _initializeComplete
343
332
. Merge ( )
344
333
. LastAsync ( )
345
334
. ToTask ( token ) ;
335
+
336
+ await _initializingTask ;
346
337
}
347
338
catch ( TaskCanceledException e )
348
339
{
@@ -354,6 +345,35 @@ await _initializeComplete
354
345
}
355
346
}
356
347
348
+ private IDisposable RegisterHandlers ( LspHandlerDescriptorDisposable handlerDisposable )
349
+ {
350
+ using ( var scope = _serviceProvider . CreateScope ( ) )
351
+ {
352
+ var registrations = handlerDisposable . Descriptors
353
+ . Where ( d => d . AllowsDynamicRegistration )
354
+ . Select ( d => new Registration ( )
355
+ {
356
+ Id = d . Id . ToString ( ) ,
357
+ Method = d . Method ,
358
+ RegisterOptions = d . RegistrationOptions
359
+ } )
360
+ . ToArray ( ) ;
361
+
362
+ // Fire and forget
363
+ DynamicallyRegisterHandlers ( registrations ) . ToObservable ( ) . Subscribe ( ) ;
364
+
365
+ return new ImmutableDisposable (
366
+ handlerDisposable ,
367
+ Disposable . Create ( ( ) =>
368
+ {
369
+ Client . UnregisterCapability ( new UnregistrationParams ( )
370
+ {
371
+ Unregisterations = registrations . ToArray ( )
372
+ } ) . ToObservable ( ) . Subscribe ( ) ;
373
+ } ) ) ;
374
+ }
375
+ }
376
+
357
377
async Task < InitializeResult > IRequestHandler < InitializeParams , InitializeResult > . Handle ( InitializeParams request , CancellationToken token )
358
378
{
359
379
ClientSettings = request ;
@@ -408,7 +428,8 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
408
428
409
429
var ccp = new ClientCapabilityProvider ( _collection ) ;
410
430
411
- var serverCapabilities = new ServerCapabilities ( ) {
431
+ var serverCapabilities = new ServerCapabilities ( )
432
+ {
412
433
CodeActionProvider = ccp . GetStaticOptions ( textDocumentCapabilities . CodeAction ) . Get < ICodeActionOptions , CodeActionOptions > ( CodeActionOptions . Of ) ,
413
434
CodeLensProvider = ccp . GetStaticOptions ( textDocumentCapabilities . CodeLens ) . Get < ICodeLensOptions , CodeLensOptions > ( CodeLensOptions . Of ) ,
414
435
CompletionProvider = ccp . GetStaticOptions ( textDocumentCapabilities . Completion ) . Get < ICompletionOptions , CompletionOptions > ( CompletionOptions . Of ) ,
@@ -435,8 +456,10 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
435
456
436
457
if ( _collection . ContainsHandler ( typeof ( IDidChangeWorkspaceFoldersHandler ) ) )
437
458
{
438
- serverCapabilities . Workspace = new WorkspaceServerCapabilities ( ) {
439
- WorkspaceFolders = new WorkspaceFolderOptions ( ) {
459
+ serverCapabilities . Workspace = new WorkspaceServerCapabilities ( )
460
+ {
461
+ WorkspaceFolders = new WorkspaceFolderOptions ( )
462
+ {
440
463
Supported = true ,
441
464
ChangeNotifications = Guid . NewGuid ( ) . ToString ( )
442
465
}
@@ -466,7 +489,8 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
466
489
}
467
490
else
468
491
{
469
- serverCapabilities . TextDocumentSync = new TextDocumentSyncOptions ( ) {
492
+ serverCapabilities . TextDocumentSync = new TextDocumentSyncOptions ( )
493
+ {
470
494
Change = textDocumentSyncKind ,
471
495
OpenClose = _collection . ContainsHandler ( typeof ( IDidOpenTextDocumentHandler ) ) || _collection . ContainsHandler ( typeof ( IDidCloseTextDocumentHandler ) ) ,
472
496
Save = _collection . ContainsHandler ( typeof ( IDidSaveTextDocumentHandler ) ) ?
0 commit comments