-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathHandlerDescriptor.cs
111 lines (93 loc) · 3.55 KB
/
HandlerDescriptor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System;
using System.Reflection;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Abstractions;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
using OmniSharp.Extensions.LanguageServer.Models;
namespace OmniSharp.Extensions.LanguageServer
{
class HandlerDescriptor : ILspHandlerDescriptor, IDisposable
{
private readonly Action _disposeAction;
public HandlerDescriptor(string method, string key, IJsonRpcHandler handler, Type handlerType, Type @params, Type registrationType, Type capabilityType, Action disposeAction)
{
_disposeAction = disposeAction;
Handler = handler;
Method = method;
Key = key;
HandlerType = handlerType;
Params = @params;
RegistrationType = registrationType;
CapabilityType = capabilityType;
}
public IJsonRpcHandler Handler { get; }
public Type HandlerType { get; }
public bool HasRegistration => RegistrationType != null;
public Type RegistrationType { get; }
public bool HasCapability => CapabilityType != null;
public Type CapabilityType { get; }
private Registration _registration;
public Registration Registration
{
get
{
if (!HasRegistration) return null;
if (_registration != null) return _registration;
// TODO: Cache this
var options = GetType()
.GetTypeInfo()
.GetMethod(nameof(GetRegistration), BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(RegistrationType)
.Invoke(this, new object[] { Handler });
return _registration = new Registration()
{
Id = Guid.NewGuid().ToString(),
Method = Method,
RegisterOptions = options
};
}
}
public void SetCapability(object instance)
{
if (instance is DynamicCapability dc)
{
AllowsDynamicRegistration = dc.DynamicRegistration;
}
// TODO: Cache this
GetType()
.GetTypeInfo()
.GetMethod(nameof(SetCapability), BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(CapabilityType)
.Invoke(this, new[] { Handler, instance });
}
public string Method { get; }
public string Key { get; }
public Type Params { get; }
public bool IsDynamicCapability => typeof(DynamicCapability).GetTypeInfo().IsAssignableFrom(CapabilityType);
public bool AllowsDynamicRegistration { get; private set; }
public void Dispose()
{
_disposeAction();
}
private static object GetRegistration<T>(IRegistration<T> registration)
{
return registration.GetRegistrationOptions();
}
private static void SetCapability<T>(ICapability<T> capability, T instance)
{
capability.SetCapability(instance);
}
public override bool Equals(object obj)
{
if (obj is HandlerDescriptor handler)
{
return handler.HandlerType == HandlerType && handler.Key == Key;
}
return false;
}
public override int GetHashCode()
{
return Tuple.Create(HandlerType, Key).GetHashCode();
}
}
}