-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathClientCapabilityProvider.cs
73 lines (62 loc) · 2.6 KB
/
ClientCapabilityProvider.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
using System;
using System.Linq;
using System.Reflection;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Abstractions;
using OmniSharp.Extensions.LanguageServer.Capabilities.Client;
namespace OmniSharp.Extensions.LanguageServer
{
internal class ClientCapabilityProvider
{
private readonly IHandlerCollection _collection;
public ClientCapabilityProvider(IHandlerCollection collection)
{
_collection = collection;
}
public bool HasHandler<T>(Supports<T> capability)
where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler>
{
if (!capability.IsSupported) return false;
if (capability.Value == null) return false;
if (capability.Value.DynamicRegistration) 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 && _collection.Any(z => z.HandlerType.GetTypeInfo().IsAssignableFrom(handlerType));
}
public IOptionsGetter GetOptions<T>(Supports<T> capability)
where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler>
{
return !HasHandler(capability) ? Null : new OptionsGetter(_collection);
}
private static readonly IOptionsGetter Null = new NullOptionsGetter();
public interface IOptionsGetter
{
TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class;
}
private class NullOptionsGetter : IOptionsGetter
{
public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
return null;
}
}
private class OptionsGetter : IOptionsGetter
{
private readonly IHandlerCollection _collection;
public OptionsGetter(IHandlerCollection collection)
{
_collection = collection;
}
public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
return _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
}
}
}
}