Skip to content

Commit 8411ac4

Browse files
Refactored static HandlerTypeDescriptorHelper to injected as HandlerTypeDescriptorProvider (#319)
1 parent 5571121 commit 8411ac4

File tree

45 files changed

+530
-264
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+530
-264
lines changed

src/Client/LanguageClientOptions.cs

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,16 @@
88
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
99
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
1010
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
11+
using OmniSharp.Extensions.LanguageServer.Shared;
1112

1213
namespace OmniSharp.Extensions.LanguageServer.Client
1314
{
1415
public class LanguageClientOptions : LanguageProtocolRpcOptionsBase<LanguageClientOptions>, ILanguageClientRegistry
1516
{
17+
public LanguageClientOptions()
18+
{
19+
WithAssemblies(typeof(LanguageClientOptions).Assembly, typeof(LspRequestRouter).Assembly);
20+
}
1621
public ClientCapabilities ClientCapabilities { get; set; } = new ClientCapabilities {
1722
Experimental = new Dictionary<string, JToken>(),
1823
Window = new WindowClientCapabilities(),
@@ -36,8 +41,6 @@ public string RootPath
3641

3742
public object InitializationOptions { get; set; }
3843

39-
public ILspClientReceiver Receiver { get; set; } = new LspClientReceiver();
40-
4144
ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHandler(string method, IJsonRpcHandler handler, JsonRpcHandlerOptions options) =>
4245
AddHandler(method, handler, options);
4346

src/Client/LanguageClientOptionsExtensions.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static LanguageClientOptions WithReceiver(
2323
ILspClientReceiver serverReceiver
2424
)
2525
{
26-
options.Receiver = serverReceiver;
26+
options.Services.AddSingleton(serverReceiver);
2727
return options;
2828
}
2929

src/Client/LanguageClientRegistrationManager.cs

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,18 @@ namespace OmniSharp.Extensions.LanguageServer.Client
2222
internal class LanguageClientRegistrationManager : IRegisterCapabilityHandler, IUnregisterCapabilityHandler, IRegistrationManager, IDisposable
2323
{
2424
private readonly ISerializer _serializer;
25+
private readonly ILspHandlerTypeDescriptorProvider _handlerTypeDescriptorProvider;
2526
private readonly ILogger<LanguageClientRegistrationManager> _logger;
2627
private readonly ConcurrentDictionary<string, Registration> _registrations;
2728
private readonly ReplaySubject<IEnumerable<Registration>> _registrationSubject;
2829

29-
public LanguageClientRegistrationManager(ISerializer serializer, ILogger<LanguageClientRegistrationManager> logger)
30+
public LanguageClientRegistrationManager(
31+
ISerializer serializer,
32+
ILspHandlerTypeDescriptorProvider handlerTypeDescriptorProvider,
33+
ILogger<LanguageClientRegistrationManager> logger)
3034
{
3135
_serializer = serializer;
36+
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
3237
_logger = logger;
3338
_registrations = new ConcurrentDictionary<string, Registration>(StringComparer.OrdinalIgnoreCase);
3439
_registrationSubject = new ReplaySubject<IEnumerable<Registration>>(1);
@@ -65,7 +70,7 @@ public void RegisterCapabilities(ServerCapabilities serverCapabilities)
6570
serverCapabilities
6671
))
6772
{
68-
var method = LspHandlerTypeDescriptorHelper.GetMethodForRegistrationOptions(registrationOptions);
73+
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
6974
if (method == null)
7075
{
7176
_logger.LogWarning("Unable to find method for given {@RegistrationOptions}", registrationOptions);
@@ -91,7 +96,7 @@ public void RegisterCapabilities(ServerCapabilities serverCapabilities)
9196
.Workspace
9297
))
9398
{
94-
var method = LspHandlerTypeDescriptorHelper.GetMethodForRegistrationOptions(registrationOptions);
99+
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
95100
if (method == null)
96101
{
97102
// TODO: Log this
@@ -117,7 +122,7 @@ private void Register(params Registration[] registrations)
117122

118123
private void Register(Registration registration)
119124
{
120-
var registrationType = LspHandlerTypeDescriptorHelper.GetRegistrationType(registration.Method);
125+
var registrationType = _handlerTypeDescriptorProvider.GetRegistrationType(registration.Method);
121126
if (registrationType == null)
122127
{
123128
_registrations.AddOrUpdate(registration.Id, x => registration, (a, b) => registration);

src/Client/LanguageClientServiceCollectionExtensions.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ public static class LanguageClientServiceCollectionExtensions
1818
{
1919
internal static IContainer AddLanguageClientInternals(this IContainer container, LanguageClientOptions options, IServiceProvider outerServiceProvider)
2020
{
21-
if (options.Receiver == null)
22-
{
23-
throw new ArgumentException("Receiver is missing!", nameof(options));
24-
}
25-
2621
container = container.AddLanguageProtocolInternals(options);
2722

2823
container.RegisterInstance(options.ClientCapabilities);
29-
container.RegisterInstanceMany(options.Receiver);
24+
container.RegisterMany<LspClientReceiver>(
25+
reuse: Reuse.Singleton,
26+
nonPublicServiceTypes: true,
27+
ifAlreadyRegistered: IfAlreadyRegistered.Keep
28+
);
3029
if (options.OnUnhandledException != null)
3130
{
3231
container.RegisterInstance(options.OnUnhandledException);

src/Client/LspClientReceiver.cs

+9-2
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,21 @@
44
using OmniSharp.Extensions.JsonRpc.Client;
55
using OmniSharp.Extensions.JsonRpc.Server;
66
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
7+
using OmniSharp.Extensions.LanguageServer.Protocol.Shared;
78
using OmniSharp.Extensions.LanguageServer.Protocol.Window;
89

910
namespace OmniSharp.Extensions.LanguageServer.Client
1011
{
1112
public class LspClientReceiver : Receiver, ILspClientReceiver
1213
{
14+
private readonly ILspHandlerTypeDescriptorProvider _handlerTypeDescriptorProvider;
1315
private bool _initialized;
1416

17+
public LspClientReceiver(ILspHandlerTypeDescriptorProvider handlerTypeDescriptorProvider)
18+
{
19+
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
20+
}
21+
1522
public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToken container)
1623
{
1724
if (_initialized) return base.GetRequests(container);
@@ -23,7 +30,7 @@ public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToke
2330
foreach (var item in results)
2431
{
2532
if (item.IsRequest &&
26-
HandlerTypeDescriptorHelper.IsMethodName(item.Request.Method, typeof(IShowMessageRequestHandler)))
33+
_handlerTypeDescriptorProvider.IsMethodName(item.Request.Method, typeof(IShowMessageRequestHandler)))
2734
{
2835
newResults.Add(item);
2936
}
@@ -32,7 +39,7 @@ public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToke
3239
newResults.Add(item);
3340
}
3441
else if (item.IsNotification &&
35-
HandlerTypeDescriptorHelper.IsMethodName(
42+
_handlerTypeDescriptorProvider.IsMethodName(
3643
item.Notification.Method,
3744
typeof(IShowMessageHandler),
3845
typeof(ILogMessageHandler),

src/Dap.Client/DebugAdapterClientOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
using OmniSharp.Extensions.DebugAdapter.Protocol;
66
using OmniSharp.Extensions.DebugAdapter.Protocol.Client;
77
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
8+
using OmniSharp.Extensions.DebugAdapter.Shared;
89
using OmniSharp.Extensions.JsonRpc;
910

1011
namespace OmniSharp.Extensions.DebugAdapter.Client
1112
{
1213
public class DebugAdapterClientOptions : DebugAdapterRpcOptionsBase<DebugAdapterClientOptions>, IDebugAdapterClientRegistry, IInitializeRequestArguments
1314
{
15+
public DebugAdapterClientOptions()
16+
{
17+
WithAssemblies(typeof(DebugAdapterClientOptions).Assembly, typeof(DebugAdapterRequestRouter).Assembly);
18+
}
1419
public override IRequestProcessIdentifier RequestProcessIdentifier { get; set; } = new ParallelRequestProcessIdentifier();
1520
public string ClientId { get; set; }
1621
public string ClientName { get; set; }

src/Dap.Protocol/DebugAdapterRpcOptionsBase.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@ namespace OmniSharp.Extensions.DebugAdapter.Protocol
88
{
99
public abstract class DebugAdapterRpcOptionsBase<T> : JsonRpcServerOptionsBase<T> where T : IJsonRpcHandlerRegistry<T>
1010
{
11-
public DebugAdapterRpcOptionsBase() => Services.AddLogging(builder => LoggingBuilderAction?.Invoke(builder));
11+
protected DebugAdapterRpcOptionsBase()
12+
{
13+
Services.AddLogging(builder => LoggingBuilderAction?.Invoke(builder));
14+
WithAssemblies(typeof(DebugAdapterRpcOptionsBase<T>).Assembly);
15+
}
16+
1217
public ISerializer Serializer { get; set; } = new DapSerializer();
1318
public override IRequestProcessIdentifier RequestProcessIdentifier { get; set; } = new ParallelRequestProcessIdentifier();
1419
internal bool AddDefaultLoggingProvider { get; set; }

src/Dap.Server/DebugAdapterServerOptions.cs

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
using OmniSharp.Extensions.DebugAdapter.Protocol;
66
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
77
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
8+
using OmniSharp.Extensions.DebugAdapter.Shared;
89
using OmniSharp.Extensions.JsonRpc;
910

1011
namespace OmniSharp.Extensions.DebugAdapter.Server
1112
{
1213
public class DebugAdapterServerOptions : DebugAdapterRpcOptionsBase<DebugAdapterServerOptions>, IDebugAdapterServerRegistry
1314
{
15+
public DebugAdapterServerOptions()
16+
{
17+
WithAssemblies(typeof(DebugAdapterServerOptions).Assembly, typeof(DebugAdapterRequestRouter).Assembly);
18+
}
1419
public Capabilities Capabilities { get; set; } = new Capabilities();
1520

1621
IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.AddHandler(string method, IJsonRpcHandler handler, JsonRpcHandlerOptions options) =>

src/Dap.Shared/DebugAdapterHandlerCollection.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ internal class DebugAdapterHandlerCollection : IEnumerable<IHandlerDescriptor>,
1616
{
1717
private ImmutableHashSet<HandlerDescriptor> _descriptors = ImmutableHashSet<HandlerDescriptor>.Empty;
1818
private readonly IServiceProvider _serviceProvider;
19+
private readonly IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> _handlerTypeDescriptorProvider;
1920

2021
public IEnumerable<IHandlerDescriptor> Descriptors => _descriptors;
2122

22-
public DebugAdapterHandlerCollection(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
23+
public DebugAdapterHandlerCollection(IServiceProvider serviceProvider, IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> handlerTypeDescriptorProvider)
24+
{
25+
_serviceProvider = serviceProvider;
26+
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
27+
}
2328

2429
public IEnumerator<IHandlerDescriptor> GetEnumerator() => _descriptors.GetEnumerator();
2530

@@ -107,7 +112,7 @@ private CompositeDisposable AddHandler(IJsonRpcHandler handler, JsonRpcHandlerOp
107112
var cd = new CompositeDisposable();
108113
foreach (var (method, implementedInterface) in handler.GetType().GetTypeInfo()
109114
.ImplementedInterfaces
110-
.Select(x => ( method: HandlerTypeDescriptorHelper.GetMethodName(x), implementedInterface: x ))
115+
.Select(x => ( method: _handlerTypeDescriptorProvider.GetMethodName(x), implementedInterface: x ))
111116
.Distinct(new EqualityComparer())
112117
.Where(x => !string.IsNullOrWhiteSpace(x.method))
113118
)
@@ -122,7 +127,7 @@ private CompositeDisposable AddHandler(IJsonRpcHandler handler, JsonRpcHandlerOp
122127

123128
private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRpcHandler handler, JsonRpcHandlerOptions options)
124129
{
125-
var typeDescriptor = HandlerTypeDescriptorHelper.GetHandlerTypeDescriptor(handlerType);
130+
var typeDescriptor = _handlerTypeDescriptorProvider.GetHandlerTypeDescriptor(handlerType);
126131
var @interface = HandlerTypeDescriptorHelper.GetHandlerInterface(handlerType);
127132

128133
return GetDescriptor(method, handlerType, handler, options, typeDescriptor, @interface);

src/Dap.Shared/DebugAdapterProtocolServiceCollectionExtensions.cs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
using System;
2+
using System.Collections.Generic;
3+
using System.Reflection;
24
using DryIoc;
35
using OmniSharp.Extensions.DebugAdapter.Protocol;
46
using OmniSharp.Extensions.JsonRpc;
@@ -15,6 +17,7 @@ internal static IContainer AddDebugAdapterProtocolInternals<T>(this IContainer c
1517
}
1618

1719
container = container.AddJsonRpcServerCore(options);
20+
container.RegisterInstanceMany(new HandlerTypeDescriptorProvider(options.Assemblies), nonPublicServiceTypes: true);
1821

1922
container.RegisterInstanceMany(options.Serializer);
2023
container.RegisterInstance(options.RequestProcessIdentifier);

src/JsonRpc.Testing/JsonRpcTestOptions.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO.Pipelines;
4+
using System.Linq;
5+
using System.Reflection;
36
using Microsoft.Extensions.Logging;
47
using Microsoft.Extensions.Logging.Abstractions;
58

@@ -25,5 +28,6 @@ public JsonRpcTestOptions(ILoggerFactory clientLoggerFactory, ILoggerFactory ser
2528
public TimeSpan Timeout { get; internal set; } = TimeSpan.FromMilliseconds(500);
2629
public TimeSpan CancellationTimeout { get; internal set; } = TimeSpan.FromMinutes(5);
2730
public PipeOptions DefaultPipeOptions { get; internal set; } = new PipeOptions();
31+
public IEnumerable<Assembly> Assemblies { get; set; } = Enumerable.Empty<Assembly>();
2832
}
2933
}

src/JsonRpc.Testing/JsonRpcTestOptionsExtensions.cs

+15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.IO.Pipelines;
4+
using System.Linq;
5+
using System.Reflection;
36
using Microsoft.Extensions.Logging;
47

58
namespace OmniSharp.Extensions.JsonRpc.Testing
@@ -41,5 +44,17 @@ public static JsonRpcTestOptions WithDefaultPipeOptions(this JsonRpcTestOptions
4144
options.DefaultPipeOptions = pipeOptions;
4245
return options;
4346
}
47+
48+
public static JsonRpcTestOptions WithAssemblies(this JsonRpcTestOptions options, IEnumerable<Assembly> assemblies)
49+
{
50+
options.Assemblies = options.Assemblies.Concat(assemblies).ToArray();
51+
return options;
52+
}
53+
54+
public static JsonRpcTestOptions WithAssemblies(this JsonRpcTestOptions options, params Assembly[] assemblies)
55+
{
56+
options.Assemblies = options.Assemblies.Concat(assemblies).ToArray();
57+
return options;
58+
}
4459
}
4560
}

src/JsonRpc/HandlerCollection.cs

+9-3
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ namespace OmniSharp.Extensions.JsonRpc
1313
internal class HandlerCollection : IHandlersManager, IEnumerable<IHandlerDescriptor>
1414
{
1515
private readonly IServiceProvider _serviceProvider;
16+
private readonly IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> _handlerTypeDescriptorProvider;
1617
private ImmutableArray<IHandlerDescriptor> _descriptors = ImmutableArray<IHandlerDescriptor>.Empty;
1718

1819
public IEnumerable<IHandlerDescriptor> Descriptors => _descriptors;
1920

20-
public HandlerCollection(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
21+
public HandlerCollection(IServiceProvider serviceProvider, IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> handlerTypeDescriptorProvider)
22+
{
23+
_serviceProvider = serviceProvider;
24+
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
25+
}
2126

2227
private void Remove(IJsonRpcHandler handler)
2328
{
@@ -36,13 +41,13 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
3641
foreach (var handler in handlers)
3742
{
3843
if (_descriptors.Any(z => z.Handler == handler)) continue;
39-
cd.Add(Add(HandlerTypeDescriptorHelper.GetMethodName(handler.GetType()), handler, null));
44+
cd.Add(Add(_handlerTypeDescriptorProvider.GetMethodName(handler.GetType()), handler, null));
4045
}
4146

4247
return cd;
4348
}
4449

45-
public IDisposable Add(IJsonRpcHandler handler, JsonRpcHandlerOptions options) => Add(HandlerTypeDescriptorHelper.GetMethodName(handler.GetType()), handler, options);
50+
public IDisposable Add(IJsonRpcHandler handler, JsonRpcHandlerOptions options) => Add(_handlerTypeDescriptorProvider.GetMethodName(handler.GetType()), handler, options);
4651

4752
public IDisposable Add(string method, IJsonRpcHandler handler, JsonRpcHandlerOptions options)
4853
{
@@ -64,6 +69,7 @@ public IDisposable Add(string method, IJsonRpcHandler handler, JsonRpcHandlerOpt
6469

6570
var requestProcessType =
6671
options?.RequestProcessType ??
72+
_handlerTypeDescriptorProvider.GetHandlerTypeDescriptor(type)?.RequestProcessType ??
6773
type.GetCustomAttributes(true)
6874
.Concat(@interface.GetCustomAttributes(true))
6975
.OfType<ProcessAttribute>()

0 commit comments

Comments
 (0)