-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathClientCapabilityProvider.cs
120 lines (104 loc) · 4.66 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
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
112
113
114
115
116
117
118
119
120
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Server.Abstractions;
namespace OmniSharp.Extensions.LanguageServer.Server
{
internal class ClientCapabilityProvider
{
private readonly IHandlerCollection _collection;
public ClientCapabilityProvider(IHandlerCollection collection)
{
_collection = collection;
}
public bool HasStaticHandler<T>(Supports<T> capability)
where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler>
{
if (!capability.IsSupported) return false;
if (capability.Value == null) return false;
if (capability.Value.DynamicRegistration == true) 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 == true && _collection.ContainsHandler(handlerType);
}
public IOptionsGetter GetStaticOptions<T>(Supports<T> capability)
where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler>
{
return !HasStaticHandler(capability) ? Null : new OptionsGetter(_collection);
}
private static readonly IOptionsGetter Null = new NullOptionsGetter();
public interface IOptionsGetter
{
/// <summary>
/// Gets a single option from a given interface.
/// </summary>
/// <param name="action"></param>
/// <typeparam name="TInterface"></typeparam>
/// <typeparam name="TOptions"></typeparam>
/// <returns></returns>
TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class;
/// <summary>
/// Reduces the options from multiple interfaces to a single option.
/// </summary>
/// <typeparam name="TInterface"></typeparam>
/// <typeparam name="TOptions"></typeparam>
/// <param name="action"></param>
/// <returns></returns>
TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<TInterface>, TOptions> action)
where TOptions : class;
}
private class NullOptionsGetter : IOptionsGetter
{
public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
return null;
}
public TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<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);
}
public Supports<TOptions> Can<TInterface, TOptions>(Func<TInterface, TOptions> action)
where TOptions : class
{
var options = _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
if (options == null)
return Supports.OfBoolean<TOptions>(false);
return _collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null)
.FirstOrDefault(x => x != null);
}
public TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<TInterface>, TOptions> action)
where TOptions : class
{
return action(_collection
.Select(x => x.Registration?.RegisterOptions is TInterface cl ? cl : default)
.Where(x => x != null));
}
}
}
}