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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 2 additions & 11 deletions
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

Lines changed: 8 additions & 3 deletions
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

Lines changed: 0 additions & 1 deletion
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

Lines changed: 6 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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,

0 commit comments

Comments
 (0)