Skip to content

Refactored static HandlerTypeDescriptorHelper to injected as HandlerTypeDescriptorProvider #319

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/Client/LanguageClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Client.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Shared;

namespace OmniSharp.Extensions.LanguageServer.Client
{
public class LanguageClientOptions : LanguageProtocolRpcOptionsBase<LanguageClientOptions>, ILanguageClientRegistry
{
public LanguageClientOptions()
{
WithAssemblies(typeof(LanguageClientOptions).Assembly, typeof(LspRequestRouter).Assembly);
}
public ClientCapabilities ClientCapabilities { get; set; } = new ClientCapabilities {
Experimental = new Dictionary<string, JToken>(),
Window = new WindowClientCapabilities(),
Expand All @@ -36,8 +41,6 @@ public string RootPath

public object InitializationOptions { get; set; }

public ILspClientReceiver Receiver { get; set; } = new LspClientReceiver();

ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHandler(string method, IJsonRpcHandler handler, JsonRpcHandlerOptions options) =>
AddHandler(method, handler, options);

Expand Down
2 changes: 1 addition & 1 deletion src/Client/LanguageClientOptionsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public static LanguageClientOptions WithReceiver(
ILspClientReceiver serverReceiver
)
{
options.Receiver = serverReceiver;
options.Services.AddSingleton(serverReceiver);
return options;
}

Expand Down
13 changes: 9 additions & 4 deletions src/Client/LanguageClientRegistrationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ namespace OmniSharp.Extensions.LanguageServer.Client
internal class LanguageClientRegistrationManager : IRegisterCapabilityHandler, IUnregisterCapabilityHandler, IRegistrationManager, IDisposable
{
private readonly ISerializer _serializer;
private readonly ILspHandlerTypeDescriptorProvider _handlerTypeDescriptorProvider;
private readonly ILogger<LanguageClientRegistrationManager> _logger;
private readonly ConcurrentDictionary<string, Registration> _registrations;
private readonly ReplaySubject<IEnumerable<Registration>> _registrationSubject;

public LanguageClientRegistrationManager(ISerializer serializer, ILogger<LanguageClientRegistrationManager> logger)
public LanguageClientRegistrationManager(
ISerializer serializer,
ILspHandlerTypeDescriptorProvider handlerTypeDescriptorProvider,
ILogger<LanguageClientRegistrationManager> logger)
{
_serializer = serializer;
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
_logger = logger;
_registrations = new ConcurrentDictionary<string, Registration>(StringComparer.OrdinalIgnoreCase);
_registrationSubject = new ReplaySubject<IEnumerable<Registration>>(1);
Expand Down Expand Up @@ -65,7 +70,7 @@ public void RegisterCapabilities(ServerCapabilities serverCapabilities)
serverCapabilities
))
{
var method = LspHandlerTypeDescriptorHelper.GetMethodForRegistrationOptions(registrationOptions);
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
if (method == null)
{
_logger.LogWarning("Unable to find method for given {@RegistrationOptions}", registrationOptions);
Expand All @@ -91,7 +96,7 @@ public void RegisterCapabilities(ServerCapabilities serverCapabilities)
.Workspace
))
{
var method = LspHandlerTypeDescriptorHelper.GetMethodForRegistrationOptions(registrationOptions);
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
if (method == null)
{
// TODO: Log this
Expand All @@ -117,7 +122,7 @@ private void Register(params Registration[] registrations)

private void Register(Registration registration)
{
var registrationType = LspHandlerTypeDescriptorHelper.GetRegistrationType(registration.Method);
var registrationType = _handlerTypeDescriptorProvider.GetRegistrationType(registration.Method);
if (registrationType == null)
{
_registrations.AddOrUpdate(registration.Id, x => registration, (a, b) => registration);
Expand Down
11 changes: 5 additions & 6 deletions src/Client/LanguageClientServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,14 @@ public static class LanguageClientServiceCollectionExtensions
{
internal static IContainer AddLanguageClientInternals(this IContainer container, LanguageClientOptions options, IServiceProvider outerServiceProvider)
{
if (options.Receiver == null)
{
throw new ArgumentException("Receiver is missing!", nameof(options));
}

container = container.AddLanguageProtocolInternals(options);

container.RegisterInstance(options.ClientCapabilities);
container.RegisterInstanceMany(options.Receiver);
container.RegisterMany<LspClientReceiver>(
reuse: Reuse.Singleton,
nonPublicServiceTypes: true,
ifAlreadyRegistered: IfAlreadyRegistered.Keep
);
if (options.OnUnhandledException != null)
{
container.RegisterInstance(options.OnUnhandledException);
Expand Down
11 changes: 9 additions & 2 deletions src/Client/LspClientReceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,21 @@
using OmniSharp.Extensions.JsonRpc.Client;
using OmniSharp.Extensions.JsonRpc.Server;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Shared;
using OmniSharp.Extensions.LanguageServer.Protocol.Window;

namespace OmniSharp.Extensions.LanguageServer.Client
{
public class LspClientReceiver : Receiver, ILspClientReceiver
{
private readonly ILspHandlerTypeDescriptorProvider _handlerTypeDescriptorProvider;
private bool _initialized;

public LspClientReceiver(ILspHandlerTypeDescriptorProvider handlerTypeDescriptorProvider)
{
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
}

public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToken container)
{
if (_initialized) return base.GetRequests(container);
Expand All @@ -23,7 +30,7 @@ public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToke
foreach (var item in results)
{
if (item.IsRequest &&
HandlerTypeDescriptorHelper.IsMethodName(item.Request.Method, typeof(IShowMessageRequestHandler)))
_handlerTypeDescriptorProvider.IsMethodName(item.Request.Method, typeof(IShowMessageRequestHandler)))
{
newResults.Add(item);
}
Expand All @@ -32,7 +39,7 @@ public override (IEnumerable<Renor> results, bool hasResponse) GetRequests(JToke
newResults.Add(item);
}
else if (item.IsNotification &&
HandlerTypeDescriptorHelper.IsMethodName(
_handlerTypeDescriptorProvider.IsMethodName(
item.Notification.Method,
typeof(IShowMessageHandler),
typeof(ILogMessageHandler),
Expand Down
5 changes: 5 additions & 0 deletions src/Dap.Client/DebugAdapterClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.DebugAdapter.Protocol.Client;
using OmniSharp.Extensions.DebugAdapter.Protocol.Requests;
using OmniSharp.Extensions.DebugAdapter.Shared;
using OmniSharp.Extensions.JsonRpc;

namespace OmniSharp.Extensions.DebugAdapter.Client
{
public class DebugAdapterClientOptions : DebugAdapterRpcOptionsBase<DebugAdapterClientOptions>, IDebugAdapterClientRegistry, IInitializeRequestArguments
{
public DebugAdapterClientOptions()
{
WithAssemblies(typeof(DebugAdapterClientOptions).Assembly, typeof(DebugAdapterRequestRouter).Assembly);
}
public override IRequestProcessIdentifier RequestProcessIdentifier { get; set; } = new ParallelRequestProcessIdentifier();
public string ClientId { get; set; }
public string ClientName { get; set; }
Expand Down
7 changes: 6 additions & 1 deletion src/Dap.Protocol/DebugAdapterRpcOptionsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ namespace OmniSharp.Extensions.DebugAdapter.Protocol
{
public abstract class DebugAdapterRpcOptionsBase<T> : JsonRpcServerOptionsBase<T> where T : IJsonRpcHandlerRegistry<T>
{
public DebugAdapterRpcOptionsBase() => Services.AddLogging(builder => LoggingBuilderAction?.Invoke(builder));
protected DebugAdapterRpcOptionsBase()
{
Services.AddLogging(builder => LoggingBuilderAction?.Invoke(builder));
WithAssemblies(typeof(DebugAdapterRpcOptionsBase<T>).Assembly);
}

public ISerializer Serializer { get; set; } = new DapSerializer();
public override IRequestProcessIdentifier RequestProcessIdentifier { get; set; } = new ParallelRequestProcessIdentifier();
internal bool AddDefaultLoggingProvider { get; set; }
Expand Down
5 changes: 5 additions & 0 deletions src/Dap.Server/DebugAdapterServerOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.DebugAdapter.Protocol.Models;
using OmniSharp.Extensions.DebugAdapter.Protocol.Server;
using OmniSharp.Extensions.DebugAdapter.Shared;
using OmniSharp.Extensions.JsonRpc;

namespace OmniSharp.Extensions.DebugAdapter.Server
{
public class DebugAdapterServerOptions : DebugAdapterRpcOptionsBase<DebugAdapterServerOptions>, IDebugAdapterServerRegistry
{
public DebugAdapterServerOptions()
{
WithAssemblies(typeof(DebugAdapterServerOptions).Assembly, typeof(DebugAdapterRequestRouter).Assembly);
}
public Capabilities Capabilities { get; set; } = new Capabilities();

IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.AddHandler(string method, IJsonRpcHandler handler, JsonRpcHandlerOptions options) =>
Expand Down
11 changes: 8 additions & 3 deletions src/Dap.Shared/DebugAdapterHandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@ internal class DebugAdapterHandlerCollection : IEnumerable<IHandlerDescriptor>,
{
private ImmutableHashSet<HandlerDescriptor> _descriptors = ImmutableHashSet<HandlerDescriptor>.Empty;
private readonly IServiceProvider _serviceProvider;
private readonly IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> _handlerTypeDescriptorProvider;

public IEnumerable<IHandlerDescriptor> Descriptors => _descriptors;

public DebugAdapterHandlerCollection(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
public DebugAdapterHandlerCollection(IServiceProvider serviceProvider, IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> handlerTypeDescriptorProvider)
{
_serviceProvider = serviceProvider;
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
}

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

Expand Down Expand Up @@ -107,7 +112,7 @@ private CompositeDisposable AddHandler(IJsonRpcHandler handler, JsonRpcHandlerOp
var cd = new CompositeDisposable();
foreach (var (method, implementedInterface) in handler.GetType().GetTypeInfo()
.ImplementedInterfaces
.Select(x => ( method: HandlerTypeDescriptorHelper.GetMethodName(x), implementedInterface: x ))
.Select(x => ( method: _handlerTypeDescriptorProvider.GetMethodName(x), implementedInterface: x ))
.Distinct(new EqualityComparer())
.Where(x => !string.IsNullOrWhiteSpace(x.method))
)
Expand All @@ -122,7 +127,7 @@ private CompositeDisposable AddHandler(IJsonRpcHandler handler, JsonRpcHandlerOp

private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRpcHandler handler, JsonRpcHandlerOptions options)
{
var typeDescriptor = HandlerTypeDescriptorHelper.GetHandlerTypeDescriptor(handlerType);
var typeDescriptor = _handlerTypeDescriptorProvider.GetHandlerTypeDescriptor(handlerType);
var @interface = HandlerTypeDescriptorHelper.GetHandlerInterface(handlerType);

return GetDescriptor(method, handlerType, handler, options, typeDescriptor, @interface);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Reflection;
using DryIoc;
using OmniSharp.Extensions.DebugAdapter.Protocol;
using OmniSharp.Extensions.JsonRpc;
Expand All @@ -15,6 +17,7 @@ internal static IContainer AddDebugAdapterProtocolInternals<T>(this IContainer c
}

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

container.RegisterInstanceMany(options.Serializer);
container.RegisterInstance(options.RequestProcessIdentifier);
Expand Down
4 changes: 4 additions & 0 deletions src/JsonRpc.Testing/JsonRpcTestOptions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;

Expand All @@ -25,5 +28,6 @@ public JsonRpcTestOptions(ILoggerFactory clientLoggerFactory, ILoggerFactory ser
public TimeSpan Timeout { get; internal set; } = TimeSpan.FromMilliseconds(500);
public TimeSpan CancellationTimeout { get; internal set; } = TimeSpan.FromMinutes(5);
public PipeOptions DefaultPipeOptions { get; internal set; } = new PipeOptions();
public IEnumerable<Assembly> Assemblies { get; set; } = Enumerable.Empty<Assembly>();
}
}
15 changes: 15 additions & 0 deletions src/JsonRpc.Testing/JsonRpcTestOptionsExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO.Pipelines;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;

namespace OmniSharp.Extensions.JsonRpc.Testing
Expand Down Expand Up @@ -41,5 +44,17 @@ public static JsonRpcTestOptions WithDefaultPipeOptions(this JsonRpcTestOptions
options.DefaultPipeOptions = pipeOptions;
return options;
}

public static JsonRpcTestOptions WithAssemblies(this JsonRpcTestOptions options, IEnumerable<Assembly> assemblies)
{
options.Assemblies = options.Assemblies.Concat(assemblies).ToArray();
return options;
}

public static JsonRpcTestOptions WithAssemblies(this JsonRpcTestOptions options, params Assembly[] assemblies)
{
options.Assemblies = options.Assemblies.Concat(assemblies).ToArray();
return options;
}
}
}
12 changes: 9 additions & 3 deletions src/JsonRpc/HandlerCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ namespace OmniSharp.Extensions.JsonRpc
internal class HandlerCollection : IHandlersManager, IEnumerable<IHandlerDescriptor>
{
private readonly IServiceProvider _serviceProvider;
private readonly IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> _handlerTypeDescriptorProvider;
private ImmutableArray<IHandlerDescriptor> _descriptors = ImmutableArray<IHandlerDescriptor>.Empty;

public IEnumerable<IHandlerDescriptor> Descriptors => _descriptors;

public HandlerCollection(IServiceProvider serviceProvider) => _serviceProvider = serviceProvider;
public HandlerCollection(IServiceProvider serviceProvider, IHandlerTypeDescriptorProvider<IHandlerTypeDescriptor> handlerTypeDescriptorProvider)
{
_serviceProvider = serviceProvider;
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
}

private void Remove(IJsonRpcHandler handler)
{
Expand All @@ -36,13 +41,13 @@ public IDisposable Add(params IJsonRpcHandler[] handlers)
foreach (var handler in handlers)
{
if (_descriptors.Any(z => z.Handler == handler)) continue;
cd.Add(Add(HandlerTypeDescriptorHelper.GetMethodName(handler.GetType()), handler, null));
cd.Add(Add(_handlerTypeDescriptorProvider.GetMethodName(handler.GetType()), handler, null));
}

return cd;
}

public IDisposable Add(IJsonRpcHandler handler, JsonRpcHandlerOptions options) => Add(HandlerTypeDescriptorHelper.GetMethodName(handler.GetType()), handler, options);
public IDisposable Add(IJsonRpcHandler handler, JsonRpcHandlerOptions options) => Add(_handlerTypeDescriptorProvider.GetMethodName(handler.GetType()), handler, options);

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

var requestProcessType =
options?.RequestProcessType ??
_handlerTypeDescriptorProvider.GetHandlerTypeDescriptor(type)?.RequestProcessType ??
type.GetCustomAttributes(true)
.Concat(@interface.GetCustomAttributes(true))
.OfType<ProcessAttribute>()
Expand Down
Loading