1
1
using System ;
2
2
using System . Collections . Generic ;
3
3
using System . Linq ;
4
+ using System . Reactive ;
4
5
using System . Reactive . Disposables ;
5
6
using System . Reactive . Linq ;
6
7
using System . Reactive . Threading . Tasks ;
@@ -51,14 +52,14 @@ internal static void InitHandlers(ILanguageServer client, CompositeDisposable re
51
52
}
52
53
53
54
internal static IDisposable RegisterHandlers (
54
- Task initializeComplete ,
55
+ IObservable < Unit > initializeComplete ,
55
56
IClientLanguageServer client ,
56
57
IServerWorkDoneManager serverWorkDoneManager ,
57
58
ISupportedCapabilities supportedCapabilities ,
58
59
IEnumerable < ILspHandlerDescriptor > collection
59
60
)
60
61
{
61
- var registrations = new List < Registration > ( ) ;
62
+ var descriptors = new List < ILspHandlerDescriptor > ( ) ;
62
63
foreach ( var descriptor in collection )
63
64
{
64
65
if ( descriptor is LspHandlerDescriptor lspHandlerDescriptor &&
@@ -68,51 +69,77 @@ IEnumerable<ILspHandlerDescriptor> collection
68
69
continue ;
69
70
}
70
71
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 ) ;
86
73
}
87
74
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 ) ;
100
76
}
101
77
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
+ )
103
85
{
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 ( ) ;
106
90
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
+ }
108
104
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
+ }
110
114
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 ;
112
139
}
113
140
114
141
internal static IDisposable RegisterHandlers (
115
- Task initializeComplete ,
142
+ IObservable < Unit > initializeComplete ,
116
143
IClientLanguageServer client ,
117
144
IServerWorkDoneManager serverWorkDoneManager ,
118
145
ISupportedCapabilities supportedCapabilities ,
@@ -145,4 +172,4 @@ IDisposable handlerDisposable
145
172
return cd ;
146
173
}
147
174
}
148
- }
175
+ }
0 commit comments