Skip to content

Commit 6be75f3

Browse files
Attemt to make HasHandler to work correctly
1 parent b7b2b74 commit 6be75f3

File tree

4 files changed

+34
-22
lines changed

4 files changed

+34
-22
lines changed

src/Lsp/HandlerCollection.cs

+1-19
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public IDisposable Add(IEnumerable<IJsonRpcHandler> handlers)
3737
.ImplementedInterfaces
3838
.Where(x => !string.IsNullOrWhiteSpace(LspHelper.GetMethodName(x))))
3939
{
40-
var @interface = GetHandlerInterface(implementedInterface);
40+
var @interface = HandlerTypeHelpers.GetHandlerInterface(implementedInterface);
4141
var registration = UnwrapGenericType(typeof(IRegistration<>), implementedInterface);
4242
var capability = UnwrapGenericType(typeof(ICapability<>), implementedInterface);
4343

@@ -72,24 +72,6 @@ public IDisposable Add(IEnumerable<IJsonRpcHandler> handlers)
7272
return new ImutableDisposable(descriptors);
7373
}
7474

75-
private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };
76-
77-
private bool IsValidInterface(Type type)
78-
{
79-
if (type.GetTypeInfo().IsGenericType)
80-
{
81-
return HandlerTypes.Contains(type.GetGenericTypeDefinition());
82-
}
83-
return HandlerTypes.Contains(type);
84-
}
85-
86-
private Type GetHandlerInterface(Type type)
87-
{
88-
return type?.GetTypeInfo()
89-
.ImplementedInterfaces
90-
.First(IsValidInterface);
91-
}
92-
9375
private Type UnwrapGenericType(Type genericType, Type type)
9476
{
9577
return type?.GetTypeInfo()

src/Lsp/HandlerDescriptor.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ public HandlerDescriptor(string method, string key, IJsonRpcHandler handler, Typ
3636

3737
public Registration Registration
3838
{
39-
get {
39+
get
40+
{
4041
if (!HasRegistration) return null;
4142
if (_registration != null) return _registration;
4243

@@ -47,7 +48,8 @@ public Registration Registration
4748
.MakeGenericMethod(RegistrationType)
4849
.Invoke(this, new object[] { Handler });
4950

50-
return _registration = new Registration() {
51+
return _registration = new Registration()
52+
{
5153
Id = Guid.NewGuid().ToString(),
5254
Method = Method,
5355
RegisterOptions = options

src/Lsp/HandlerTypeHelpers.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Linq;
3+
using System.Reflection;
4+
using OmniSharp.Extensions.JsonRpc;
5+
6+
namespace OmniSharp.Extensions.LanguageServer
7+
{
8+
public static class HandlerTypeHelpers
9+
{
10+
private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };
11+
12+
private static bool IsValidInterface(Type type)
13+
{
14+
if (type.GetTypeInfo().IsGenericType)
15+
{
16+
return HandlerTypes.Contains(type.GetGenericTypeDefinition());
17+
}
18+
return HandlerTypes.Contains(type);
19+
}
20+
21+
public static Type GetHandlerInterface(Type type)
22+
{
23+
return type?.GetTypeInfo()
24+
.ImplementedInterfaces
25+
.First(IsValidInterface);
26+
}
27+
}
28+
}

src/Lsp/LanguageServer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ public Task Handle()
196196

197197
private bool HasHandler<T>()
198198
{
199-
return _collection.Any(z => z.HandlerType == typeof(T));
199+
return _collection.Any(z => z.Handler is T);
200200
}
201201

202202
private T GetOptions<O, T>(Func<O, T> action)

0 commit comments

Comments
 (0)