11using System ;
22using System . Collections . Generic ;
33using System . Linq ;
4+ using System . Reactive ;
45using System . Reactive . Disposables ;
56using System . Reactive . Linq ;
67using System . Reactive . Threading . Tasks ;
@@ -51,14 +52,14 @@ internal static void InitHandlers(ILanguageServer client, CompositeDisposable re
5152 }
5253
5354 internal static IDisposable RegisterHandlers (
54- Task initializeComplete ,
55+ IObservable < Unit > initializeComplete ,
5556 IClientLanguageServer client ,
5657 IServerWorkDoneManager serverWorkDoneManager ,
5758 ISupportedCapabilities supportedCapabilities ,
5859 IEnumerable < ILspHandlerDescriptor > collection
5960 )
6061 {
61- var registrations = new List < Registration > ( ) ;
62+ var descriptors = new List < ILspHandlerDescriptor > ( ) ;
6263 foreach ( var descriptor in collection )
6364 {
6465 if ( descriptor is LspHandlerDescriptor lspHandlerDescriptor &&
@@ -68,51 +69,77 @@ IEnumerable<ILspHandlerDescriptor> collection
6869 continue ;
6970 }
7071
71- if ( descriptor . HasCapability && supportedCapabilities . AllowsDynamicRegistration ( descriptor . CapabilityType ) )
72- {
73- if ( descriptor . RegistrationOptions is IWorkDoneProgressOptions wdpo )
74- {
75- wdpo . WorkDoneProgress = serverWorkDoneManager . IsSupported ;
76- }
77-
78- registrations . Add (
79- new Registration {
80- Id = descriptor . Id . ToString ( ) ,
81- Method = descriptor . Method ,
82- RegisterOptions = descriptor . RegistrationOptions
83- }
84- ) ;
85- }
72+ descriptors . Add ( descriptor ) ;
8673 }
8774
88- // Fire and forget
89- DynamicallyRegisterHandlers ( client , initializeComplete , registrations . ToArray ( ) ) . ToObservable ( ) . Subscribe ( ) ;
90-
91- return Disposable . Create (
92- ( ) => {
93- client . UnregisterCapability (
94- new UnregistrationParams {
95- Unregisterations = registrations . ToArray ( )
96- }
97- ) . ToObservable ( ) . Subscribe ( ) ;
98- }
99- ) ;
75+ return DynamicallyRegisterHandlers ( client , initializeComplete , serverWorkDoneManager , supportedCapabilities , descriptors ) ;
10076 }
10177
102- internal static async Task DynamicallyRegisterHandlers ( IClientLanguageServer client , Task initializeComplete , Registration [ ] registrations )
78+ internal static IDisposable DynamicallyRegisterHandlers (
79+ IClientLanguageServer client ,
80+ IObservable < Unit > initializeComplete ,
81+ IServerWorkDoneManager serverWorkDoneManager ,
82+ ISupportedCapabilities supportedCapabilities ,
83+ IReadOnlyList < ILspHandlerDescriptor > descriptors
84+ )
10385 {
104- if ( registrations . Length == 0 )
105- return ; // No dynamic registrations supported by client.
86+ if ( descriptors . Count == 0 )
87+ return Disposable . Empty ; // No dynamic registrations supported by client.
88+
89+ var disposable = new CompositeDisposable ( ) ;
10690
107- var @params = new RegistrationParams { Registrations = registrations } ;
91+ var result = initializeComplete
92+ . LastOrDefaultAsync ( )
93+ . Select (
94+ _ => {
95+ var registrations = new List < Registration > ( ) ;
96+ foreach ( var descriptor in descriptors )
97+ {
98+ if ( descriptor . HasCapability && supportedCapabilities . AllowsDynamicRegistration ( descriptor . CapabilityType ) )
99+ {
100+ if ( descriptor . RegistrationOptions is IWorkDoneProgressOptions wdpo )
101+ {
102+ wdpo . WorkDoneProgress = serverWorkDoneManager . IsSupported ;
103+ }
108104
109- await initializeComplete ;
105+ registrations . Add (
106+ new Registration {
107+ Id = descriptor . Id . ToString ( ) ,
108+ Method = descriptor . Method ,
109+ RegisterOptions = descriptor . RegistrationOptions
110+ }
111+ ) ;
112+ }
113+ }
110114
111- await client . RegisterCapability ( @params ) ;
115+ return registrations ;
116+ }
117+ )
118+ . SelectMany (
119+ registrations => Observable . FromAsync ( ct => client . RegisterCapability ( new RegistrationParams { Registrations = registrations } , ct ) ) , ( a , b ) => a
120+ )
121+ . Aggregate ( ( z , b ) => z )
122+ . Subscribe (
123+ registrations => {
124+ disposable . Add (
125+ Disposable . Create (
126+ ( ) => {
127+ client . UnregisterCapability (
128+ new UnregistrationParams {
129+ Unregisterations = registrations
130+ }
131+ ) . ToObservable ( ) . Subscribe ( ) ;
132+ }
133+ )
134+ ) ;
135+ }
136+ ) ;
137+ disposable . Add ( result ) ;
138+ return disposable ;
112139 }
113140
114141 internal static IDisposable RegisterHandlers (
115- Task initializeComplete ,
142+ IObservable < Unit > initializeComplete ,
116143 IClientLanguageServer client ,
117144 IServerWorkDoneManager serverWorkDoneManager ,
118145 ISupportedCapabilities supportedCapabilities ,
@@ -145,4 +172,4 @@ IDisposable handlerDisposable
145172 return cd ;
146173 }
147174 }
148- }
175+ }
0 commit comments