Skip to content

Commit

Permalink
Added id and AllowsDynamicRegistration to descriptor, removed registr…
Browse files Browse the repository at this point in the history
…ation and create the registrations on demand as needed
  • Loading branch information
david-driscoll committed Jul 19, 2019
1 parent a52197f commit bb2b4bd
Show file tree
Hide file tree
Showing 12 changed files with 40 additions and 56 deletions.
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
5 changes: 3 additions & 2 deletions src/Server/Abstractions/ILspHandlerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server.Abstractions
{
public interface ILspHandlerDescriptor : IHandlerDescriptor
{
Guid Id { get; }
bool HasRegistration { get; }
Type RegistrationType { get; }
object RegisterOptions { 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.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.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.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.RegisterOptions is TInterface cl ? cl : default)
.Select(x => x.RegistrationOptions is TInterface cl ? cl : default)
.Where(x => x != null));
}
}
Expand Down
12 changes: 1 addition & 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 @@ -194,7 +184,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
@params,
registrationType,
registrationOptions,
registration,
registrationType != null && _supportedCapabilities.AllowsDynamicRegistration(capabilityType),
capabilityType,
() => {
_handlers.RemoveWhere(d => d.Handler == handler);
Expand Down
14 changes: 8 additions & 6 deletions src/Server/HandlerDescriptor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public HandlerDescriptor(
Type handlerType,
Type @params,
Type registrationType,
object registerOptions,
Registration registration,
object registrationOptions,
bool allowsDynamicRegistration,
Type capabilityType,
Action disposeAction)
{
_disposeAction = disposeAction;
Id = Guid.NewGuid();
Method = method;
Key = key;
ImplementationType = handler.GetType();
Expand All @@ -36,8 +37,8 @@ public HandlerDescriptor(
Params = @params;
Response = Response;
RegistrationType = registrationType;
RegisterOptions = registerOptions;
Registration = registration;
RegistrationOptions = registrationOptions;
AllowsDynamicRegistration = allowsDynamicRegistration;
CapabilityType = capabilityType;

var requestInterface = @params?.GetInterfaces()
Expand Down Expand Up @@ -65,10 +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 object RegisterOptions { 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.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.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
4 changes: 1 addition & 3 deletions test/Lsp.Tests/Matchers/ExecuteCommandHandlerMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,7 @@ public void Should_Return_Handler_Descriptor()
typeof(ExecuteCommandParams),
typeof(ExecuteCommandRegistrationOptions),
registrationsOptions,
new Registration() {
RegisterOptions = registrationsOptions
},
true,
typeof(ExecuteCommandCapability),
() => { })
});
Expand Down
28 changes: 14 additions & 14 deletions test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void Should_Not_Throw_Given_Another_Descriptor()
typeof(CodeLensParams),
null,
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
Expand Down Expand Up @@ -101,7 +101,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
typeof(CodeLens),
null,
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CodeLensResolve,
Expand All @@ -111,7 +111,7 @@ public void Should_Return_CodeLensResolve_Descriptor()
typeof(CodeLens),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -140,7 +140,7 @@ public void Should_Handle_Null_Data()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -171,7 +171,7 @@ public void Should_Handle_Simple_Json_Data()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -204,7 +204,7 @@ public void Should_Return_CompletionResolve_Descriptor()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -214,7 +214,7 @@ public void Should_Return_CompletionResolve_Descriptor()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -254,7 +254,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -264,7 +264,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -300,7 +300,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
new HandlerDescriptor(DocumentNames.CompletionResolve,
Expand All @@ -310,7 +310,7 @@ public void Should_Deal_WithHandlers_That_Not_Also_Resolvers2()
typeof(CompletionItem),
null,
null,
null,
false,
null,
() => { }),
})
Expand Down Expand Up @@ -338,7 +338,7 @@ public async Task Should_Update_CompletionItems_With_HandlerType()
typeof(CompletionParams),
null,
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CompletionParams, CompletionList>(
Expand Down Expand Up @@ -381,7 +381,7 @@ public async Task Should_Update_CodeLensContainer_With_HandlerType()
typeof(CodeLensParams),
null,
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLensParams, CodeLensContainer>(
Expand Down Expand Up @@ -424,7 +424,7 @@ public async Task Should_Update_CodeLens_Removing_HandlerType()
typeof(CodeLens),
null,
null,
null,
false,
null,
() => { });
var handlerMatcher = new ResolveCommandPipeline<CodeLens, CodeLens>(
Expand Down

0 comments on commit bb2b4bd

Please sign in to comment.