|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Reflection; |
| 4 | +using OmniSharp.Extensions.JsonRpc; |
| 5 | +using OmniSharp.Extensions.LanguageServer.Abstractions; |
| 6 | +using OmniSharp.Extensions.LanguageServer.Capabilities.Client; |
| 7 | + |
| 8 | +namespace OmniSharp.Extensions.LanguageServer |
| 9 | +{ |
| 10 | + internal class ClientCapabilityProvider |
| 11 | + { |
| 12 | + private readonly IHandlerCollection _collection; |
| 13 | + |
| 14 | + public ClientCapabilityProvider(IHandlerCollection collection) |
| 15 | + { |
| 16 | + _collection = collection; |
| 17 | + } |
| 18 | + |
| 19 | + public bool HasHandler<T>(Supports<T> capability) |
| 20 | + where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler> |
| 21 | + { |
| 22 | + if (!capability.IsSupported) return false; |
| 23 | + if (capability.Value == null) return false; |
| 24 | + if (capability.Value.DynamicRegistration) return false; |
| 25 | + |
| 26 | + var handlerType = typeof(T).GetTypeInfo().ImplementedInterfaces |
| 27 | + .Single(x => x.GetTypeInfo().IsGenericType && x.GetTypeInfo().GetGenericTypeDefinition() == typeof(ConnectedCapability<>)) |
| 28 | + .GetTypeInfo().GetGenericArguments()[0].GetTypeInfo(); |
| 29 | + return !capability.Value.DynamicRegistration && _collection.Any(z => z.HandlerType.GetTypeInfo().IsAssignableFrom(handlerType)); |
| 30 | + } |
| 31 | + |
| 32 | + public IOptionsGetter GetOptions<T>(Supports<T> capability) |
| 33 | + where T : DynamicCapability, ConnectedCapability<IJsonRpcHandler> |
| 34 | + { |
| 35 | + return !HasHandler(capability) ? Null : new OptionsGetter(_collection); |
| 36 | + } |
| 37 | + |
| 38 | + private static readonly IOptionsGetter Null = new NullOptionsGetter(); |
| 39 | + |
| 40 | + public interface IOptionsGetter |
| 41 | + { |
| 42 | + TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action) |
| 43 | + where TOptions : class; |
| 44 | + } |
| 45 | + |
| 46 | + private class NullOptionsGetter : IOptionsGetter |
| 47 | + { |
| 48 | + public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action) |
| 49 | + where TOptions : class |
| 50 | + { |
| 51 | + return null; |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private class OptionsGetter : IOptionsGetter |
| 56 | + { |
| 57 | + private readonly IHandlerCollection _collection; |
| 58 | + |
| 59 | + public OptionsGetter(IHandlerCollection collection) |
| 60 | + { |
| 61 | + _collection = collection; |
| 62 | + } |
| 63 | + |
| 64 | + public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action) |
| 65 | + where TOptions : class |
| 66 | + { |
| 67 | + return _collection |
| 68 | + .Select(x => x.Registration?.RegisterOptions is TInterface cl ? action(cl) : null) |
| 69 | + .FirstOrDefault(x => x != null); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments