Skip to content

Commit

Permalink
Merge pull request #156 from OmniSharp/fix-registration
Browse files Browse the repository at this point in the history
Move registration options to the descriptor
  • Loading branch information
david-driscoll authored Jul 19, 2019
2 parents df30f5e + 60e4f85 commit 2c88d7c
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 65 deletions.
6 changes: 3 additions & 3 deletions src/JsonRpc/JsonRpc.csproj
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" />
<PackageReference Include="Newtonsoft.Json" />
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/AssemblyInfo.cs" Visible="false" />
<Compile Include="../../submodules/MediatR/src/MediatR/**/*.cs" Exclude="**/*AssemblyInfo.cs" Visible="false" />
<Compile Include="../../submodules/MediatR.Extensions.Microsoft.DependencyInjection/src/MediatR.Extensions.Microsoft.DependencyInjection/**/*.cs" Exclude="**/*AssemblyInfo.cs" Visible="false" />
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion src/JsonRpc/RequestRouterBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public async Task RouteNotification(TDescriptor descriptor, Notification notific
@params = notification.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
}

await HandleNotification(mediator, descriptor, @params ?? EmptyRequest.Instance, token);
await HandleNotification(mediator, descriptor, @params ?? Activator.CreateInstance(descriptor.Params), token);
}
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion src/Protocol/Models/Registration.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Newtonsoft.Json;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using OmniSharp.Extensions.LanguageServer.Protocol.Serialization;

Expand Down
4 changes: 3 additions & 1 deletion src/Server/Abstractions/ILspHandlerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server.Abstractions
{
public interface ILspHandlerDescriptor : IHandlerDescriptor
{
Guid Id { get; }
bool HasRegistration { get; }
Type RegistrationType { get; }
Registration Registration { get; }
object RegistrationOptions { get; }
bool AllowsDynamicRegistration { get; }

bool HasCapability { get; }
Type CapabilityType { get; }
Expand Down
8 changes: 4 additions & 4 deletions src/Server/ClientCapabilityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,29 +92,29 @@ public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
return _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
}

public Supports<TOptions> Can<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
var options = _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
if (options == null)
return Supports.OfBoolean<TOptions>(false);

return _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
}

public TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<TInterface>, TOptions> action)
where TOptions : class
{
return action(_collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? cl : default)
.Select(x => x.RegistrationOptions is TInterface cl ? cl : default)
.Where(x => x != null));
}
}
Expand Down
13 changes: 2 additions & 11 deletions src/Server/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp

Type @params = null;
object registrationOptions = null;
Registration registration = null;
if (@interface.GetTypeInfo().IsGenericType)
{
@params = @interface.GetTypeInfo().GetGenericArguments()[0];
Expand All @@ -156,15 +155,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
registrationOptions = GetRegistrationMethod
.MakeGenericMethod(registrationType)
.Invoke(null, new object[] { handler });

if (_supportedCapabilities.AllowsDynamicRegistration(capabilityType))
{
registration = new Registration() {
Id = Guid.NewGuid().ToString(),
Method = method,
RegisterOptions = registrationOptions
};
}
}

