Skip to content

Commit 0b3c365

Browse files
fixed issue with non object JTokens in resolve command matcher. Determined that HandlerDescriptor was setting CanBeResolvedHandlerType incorrectly for some cases
1 parent 6d8ff97 commit 0b3c365

File tree

3 files changed

+40
-9
lines changed

3 files changed

+40
-9
lines changed

src/Server/HandlerDescriptor.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using System.Reflection;
45
using OmniSharp.Extensions.JsonRpc;
56
using OmniSharp.Extensions.LanguageServer.Protocol;
@@ -23,10 +24,11 @@ public HandlerDescriptor(string method, string key, IJsonRpcHandler handler, Typ
2324
Params = @params;
2425
RegistrationType = registrationType;
2526
CapabilityType = capabilityType;
26-
if (@params != null && typeof(ICanBeResolved).GetTypeInfo().IsAssignableFrom(@params))
27-
{
28-
CanBeResolvedHandlerType = typeof(ICanBeResolvedHandler<>).MakeGenericType(@params);
29-
}
27+
28+
// If multiple are implemented this behavior is unknown
29+
CanBeResolvedHandlerType = handler.GetType().GetTypeInfo()
30+
.ImplementedInterfaces
31+
.FirstOrDefault(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ICanBeResolvedHandler<>));
3032
}
3133

3234
public IJsonRpcHandler Handler { get; }

src/Server/Matchers/ResolveCommandMatcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public IEnumerable<ILspHandlerDescriptor> FindHandler(object parameters, IEnumer
3030
if (parameters is ICanBeResolved canBeResolved)
3131
{
3232
string handlerType = null;
33-
if (canBeResolved.Data != null)
34-
handlerType = canBeResolved.Data.Value<string>(PrivateHandlerTypeName);
33+
if (canBeResolved.Data != null && canBeResolved.Data.Type == JTokenType.Object)
34+
handlerType = canBeResolved.Data?[PrivateHandlerTypeName]?.ToString();
3535

3636
if (string.IsNullOrWhiteSpace(handlerType))
3737
{

test/Lsp.Tests/Matchers/ResolveCommandMatcherTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,35 @@ public void Should_Handle_Null_Data()
139139
result.Should().Contain(x => x.Handler == resolveHandler);
140140
}
141141

142+
[Fact]
143+
public void Should_Handle_Simple_Json_Data()
144+
{
145+
// Given
146+
var handlerMatcher = new ResolveCommandMatcher(_logger);
147+
var resolveHandler = Substitute.For<ICompletionResolveHandler>();
148+
resolveHandler.CanResolve(Arg.Any<CompletionItem>()).Returns(true);
149+
150+
// When
151+
var result = handlerMatcher.FindHandler(new CompletionItem() {
152+
Data = new Uri("file:///c%3A/Users/mb/src/gh/Cake.Json/src/Cake.Json/Namespaces.cs")
153+
},
154+
new List<HandlerDescriptor> {
155+
new HandlerDescriptor(DocumentNames.CompletionResolve,
156+
"Key",
157+
resolveHandler,
158+
resolveHandler.GetType(),
159+
typeof(CompletionItem),
160+
null,
161+
null,
162+
() => { }),
163+
})
164+
.ToArray();
165+
166+
// Then
167+
result.Should().NotBeNullOrEmpty();
168+
result.Should().Contain(x => x.Handler == resolveHandler);
169+
}
170+
142171
[Fact]
143172
public void Should_Return_CompletionResolve_Descriptor()
144173
{
@@ -296,7 +325,7 @@ public void Should_FindPostProcessor_AsSelf_For_Completion()
296325
"Key",
297326
resolveHandler,
298327
resolveHandler.GetType(),
299-
typeof(CodeLensParams),
328+
typeof(CompletionParams),
300329
null,
301330
null,
302331
() => { });
@@ -325,7 +354,7 @@ public void Should_Update_CompletionItems_With_HandlerType()
325354
"Key",
326355
resolveHandler as IJsonRpcHandler,
327356
resolveHandler.GetType(),
328-
typeof(CompletionItem),
357+
typeof(CompletionParams),
329358
null,
330359
null,
331360
() => { });
@@ -363,7 +392,7 @@ public void Should_Update_CodeLensContainer_With_HandlerType()
363392
"Key",
364393
resolveHandler as IJsonRpcHandler,
365394
resolveHandler.GetType(),
366-
typeof(CodeLens),
395+
typeof(CodeLensParams),
367396
null,
368397
null,
369398
() => { });

0 commit comments

Comments
 (0)