Skip to content

Commit bb2b4bd

Browse files
Added id and AllowsDynamicRegistration to descriptor, removed registration and create the registrations on demand as needed
1 parent a52197f commit bb2b4bd

12 files changed

+40
-56
lines changed

src/JsonRpc/RequestRouterBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public async Task RouteNotification(TDescriptor descriptor, Notification notific
6565
@params = notification.Params?.ToObject(descriptor.Params, _serializer.JsonSerializer);
6666
}
6767

68-
await HandleNotification(mediator, descriptor, @params ?? EmptyRequest.Instance, token);
68+
await HandleNotification(mediator, descriptor, @params ?? Activator.CreateInstance(descriptor.Params), token);
6969
}
7070
}
7171
catch (Exception e)

src/Server/Abstractions/ILspHandlerDescriptor.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ namespace OmniSharp.Extensions.LanguageServer.Server.Abstractions
66
{
77
public interface ILspHandlerDescriptor : IHandlerDescriptor
88
{
9+
Guid Id { get; }
910
bool HasRegistration { get; }
1011
Type RegistrationType { get; }
11-
object RegisterOptions { get; }
12-
Registration Registration { get; }
12+
object RegistrationOptions { get; }
13+
bool AllowsDynamicRegistration { get; }
1314

1415
bool HasCapability { get; }
1516
Type CapabilityType { get; }

src/Server/ClientCapabilityProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,29 +92,29 @@ public TOptions Get<TInterface, TOptions>(Func<TInterface, TOptions> action)
9292
where TOptions : class
9393
{
9494
return _collection
95-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
95+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
9696
.FirstOrDefault(x => x != null);
9797
}
9898

9999
public Supports<TOptions> Can<TInterface, TOptions>(Func<TInterface, TOptions> action)
100100
where TOptions : class
101101
{
102102
var options = _collection
103-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
103+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
104104
.FirstOrDefault(x => x != null);
105105
if (options == null)
106106
return Supports.OfBoolean<TOptions>(false);
107107

108108
return _collection
109-
.Select(x => x.RegisterOptions is TInterface cl ? action(cl) : null)
109+
.Select(x => x.RegistrationOptions is TInterface cl ? action(cl) : null)
110110
.FirstOrDefault(x => x != null);
111111
}
112112

113113
public TOptions Reduce<TInterface, TOptions>(Func<IEnumerable<TInterface>, TOptions> action)
114114
where TOptions : class
115115
{
116116
return action(_collection
117-
.Select(x => x.RegisterOptions is TInterface cl ? cl : default)
117+
.Select(x => x.RegistrationOptions is TInterface cl ? cl : default)
118118
.Where(x => x != null));
119119
}
120120
}

src/Server/HandlerCollection.cs

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
145145

146146
Type @params = null;
147147
object registrationOptions = null;
148-
Registration registration = null;
149148
if (@interface.GetTypeInfo().IsGenericType)
150149
{
151150
@params = @interface.GetTypeInfo().GetGenericArguments()[0];
@@ -156,15 +155,6 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
156155
registrationOptions = GetRegistrationMethod
157156
.MakeGenericMethod(registrationType)
158157
.Invoke(null, new object[] { handler });
159-
160-
if (_supportedCapabilities.AllowsDynamicRegistration(capabilityType))
161-
{
162-
registration = new Registration() {
163-
Id = Guid.NewGuid().ToString(),
164-
Method = method,
165-
RegisterOptions = registrationOptions
166-
};
167-
}
168158
}
169159

170160
var key = "default";
@@ -194,7 +184,7 @@ private HandlerDescriptor GetDescriptor(string method, Type handlerType, IJsonRp
194184
@params,
195185
registrationType,
196186
registrationOptions,
197-
registration,
187+
registrationType != null && _supportedCapabilities.AllowsDynamicRegistration(capabilityType),
198188
capabilityType,
199189
() => {
200190
_handlers.RemoveWhere(d => d.Handler == handler);

src/Server/HandlerDescriptor.cs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@ public HandlerDescriptor(
2222
Type handlerType,
2323
Type @params,
2424
Type registrationType,
25-
object registerOptions,
26-
Registration registration,
25+
object registrationOptions,
26+
bool allowsDynamicRegistration,
2727
Type capabilityType,
2828
Action disposeAction)
2929
{
3030
_disposeAction = disposeAction;
31+
Id = Guid.NewGuid();
3132
Method = method;
3233
Key = key;
3334
ImplementationType = handler.GetType();
@@ -36,8 +37,8 @@ public HandlerDescriptor(
3637
Params = @params;
3738
Response = Response;
3839
RegistrationType = registrationType;
39-
RegisterOptions = registerOptions;
40-
Registration = registration;
40+
RegistrationOptions = registrationOptions;
41+
AllowsDynamicRegistration = allowsDynamicRegistration;
4142
CapabilityType = capabilityType;
4243

4344
var requestInterface = @params?.GetInterfaces()
@@ -65,10 +66,11 @@ public HandlerDescriptor(
6566
public Type ImplementationType { get; }
6667
public Type HandlerType { get; }
6768

69+
public Guid Id { get; }
6870
public bool HasRegistration => RegistrationType != null;
6971
public Type RegistrationType { get; }
70-
public object RegisterOptions { get; }
71-
public Registration Registration { get; }
72+
public object RegistrationOptions { get; }
73+
public bool AllowsDynamicRegistration { get; }
7274

7375
public bool HasCapability => CapabilityType != null;
7476
public Type CapabilityType { get; }

src/Server/ISupportedCapabilities.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ namespace OmniSharp.Extensions.LanguageServer.Server
66
{
77
public interface ISupportedCapabilities
88
{
9-
bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor);
109
bool AllowsDynamicRegistration(Type capabilityType);
1110
void SetCapability(ILspHandlerDescriptor descriptor, IJsonRpcHandler handler);
1211
}

src/Server/LanguageServer.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,8 +328,12 @@ private IDisposable RegisterHandlers(LspHandlerDescriptorDisposable handlerDispo
328328
using (var scope = _serviceProvider.CreateScope())
329329
{
330330
var registrations = handlerDisposable.Descriptors
331-
.Select(x => x.Registration)
332-
.Where(x => x != null)
331+
.Where(d => d.AllowsDynamicRegistration)
332+
.Select(d => new Registration() {
333+
Id = d.Id.ToString(),
334+
Method = d.Method,
335+
RegisterOptions = d.RegistrationOptions
336+
})
333337
.ToArray();
334338

335339
// Fire and forget

src/Server/Matchers/ExecuteCommandMatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public IEnumerable<ILspHandlerDescriptor> FindHandler(object parameters, IEnumer
2828
_logger.LogTrace("Registration options {OptionsName}", executeCommandParams.GetType().FullName);
2929
foreach (var descriptor in descriptors)
3030
{
31-
if (descriptor.RegisterOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
31+
if (descriptor.RegistrationOptions is ExecuteCommandRegistrationOptions registrationOptions && registrationOptions.Commands.Any(x => x == executeCommandParams.Command))
3232
{
3333
_logger.LogTrace("Checking handler {Method}:{Handler}",
3434
executeCommandParams.Command,

src/Server/Matchers/TextDocumentMatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ private IEnumerable<ILspHandlerDescriptor> GetHandler(IEnumerable<ILspHandlerDes
7575
foreach (var descriptor in descriptors)
7676
{
7777
_logger.LogTrace("Checking handler {Method}:{Handler}", method, descriptor.ImplementationType.FullName);
78-
var registrationOptions = descriptor.RegisterOptions as ITextDocumentRegistrationOptions;
78+
var registrationOptions = descriptor.RegistrationOptions as ITextDocumentRegistrationOptions;
7979

8080
_logger.LogTrace("Registration options {OptionsName}", registrationOptions?.GetType().FullName);
8181
_logger.LogTrace("Document Selector {DocumentSelector}", registrationOptions?.DocumentSelector.ToString());

src/Server/SupportedCapabilities.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,6 @@ public void Add(IEnumerable<ISupports> supports)
2828
}
2929
}
3030

31-
public bool AllowsDynamicRegistration(ILspHandlerDescriptor descriptor)
32-
{
33-
if (descriptor.HasCapability && _supports.TryGetValue(descriptor.CapabilityType, out var capability))
34-
{
35-
if (capability is DynamicCapability dc)
36-
return dc.DynamicRegistration;
37-
}
38-
return false;
39-
}
40-
4131
public bool AllowsDynamicRegistration(Type capabilityType)
4232
{
4333
if (_supports.TryGetValue(capabilityType, out var capability))

0 commit comments

Comments
 (0)