Skip to content

Commit

Permalink
Support Client capabilities implementing ConnectedCapability<> multip…
Browse files Browse the repository at this point in the history
…le times.

- Fixes #132
  • Loading branch information
bjorkstromm committed Apr 3, 2019
1 parent 7d8567c commit 21907ad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 31 deletions.
9 changes: 5 additions & 4 deletions src/Server/ClientCapabilityProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ public bool HasStaticHandler<T>(Supports<T> capability)
if (capability.Value == null) return false;
if (capability.Value.DynamicRegistration == true) return false;

var handlerType = typeof(T).GetTypeInfo().ImplementedInterfaces
.Single(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
.GetTypeInfo().GetGenericArguments()[0].GetTypeInfo();
return !capability.Value.DynamicRegistration == true && _collection.ContainsHandler(handlerType);
var handlerTypes = typeof(T).GetTypeInfo().ImplementedInterfaces
.Where(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
.Select(x => x.GetTypeInfo().GetGenericArguments()[0].GetTypeInfo());

return handlerTypes.All(_collection.ContainsHandler);
}

public IOptionsGetter GetStaticOptions<T>(Supports<T> capability)
Expand Down
39 changes: 12 additions & 27 deletions test/Lsp.Tests/ClientCapabilityProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,9 @@ namespace Lsp.Tests
{
public class ClientCapabilityProviderTests
{
private static readonly Type[] Capabilities = {
typeof(CompletionCapability),
typeof(HoverCapability),
typeof(SignatureHelpCapability),
typeof(ReferencesCapability),
typeof(DocumentHighlightCapability),
typeof(DocumentSymbolCapability),
typeof(DocumentFormattingCapability),
typeof(DocumentRangeFormattingCapability),
typeof(DocumentOnTypeFormattingCapability),
typeof(DefinitionCapability),
typeof(CodeActionCapability),
typeof(CodeLensCapability),
typeof(DocumentLinkCapability),
typeof(RenameCapability),
typeof(WorkspaceSymbolCapability),
typeof(ExecuteCommandCapability),
};
private static readonly Type[] Capabilities = typeof(ClientCapabilities).Assembly.GetTypes()
.Where(x => x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ConnectedCapability<>)))
.ToArray();

[Theory, MemberData(nameof(AllowSupportedCapabilities))]
public void Should_AllowSupportedCapabilities(IJsonRpcHandler handler, object instance)
Expand All @@ -51,8 +36,8 @@ public void Should_AllowSupportedCapabilities(IJsonRpcHandler handler, object in
public static IEnumerable<object[]> AllowSupportedCapabilities()
{
return GetItems(Capabilities, type => {
var handlerType = GetHandlerType(type);
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true, Activator.CreateInstance(type)) };
});
}
Expand Down Expand Up @@ -99,8 +84,8 @@ public void Should_Invoke_Reduce_Delegate()
public static IEnumerable<object[]> DisallowUnsupportedCapabilities()
{
return GetItems(Capabilities, type => {
var handlerType = GetHandlerType(type);
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), false) };
});
}
Expand All @@ -119,8 +104,8 @@ public void Should_DisallowNullSupportedCapabilities(IJsonRpcHandler handler, ob
public static IEnumerable<object[]> DisallowNullSupportsCapabilities()
{
return GetItems(Capabilities, type => {
var handlerType = GetHandlerType(type);
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
var handlerTypes = GetHandlerTypes(type);
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true) };
});
}
Expand Down Expand Up @@ -150,11 +135,11 @@ private static IEnumerable<object[]> GetItems<T>(IEnumerable<T> types, Func<T, I
return types.Select(x => func(x).ToArray());
}

private static Type GetHandlerType(Type type)
private static IEnumerable<Type> GetHandlerTypes(Type type)
{
return type.GetTypeInfo().ImplementedInterfaces
.Single(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
.GetTypeInfo().GetGenericArguments()[0];
.Where(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
.Select(x => x.GetTypeInfo().GetGenericArguments()[0]);
}
}
}

0 comments on commit 21907ad

Please sign in to comment.