-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathLanguageClientRegistrationManager.cs
More file actions
202 lines (177 loc) · 8.19 KB
/
Copy pathLanguageClientRegistrationManager.cs
File metadata and controls
202 lines (177 loc) · 8.19 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System.Collections.Concurrent;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using MediatR;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using OmniSharp.Extensions.JsonRpc;
using OmniSharp.Extensions.LanguageServer.Protocol;
using OmniSharp.Extensions.LanguageServer.Protocol.Client;
using OmniSharp.Extensions.LanguageServer.Protocol.Models;
using OmniSharp.Extensions.LanguageServer.Protocol.Server.Capabilities;
using OmniSharp.Extensions.LanguageServer.Protocol.Shared;
using OmniSharp.Extensions.LanguageServer.Shared;
namespace OmniSharp.Extensions.LanguageServer.Client
{
[BuiltIn]
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 = new ReplaySubject<IEnumerable<Registration>>(1, Scheduler.Immediate);
public LanguageClientRegistrationManager(
ISerializer serializer,
ILspHandlerTypeDescriptorProvider handlerTypeDescriptorProvider,
ILogger<LanguageClientRegistrationManager> logger
)
{
_serializer = serializer;
_handlerTypeDescriptorProvider = handlerTypeDescriptorProvider;
_logger = logger;
_registrations = new ConcurrentDictionary<string, Registration>(StringComparer.OrdinalIgnoreCase);
}
async Task<Unit> IRequestHandler<RegistrationParams, Unit>.Handle(RegistrationParams request, CancellationToken cancellationToken)
{
await Task.Yield();
lock (this)
{
Register(request.Registrations.ToArray());
}
if (!_registrationSubject.IsDisposed)
{
_registrationSubject.OnNext(_registrations.Values);
}
return Unit.Value;
}
async Task<Unit> IRequestHandler<UnregistrationParams, Unit>.Handle(UnregistrationParams request, CancellationToken cancellationToken)
{
await Task.Yield();
lock (this)
{
foreach (var item in request.Unregisterations ?? new UnregistrationContainer())
{
_registrations.TryRemove(item.Id, out _);
}
}
if (!_registrationSubject.IsDisposed)
{
_registrationSubject.OnNext(_registrations.Values);
}
return Unit.Value;
}
public void RegisterCapabilities(ServerCapabilities serverCapabilities)
{
foreach (var registrationOptions in LspHandlerDescriptorHelpers.GetStaticRegistrationOptions(serverCapabilities))
{
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
if (method == null)
{
_logger.LogWarning("Unable to find method for given {@RegistrationOptions}", registrationOptions);
continue;
}
if (registrationOptions.Id != null)
{
var reg = new Registration {
Id = registrationOptions.Id,
Method = method,
RegisterOptions = registrationOptions
};
_registrations.AddOrUpdate(registrationOptions.Id, _ => reg, (_, _) => reg);
}
}
if (serverCapabilities.Workspace != null)
{
foreach (var registrationOptions in LspHandlerDescriptorHelpers.GetStaticRegistrationOptions(serverCapabilities.Workspace))
{
var method = _handlerTypeDescriptorProvider.GetMethodForRegistrationOptions(registrationOptions);
if (method == null)
{
// TODO: Log this
continue;
}
if (registrationOptions.Id != null)
{
var reg = new Registration {
Id = registrationOptions.Id,
Method = method,
RegisterOptions = registrationOptions
};
_registrations.AddOrUpdate(registrationOptions.Id, _ => reg, (_, _) => reg);
}
}
}
_registrationSubject.OnNext(_registrations.Values);
}
private void Register(params Registration[] registrations)
{
var newRegistrations = new List<Registration>();
foreach (var registration in registrations)
{
newRegistrations.Add(Register(registration));
}
foreach (var reg in newRegistrations)
{
_registrations.AddOrUpdate(reg.Id, reg, (_, _) => reg);
}
}
private Registration Register(Registration registration)
{
var registrationType = _handlerTypeDescriptorProvider.GetRegistrationType(registration.Method);
if (registrationType == null)
{
// vscode client throws if given an unknown registration type
_logger.LogError("Unknown Registration Type {Method} {@Registration}", registration.Method, registration);
throw new NotSupportedException($"Unknown Registration Type '{registration.Method}'");
}
var deserializedRegistration = new Registration {
Id = registration.Id,
Method = registration.Method,
RegisterOptions = registration.RegisterOptions is JToken token
? token.ToObject(registrationType, _serializer.JsonSerializer)
: registration.RegisterOptions
};
if (_logger.IsEnabled(LogLevel.Trace))
{
_logger.LogTrace("Registered handler for {Method} {@Registration}", deserializedRegistration.Method, deserializedRegistration.RegisterOptions );
}
return deserializedRegistration;
}
public IObservable<IEnumerable<Registration>> Registrations
{
get {
if (_registrationSubject.IsDisposed)
{
return Observable.Empty<IEnumerable<Registration>>();
}
return _registrationSubject.AsObservable();
}
}
public IEnumerable<Registration> CurrentRegistrations => _registrations.Values;
public IEnumerable<Registration> GetRegistrationsForMethod(string method) => _registrations.Select(z => z.Value).Where(x => x.Method == method);
public IEnumerable<Registration> GetRegistrationsMatchingSelector(TextDocumentSelector textDocumentSelector) =>
_registrations
.Select(z => z.Value)
.Where(
x => x.RegisterOptions is ITextDocumentRegistrationOptions { DocumentSelector: { } } ro &&
ro.DocumentSelector
.Join(
textDocumentSelector,
z => z.HasLanguage ? z.Language :
z.HasScheme ? z.Scheme :
z.HasPattern ? z.Pattern : string.Empty,
z => z.HasLanguage ? z.Language :
z.HasScheme ? z.Scheme :
z.HasPattern ? z.Pattern : string.Empty, (a, _) => a
)
.Any(y => y.HasLanguage || y.HasPattern || y.HasScheme)
);
public void Dispose()
{
if (_registrationSubject.IsDisposed) return;
_registrationSubject.Dispose();
}
}
}