Skip to content

Commit bb2b4bd

Browse files
Added id and AllowsDynamicRegistration to descriptor, removed registration and create the registrations on demand as needed
1 parent a52197f commit bb2b4bd

12 files changed

+40
-56
lines changed

src/JsonRpc/RequestRouterBase.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task RouteNotification(TDescriptor descriptor, Notification notific
6565
@params = notification.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
6666
}
6767

68-
await HandleNotification(mediator, descriptor, @params ?? EmptyRequest.Instance, token);
68+
await HandleNotification(mediator, descriptor, @params ?? Activator.CreateInstance(descriptor.Params), token);
6969
}
7070
}
7171
catch (Exception e)

src/Server/Abstractions/ILspHandlerDescriptor.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server.Abstractions
66
{
77
public interface ILspHandlerDescriptor : IHandlerDescriptor
88
{
9+
Guid Id { get; }
910
bool HasRegistration { get; }
1011
Type RegistrationType { get; }
11-
object RegisterOptions { get; }
12-
Registration Registration { get; }
12+
object RegistrationOptions { get; }
13+
bool AllowsDynamicRegistration { get; }
1314

1415
bool HasCapability { get; }
1516
Type CapabilityType { get; }

src/Server/ClientCapabilityProvider.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,29 @@ public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
9292
where TOptions : class
9393
{
9494
return _collection
95-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
95+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
9696
.FirstOrDefault(x => x != null);
9797
}
9898

9999
public Supports<TOptions> Can<TInterface, TOptions>(Func<TInterface, TOptions> action)
100100
where TOptions : class
101101
{
102102
var options = _collection
103-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
103+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
104104
.FirstOrDefault(x => x != null);
105105
if (options == null)
106106
return Supports.OfBoolean<TOptions>(false);
107107

108108
return _collection
109-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
109+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
110110
.FirstOrDefault(x => x != null);
111111
}
112112

113113
public TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<TInterface>, TOptions> action)
114114
where TOptions : class
115115
{
116116
return action(_collection
117-
.Select(x => x.RegisterOptions is TInterface cl ? cl : default)
117+
.Select(x => x.RegistrationOptions is TInterface cl ? cl : default)
118118
.Where(x => x != null));
119119
}
120120
}

src/Server/HandlerCollection.cs

+1-11
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
145145

146146
Type @params = null;
147147
object registrationOptions = null;
148-
Registration registration = null;
149148
if (@interface.GetTypeInfo().IsGenericType)
150149
{
151150
@params = @interface.GetTypeInfo().GetGenericArguments()[0];
@@ -156,15 +155,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
156155
registrationOptions = GetRegistrationMethod
157156
.MakeGenericMethod(registrationType)
158157
.Invoke(null, new object[] { handler });
159-
160-
if (_supportedCapabilities.AllowsDynamicRegistration(capabilityType))
161-
{
162-
registration = new Registration() {
163-
Id = Guid.NewGuid().ToString(),
164-
Method = method,
165-
RegisterOptions = registrationOptions
166-
};
167-
}
168158
}
169159