var key = "default";
Expand Down Expand Up @@ -193,7 +183,8 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
@interface,
@params,
registrationType,
registration,
registrationOptions,
registrationType != null && _supportedCapabilities.AllowsDynamicRegistration(capabilityType),
capabilityType,
() => {
_handlers.RemoveWhere(d => d.Handler == handler);
Expand Down
11 changes: 8 additions & 3 deletions src/Server/HandlerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ public HandlerDescriptor(
Type handlerType,
Type @params,
Type registrationType,
Registration registration,
object registrationOptions,
bool allowsDynamicRegistration,
Type capabilityType,
Action disposeAction)
{
_disposeAction = disposeAction;
Id = Guid.NewGuid();
Method = method;
Key = key;
ImplementationType = handler.GetType();
Expand All @@ -35,7 +37,8 @@ public HandlerDescriptor(
Params = @params;
Response = Response;
RegistrationType = registrationType;
Registration = registration;
RegistrationOptions = registrationOptions;
AllowsDynamicRegistration = allowsDynamicRegistration;
CapabilityType = capabilityType;

var requestInterface = @params?.GetInterfaces()
Expand Down Expand Up @@ -63,9 +66,11 @@ public HandlerDescriptor(
public Type ImplementationType { get; }
public Type HandlerType { get; }

public Guid Id { get; }
public bool HasRegistration => RegistrationType != null;
public Type RegistrationType { get; }
public Registration Registration { get; }
public object RegistrationOptions { get; }
public bool AllowsDynamicRegistration { get; }

public bool HasCapability => CapabilityType != null;
public Type CapabilityType { get; }
Expand Down
1 change: 0 additions & 1 deletion src/Server/ISupportedCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ namespace OmniSharp.Extensions.LanguageServer.Server
{
public interface ISupportedCapabilities
{
bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor);
bool AllowsDynamicRegistration(Type capabilityType);
void SetCapability(ILspHandlerDescriptor descriptor, IJsonRpcHandler handler);
}
Expand Down
8 changes: 6 additions & 2 deletions src/Server/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,12 @@ private IDisposable RegisterHandlers(LspHandlerDescriptorDisposable handlerDispo
using (var scope = _serviceProvider.CreateScope())
{
var registrations = handlerDisposable.Descriptors
.Select(x => x.Registration)
.Where(x => x != null)
.Where(d => d.AllowsDynamicRegistration)
.Select(d => new Registration() {
Id = d.Id.ToString(),
Method = d.Method,
RegisterOptions = d.RegistrationOptions
})
.ToArray();

// Fire and forget
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Matchers/ExecuteCommandMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IEnumerable<ILspHandlerDescriptor> FindHandler(object parameters, IEnumer
_logger.LogTrace("Registration options {OptionsName}", executeCommandParams.GetType().FullName);
foreach (var descriptor in descriptors)
{
if (descriptor.Registration?.RegisterOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
if (descriptor.RegistrationOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
{
_logger.LogTrace("Checking handler {Method}:{Handler}",
executeCommandParams.Command,
Expand Down
2 changes: 1 addition & 1 deletion src/Server/Matchers/TextDocumentMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private IEnumerable<ILspHandlerDescriptor> GetHandler(IEnumerable<ILspHandlerDes
foreach (var descriptor in descriptors)
{
_logger.LogTrace("Checking handler {Method}:{Handler}", method, descriptor.ImplementationType.FullName);
var registrationOptions = descriptor.Registration?.RegisterOptions as ITextDocumentRegistrationOptions;
var registrationOptions = descriptor.RegistrationOptions as ITextDocumentRegistrationOptions;

_logger.LogTrace("Registration options {OptionsName}", registrationOptions?.GetType().FullName);
_logger.LogTrace("Document Selector {DocumentSelector}", registrationOptions?.DocumentSelector.ToString());
Expand Down
10 changes: 0 additions & 10 deletions src/Server/SupportedCapabilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,6 @@ public void Add(IEnumerable<ISupports> supports)
}
}

public bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor)
{
if (descriptor.HasCapability && _supports.TryGetValue(descriptor.CapabilityType, out var capability))
{
if (capability is DynamicCapability dc)
return dc.DynamicRegistration;
}
return false;
}

public bool AllowsDynamicRegistration(Type capabilityType)
{
if (_supports.TryGetValue(capabilityType, out var capability))
Expand Down
10 changes: 5 additions & 5 deletions test/Lsp.Tests/Matchers/ExecuteCommandHandlerMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ public void Should_Return_Handler_Descriptor()
// Given
var handlerMatcher = AutoSubstitute.Resolve<ExecuteCommandMatcher>();
var executeCommandHandler = Substitute.For<IExecuteCommandHandler>().With(new Container<string>("Command"));
var registrationsOptions = new ExecuteCommandRegistrationOptions() {
Commands = new Container<string>("Command")
};

// When
var result = handlerMatcher.FindHandler(new ExecuteCommandParams { Command = "Command" },
Expand All @@ -68,11 +71,8 @@ public void Should_Return_Handler_Descriptor()
executeCommandHandler.GetType(),
typeof(ExecuteCommandParams),
typeof(ExecuteCommandRegistrationOptions),
new Registration() {
RegisterOptions = new ExecuteCommandRegistrationOptions() {
Commands = new Container<string>("Command")
}
},
registrationsOptions,
true,
typeof(ExecuteCommandCapability),
() => { })
});
Expand Down
14 changes: 14 additions & 0 deletions test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public void Should_Not_Throw_Given_Another_Descriptor()
typeof(CodeLensParams),
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
Expand Down Expand Up @@ -100,6 +101,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
typeof(CodeLens),
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CodeLensResolve,
Expand All @@ -109,6 +111,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
typeof(CodeLens),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -137,6 +140,7 @@ public void Should_Handle_Null_Data()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -167,6 +171,7 @@ public void Should_Handle_Simple_Json_Data()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -199,6 +204,7 @@ public void Should_Return_CompletionResolve_Descriptor()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -208,6 +214,7 @@ public void Should_Return_CompletionResolve_Descriptor()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -247,6 +254,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -256,6 +264,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -291,6 +300,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -300,6 +310,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
typeof(CompletionItem),
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -327,6 +338,7 @@ public async Task Should_Update_CompletionItems_With_HandlerType()
typeof(CompletionParams),
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CompletionParams, CompletionList>(
Expand Down Expand Up @@ -369,6 +381,7 @@ public async Task Should_Update_CodeLensContainer_With_HandlerType()
typeof(CodeLensParams),
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
Expand Down Expand Up @@ -411,6 +424,7 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
typeof(CodeLens),
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLens, CodeLens>(
Expand Down
Loading

0 comments on commit 2c88d7c

Please sign in to comment.