Skip to content

Commit 21907ad

Browse files
committed
Support Client capabilities implementing ConnectedCapability<> multiple times.
- Fixes #132
1 parent 7d8567c commit 21907ad

File tree

2 files changed

+17
-31
lines changed

2 files changed

+17
-31
lines changed

src/Server/ClientCapabilityProvider.cs

+5-4
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ public bool HasStaticHandler<T>(Supports<T> capability)
2525
if (capability.Value == null) return false;
2626
if (capability.Value.DynamicRegistration == true) return false;
2727

28-
var handlerType = typeof(T).GetTypeInfo().ImplementedInterfaces
29-
.Single(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
30-
.GetTypeInfo().GetGenericArguments()[0].GetTypeInfo();
31-
return !capability.Value.DynamicRegistration == true && _collection.ContainsHandler(handlerType);
28+
var handlerTypes = typeof(T).GetTypeInfo().ImplementedInterfaces
29+
.Where(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
30+
.Select(x => x.GetTypeInfo().GetGenericArguments()[0].GetTypeInfo());
31+
32+
return handlerTypes.All(_collection.ContainsHandler);
3233
}
3334

3435
public IOptionsGetter GetStaticOptions<T>(Supports<T> capability)

test/Lsp.Tests/ClientCapabilityProviderTests.cs

+12-27
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,9 @@ namespace Lsp.Tests
1818
{
1919
public class ClientCapabilityProviderTests
2020
{
21-
private static readonly Type[] Capabilities = {
22-
typeof(CompletionCapability),
23-
typeof(HoverCapability),
24-
typeof(SignatureHelpCapability),
25-
typeof(ReferencesCapability),
26-
typeof(DocumentHighlightCapability),
27-
typeof(DocumentSymbolCapability),
28-
typeof(DocumentFormattingCapability),
29-
typeof(DocumentRangeFormattingCapability),
30-
typeof(DocumentOnTypeFormattingCapability),
31-
typeof(DefinitionCapability),
32-
typeof(CodeActionCapability),
33-
typeof(CodeLensCapability),
34-
typeof(DocumentLinkCapability),
35-
typeof(RenameCapability),
36-
typeof(WorkspaceSymbolCapability),
37-
typeof(ExecuteCommandCapability),
38-
};
21+
private static readonly Type[] Capabilities = typeof(ClientCapabilities).Assembly.GetTypes()
22+
.Where(x => x.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ConnectedCapability<>)))
23+
.ToArray();
3924

4025
[Theory, MemberData(nameof(AllowSupportedCapabilities))]
4126
public void Should_AllowSupportedCapabilities(IJsonRpcHandler handler, object instance)
@@ -51,8 +36,8 @@ public void Should_AllowSupportedCapabilities(IJsonRpcHandler handler, object in
5136
public static IEnumerable<object[]> AllowSupportedCapabilities()
5237
{
5338
return GetItems(Capabilities, type => {
54-
var handlerType = GetHandlerType(type);
55-
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
39+
var handlerTypes = GetHandlerTypes(type);
40+
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
5641
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true, Activator.CreateInstance(type)) };
5742
});
5843
}
@@ -99,8 +84,8 @@ public void Should_Invoke_Reduce_Delegate()
9984
public static IEnumerable<object[]> DisallowUnsupportedCapabilities()
10085
{
10186
return GetItems(Capabilities, type => {
102-
var handlerType = GetHandlerType(type);
103-
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
87+
var handlerTypes = GetHandlerTypes(type);
88+
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
10489
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), false) };
10590
});
10691
}
@@ -119,8 +104,8 @@ public void Should_DisallowNullSupportedCapabilities(IJsonRpcHandler handler, ob
119104
public static IEnumerable<object[]> DisallowNullSupportsCapabilities()
120105
{
121106
return GetItems(Capabilities, type => {
122-
var handlerType = GetHandlerType(type);
123-
var handler = Substitute.For(new Type[] { handlerType }, new object[0]);
107+
var handlerTypes = GetHandlerTypes(type);
108+
var handler = Substitute.For(handlerTypes.ToArray(), new object[0]);
124109
return new[] { handler, Activator.CreateInstance(typeof(Supports<>).MakeGenericType(type), true) };
125110
});
126111
}
@@ -150,11 +135,11 @@ private static IEnumerable<object[]> GetItems<T>(IEnumerable<T> types, Func<T, I
150135
return types.Select(x => func(x).ToArray());
151136
}
152137

153-
private static Type GetHandlerType(Type type)
138+
private static IEnumerable<Type> GetHandlerTypes(Type type)
154139
{
155140
return type.GetTypeInfo().ImplementedInterfaces
156-
.Single(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
157-
.GetTypeInfo().GetGenericArguments()[0];
141+
.Where(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>))
142+
.Select(x => x.GetTypeInfo().GetGenericArguments()[0]);
158143
}
159144
}
160145
}

0 commit comments

Comments
 (0)