170160
var key = "default";
@@ -194,7 +184,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
194184
@params,
195185
registrationType,
196186
registrationOptions,
197-
registration,
187+
registrationType != null && _supportedCapabilities.AllowsDynamicRegistration(capabilityType),
198188
capabilityType,
199189
() => {
200190
_handlers.RemoveWhere(d => d.Handler == handler);

src/Server/HandlerDescriptor.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public HandlerDescriptor(
2222
Type handlerType,
2323
Type @params,
2424
Type registrationType,
25-
object registerOptions,
26-
Registration registration,
25+
object registrationOptions,
26+
bool allowsDynamicRegistration,
2727
Type capabilityType,
2828
Action disposeAction)
2929
{
3030
_disposeAction = disposeAction;
31+
Id = Guid.NewGuid();
3132
Method = method;
3233
Key = key;
3334
ImplementationType = handler.GetType();
@@ -36,8 +37,8 @@ public HandlerDescriptor(
3637
Params = @params;
3738
Response = Response;
3839
RegistrationType = registrationType;
39-
RegisterOptions = registerOptions;
40-
Registration = registration;
40+
RegistrationOptions = registrationOptions;
41+
AllowsDynamicRegistration = allowsDynamicRegistration;
4142
CapabilityType = capabilityType;
4243

4344
var requestInterface = @params?.GetInterfaces()
@@ -65,10 +66,11 @@ public HandlerDescriptor(
6566
public Type ImplementationType { get; }
6667
public Type HandlerType { get; }
6768

69+
public Guid Id { get; }
6870
public bool HasRegistration => RegistrationType != null;
6971
public Type RegistrationType { get; }
70-
public object RegisterOptions { get; }
71-
public Registration Registration { get; }
72+
public object RegistrationOptions { get; }
73+
public bool AllowsDynamicRegistration { get; }
7274

7375
public bool HasCapability => CapabilityType != null;
7476
public Type CapabilityType { get; }

src/Server/ISupportedCapabilities.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace OmniSharp.Extensions.LanguageServer.Server
66
{
77
public interface ISupportedCapabilities
88
{
9-
bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor);
109
bool AllowsDynamicRegistration(Type capabilityType);
1110
void SetCapability(ILspHandlerDescriptor descriptor, IJsonRpcHandler handler);
1211
}

src/Server/LanguageServer.cs

+6-2
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ private IDisposable RegisterHandlers(LspHandlerDescriptorDisposable handlerDispo
328328
using (var scope = _serviceProvider.CreateScope())
329329
{
330330
var registrations = handlerDisposable.Descriptors
331-
.Select(x => x.Registration)
332-
.Where(x => x != null)
331+
.Where(d => d.AllowsDynamicRegistration)
332+
.Select(d => new Registration() {
333+
Id = d.Id.ToString(),
334+
Method = d.Method,
335+
RegisterOptions = d.RegistrationOptions
336+
})
333337
.ToArray();
334338

335339
// Fire and forget

src/Server/Matchers/ExecuteCommandMatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public IEnumerable<ILspHandlerDescriptor> FindHandler(object parameters, IEnumer
2828
_logger.LogTrace("Registration options {OptionsName}", executeCommandParams.GetType().FullName);
2929
foreach (var descriptor in descriptors)
3030
{
31-
if (descriptor.RegisterOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
31+
if (descriptor.RegistrationOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
3232
{
3333
_logger.LogTrace("Checking handler {Method}:{Handler}",
3434
executeCommandParams.Command,

src/Server/Matchers/TextDocumentMatcher.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private IEnumerable<ILspHandlerDescriptor> GetHandler(IEnumerable<ILspHandlerDes
7575
foreach (var descriptor in descriptors)
7676
{
7777
_logger.LogTrace("Checking handler {Method}:{Handler}", method, descriptor.ImplementationType.FullName);
78-
var registrationOptions = descriptor.RegisterOptions as ITextDocumentRegistrationOptions;
78+
var registrationOptions = descriptor.RegistrationOptions as ITextDocumentRegistrationOptions;
7979

8080
_logger.LogTrace("Registration options {OptionsName}", registrationOptions?.GetType().FullName);
8181
_logger.LogTrace("Document Selector {DocumentSelector}", registrationOptions?.DocumentSelector.ToString());

src/Server/SupportedCapabilities.cs

-10
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ public void Add(IEnumerable<ISupports> supports)
2828
}
2929
}
3030

31-
public bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor)
32-
{
33-
if (descriptor.HasCapability && _supports.TryGetValue(descriptor.CapabilityType, out var capability))
34-
{
35-
if (capability is DynamicCapability dc)
36-
return dc.DynamicRegistration;
37-
}
38-
return false;
39-
}
40-
4131
public bool AllowsDynamicRegistration(Type capabilityType)
4232
{
4333
if (_supports.TryGetValue(capabilityType, out var capability))

test/Lsp.Tests/Matchers/ExecuteCommandHandlerMatcherTests.cs

+1-3
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ public void Should_Return_Handler_Descriptor()
7272
typeof(ExecuteCommandParams),
7373
typeof(ExecuteCommandRegistrationOptions),
7474
registrationsOptions,
75-
new Registration() {
76-
RegisterOptions = registrationsOptions
77-
},
75+
true,
7876
typeof(ExecuteCommandCapability),
7977
() => { })
8078
});

test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs

+14-14
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void Should_Not_Throw_Given_Another_Descriptor()
6767
typeof(CodeLensParams),
6868
null,
6969
null,
70-
null,
70+
false,
7171
null,
7272
() => { });
7373
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
@@ -101,7 +101,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
101101
typeof(CodeLens),
102102
null,
103103
null,
104-
null,
104+
false,
105105
null,
106106
() => { }),
107107
new HandlerDescriptor(DocumentNames.CodeLensResolve,
@@ -111,7 +111,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
111111
typeof(CodeLens),
112112
null,
113113
null,
114-
null,
114+
false,
115115
null,
116116
() => { }),
117117
})
@@ -140,7 +140,7 @@ public void Should_Handle_Null_Data()
140140
typeof(CompletionItem),
141141
null,
142142
null,
143-
null,
143+
false,
144144
null,
145145
() => { }),
146146
})
@@ -171,7 +171,7 @@ public void Should_Handle_Simple_Json_Data()
171171
typeof(CompletionItem),
172172
null,
173173
null,
174-
null,
174+
false,
175175
null,
176176
() => { }),
177177
})
@@ -204,7 +204,7 @@ public void Should_Return_CompletionResolve_Descriptor()
204204
typeof(CompletionItem),
205205
null,
206206
null,
207-
null,
207+
false,
208208
null,
209209
() => { }),
210210
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -214,7 +214,7 @@ public void Should_Return_CompletionResolve_Descriptor()
214214
typeof(CompletionItem),
215215
null,
216216
null,
217-
null,
217+
false,
218218
null,
219219
() => { }),
220220
})
@@ -254,7 +254,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
254254
typeof(CompletionItem),
255255
null,
256256
null,
257-
null,
257+
false,
258258
null,
259259
() => { }),
260260
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -264,7 +264,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
264264
typeof(CompletionItem),
265265
null,
266266
null,
267-
null,
267+
false,
268268
null,
269269
() => { }),
270270
})
@@ -300,7 +300,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
300300
typeof(CompletionItem),
301301
null,
302302
null,
303-
null,
303+
false,
304304
null,
305305
() => { }),
306306
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -310,7 +310,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
310310
typeof(CompletionItem),
311311
null,
312312
null,
313-
null,
313+
false,
314314
null,
315315
() => { }),
316316
})
@@ -338,7 +338,7 @@ public async Task Should_Update_CompletionItems_With_HandlerType()
338338
typeof(CompletionParams),
339339
null,
340340
null,
341-
null,
341+
false,
342342
null,
343343
() => { });
344344
var handlerMatcher = new ResolveCommandPipeline<CompletionParams, CompletionList>(
@@ -381,7 +381,7 @@ public async Task Should_Update_CodeLensContainer_With_HandlerType()
381381
typeof(CodeLensParams),
382382
null,
383383
null,
384-
null,
384+
false,
385385
null,
386386
() => { });
387387
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
@@ -424,7 +424,7 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
424424
typeof(CodeLens),
425425
null,
426426
null,
427-
null,
427+
false,
428428
null,
429429
() => { });
430430
var handlerMatcher = new ResolveCommandPipeline<CodeLens, CodeLens>(

0 commit comments

Comments
 (0)