Skip to content

Commit 2c88d7c

Browse files
Merge pull request #156 from OmniSharp/fix-registration
Move registration options to the descriptor
2 parents df30f5e + 60e4f85 commit 2c88d7c

15 files changed

+88
-65
lines changed

src/JsonRpc/JsonRpc.csproj

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.0</TargetFrameworks>
44
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -13,7 +13,7 @@
1313
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
1414
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
1515
<PackageReference Include="Newtonsoft.Json" />
16-
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
17-
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
16+
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/*AssemblyInfo.cs" Visible="false" />
17+
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/*AssemblyInfo.cs" Visible="false" />
1818
</ItemGroup>
1919
</Project>

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/Protocol/Models/Registration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;
44

src/Server/Abstractions/ILspHandlerDescriptor.cs

+3-1
Original file line numberDiff line numberDiff line change
@@ -6,9 +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-
Registration Registration { get; }
12+
object RegistrationOptions { get; }
13+
bool AllowsDynamicRegistration { get; }
1214

1315
bool HasCapability { get; }
1416
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.Registration?.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.Registration?.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.Registration?.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.Registration?.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

+2-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";
@@ -193,7 +183,8 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
193183
@interface,
194184
@params,
195185
registrationType,
196-
registration,
186+
registrationOptions,
187+
registrationType != null && _supportedCapabilities.AllowsDynamicRegistration(capabilityType),
197188
capabilityType,
198189
() => {
199190
_handlers.RemoveWhere(d => d.Handler == handler);

src/Server/HandlerDescriptor.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ public HandlerDescriptor(
2222
Type handlerType,
2323
Type @params,
2424
Type registrationType,
25-
Registration registration,
25+
object registrationOptions,
26+
bool allowsDynamicRegistration,
2627
Type capabilityType,
2728
Action disposeAction)
2829
{
2930
_disposeAction = disposeAction;
31+
Id = Guid.NewGuid();
3032
Method = method;
3133
Key = key;
3234
ImplementationType = handler.GetType();
@@ -35,7 +37,8 @@ public HandlerDescriptor(
3537
Params = @params;
3638
Response = Response;
3739
RegistrationType = registrationType;
38-
Registration = registration;
40+
RegistrationOptions = registrationOptions;
41+
AllowsDynamicRegistration = allowsDynamicRegistration;
3942
CapabilityType = capabilityType;
4043

4144
var requestInterface = @params?.GetInterfaces()
@@ -63,9 +66,11 @@ public HandlerDescriptor(
6366
public Type ImplementationType { get; }
6467
public Type HandlerType { get; }
6568

69+
public Guid Id { get; }
6670
public bool HasRegistration => RegistrationType != null;
6771
public Type RegistrationType { get; }
68-
public Registration Registration { get; }
72+
public object RegistrationOptions { get; }
73+
public bool AllowsDynamicRegistration { get; }
6974

7075
public bool HasCapability => CapabilityType != null;
7176
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.Registration?.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.Registration?.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

+5-5
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ public void Should_Return_Handler_Descriptor()
5858
// Given
5959
var handlerMatcher = AutoSubstitute.Resolve<ExecuteCommandMatcher>();
6060
var executeCommandHandler = Substitute.For<IExecuteCommandHandler>().With(new Container<string>("Command"));
61+
var registrationsOptions = new ExecuteCommandRegistrationOptions() {
62+
Commands = new Container<string>("Command")
63+
};
6164

6265
// When
6366
var result = handlerMatcher.FindHandler(new ExecuteCommandParams { Command = "Command" },
@@ -68,11 +71,8 @@ public void Should_Return_Handler_Descriptor()
6871
executeCommandHandler.GetType(),
6972
typeof(ExecuteCommandParams),
7073
typeof(ExecuteCommandRegistrationOptions),
71-
new Registration() {
72-
RegisterOptions = new ExecuteCommandRegistrationOptions() {
73-
Commands = new Container<string>("Command")
74-
}
75-
},
74+
registrationsOptions,
75+
true,
7676
typeof(ExecuteCommandCapability),
7777
() => { })
7878
});

test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs

+14
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public void Should_Not_Throw_Given_Another_Descriptor()
6767
typeof(CodeLensParams),
6868
null,
6969
null,
70+
false,
7071
null,
7172
() => { });
7273
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
@@ -100,6 +101,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
100101
typeof(CodeLens),
101102
null,
102103
null,
104+
false,
103105
null,
104106
() => { }),
105107
new HandlerDescriptor(DocumentNames.CodeLensResolve,
@@ -109,6 +111,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
109111
typeof(CodeLens),
110112
null,
111113
null,
114+
false,
112115
null,
113116
() => { }),
114117
})
@@ -137,6 +140,7 @@ public void Should_Handle_Null_Data()
137140
typeof(CompletionItem),
138141
null,
139142
null,
143+
false,
140144
null,
141145
() => { }),
142146
})
@@ -167,6 +171,7 @@ public void Should_Handle_Simple_Json_Data()
167171
typeof(CompletionItem),
168172
null,
169173
null,
174+
false,
170175
null,
171176
() => { }),
172177
})
@@ -199,6 +204,7 @@ public void Should_Return_CompletionResolve_Descriptor()
199204
typeof(CompletionItem),
200205
null,
201206
null,
207+
false,
202208
null,
203209
() => { }),
204210
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -208,6 +214,7 @@ public void Should_Return_CompletionResolve_Descriptor()
208214
typeof(CompletionItem),
209215
null,
210216
null,
217+
false,
211218
null,
212219
() => { }),
213220
})
@@ -247,6 +254,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
247254
typeof(CompletionItem),
248255
null,
249256
null,
257+
false,
250258
null,
251259
() => { }),
252260
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -256,6 +264,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
256264
typeof(CompletionItem),
257265
null,
258266
null,
267+
false,
259268
null,
260269
() => { }),
261270
})
@@ -291,6 +300,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
291300
typeof(CompletionItem),
292301
null,
293302
null,
303+
false,
294304
null,
295305
() => { }),
296306
new HandlerDescriptor(DocumentNames.CompletionResolve,
@@ -300,6 +310,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
300310
typeof(CompletionItem),
301311
null,
302312
null,
313+
false,
303314
null,
304315
() => { }),
305316
})
@@ -327,6 +338,7 @@ public async Task Should_Update_CompletionItems_With_HandlerType()
327338
typeof(CompletionParams),
328339
null,
329340
null,
341+
false,
330342
null,
331343
() => { });
332344
var handlerMatcher = new ResolveCommandPipeline<CompletionParams, CompletionList>(
@@ -369,6 +381,7 @@ public async Task Should_Update_CodeLensContainer_With_HandlerType()
369381
typeof(CodeLensParams),
370382
null,
371383
null,
384+
false,
372385
null,
373386
() => { });
374387
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
@@ -411,6 +424,7 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
411424
typeof(CodeLens),
412425
null,
413426
null,
427+
false,
414428
null,
415429
() => { });
416430
var handlerMatcher = new ResolveCommandPipeline<CodeLens, CodeLens>(

0 commit comments

Comments
 (0)