@@ -52,6 +52,7 @@ public class LanguageServer : ILanguageServer, IInitializeHandler, IInitializedH
5252 private readonly CompositeDisposable _disposable = new CompositeDisposable ( ) ;
5353 private readonly IServiceProvider _serviceProvider ;
5454 private readonly SupportedCapabilities _supportedCapabilities ;
55+ private Task _initializingTask ;
5556
5657 public static Task < ILanguageServer > From ( Action < LanguageServerOptions > optionsAction )
5758 {
@@ -301,38 +302,26 @@ public IDisposable AddTextDocumentIdentifier<T>() where T : ITextDocumentIdentif
301302 return _textDocumentIdentifiers . Add ( ActivatorUtilities . CreateInstance < T > ( _serviceProvider ) ) ;
302303 }
303304
304- private IDisposable RegisterHandlers ( LspHandlerDescriptorDisposable handlerDisposable )
305+ public async Task Initialize ( CancellationToken token )
305306 {
306- using ( var scope = _serviceProvider . CreateScope ( ) )
307+ if ( _initializingTask != null )
307308 {
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+ }
319317
320- return new ImmutableDisposable (
321- handlerDisposable ,
322- Disposable . Create ( ( ) => {
323- Client . UnregisterCapability ( new UnregistrationParams ( ) {
324- Unregisterations = registrations . ToArray ( )
325- } ) . ToObservable ( ) . Subscribe ( ) ;
326- } ) ) ;
318+ return ;
327319 }
328- }
329320
330- private async Task Initialize ( CancellationToken token )
331- {
332- _connection . Open ( ) ;
333321 try
334322 {
335- await _initializeComplete
323+ _connection . Open ( ) ;
324+ _initializingTask = _initializeComplete
336325 . Select ( result => _startedDelegates . Select ( @delegate =>
337326 Observable . FromAsync ( ( ) => @delegate ( result ) )
338327 )
@@ -343,6 +332,8 @@ await _initializeComplete
343332 . Merge ( )
344333 . LastAsync ( )
345334 . ToTask ( token ) ;
335+
336+ await _initializingTask ;
346337 }
347338 catch ( TaskCanceledException e )
348339 {
@@ -354,6 +345,35 @@ await _initializeComplete
354345 }
355346 }
356347
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+
357377 async Task < InitializeResult > IRequestHandler < InitializeParams , InitializeResult > . Handle ( InitializeParams request , CancellationToken token )
358378 {
359379 ClientSettings = request ;
@@ -408,7 +428,8 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
408428
409429 var ccp = new ClientCapabilityProvider ( _collection ) ;
410430
411- var serverCapabilities = new ServerCapabilities ( ) {
431+ var serverCapabilities = new ServerCapabilities ( )
432+ {
412433 CodeActionProvider = ccp . GetStaticOptions ( textDocumentCapabilities . CodeAction ) . Get < ICodeActionOptions , CodeActionOptions > ( CodeActionOptions . Of ) ,
413434 CodeLensProvider = ccp . GetStaticOptions ( textDocumentCapabilities . CodeLens ) . Get < ICodeLensOptions , CodeLensOptions > ( CodeLensOptions . Of ) ,
414435 CompletionProvider = ccp . GetStaticOptions ( textDocumentCapabilities . Completion ) . Get < ICompletionOptions , CompletionOptions > ( CompletionOptions . Of ) ,
@@ -435,8 +456,10 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
435456
436457 if ( _collection . ContainsHandler ( typeof ( IDidChangeWorkspaceFoldersHandler ) ) )
437458 {
438- serverCapabilities . Workspace = new WorkspaceServerCapabilities ( ) {
439- WorkspaceFolders = new WorkspaceFolderOptions ( ) {
459+ serverCapabilities . Workspace = new WorkspaceServerCapabilities ( )
460+ {
461+ WorkspaceFolders = new WorkspaceFolderOptions ( )
462+ {
440463 Supported = true ,
441464 ChangeNotifications = Guid . NewGuid ( ) . ToString ( )
442465 }
@@ -466,7 +489,8 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
466489 }
467490 else
468491 {
469- serverCapabilities . TextDocumentSync = new TextDocumentSyncOptions ( ) {
492+ serverCapabilities . TextDocumentSync = new TextDocumentSyncOptions ( )
493+ {
470494 Change = textDocumentSyncKind ,
471495 OpenClose = _collection . ContainsHandler ( typeof ( IDidOpenTextDocumentHandler ) ) || _collection . ContainsHandler ( typeof ( IDidCloseTextDocumentHandler ) ) ,
472496 Save = _collection . ContainsHandler ( typeof ( IDidSaveTextDocumentHandler ) ) ?
0 commit comments