Skip to content

Commit b1f0d7e

Browse files
Removed key, not needed, and use the text registration options
1 parent f453534 commit b1f0d7e

17 files changed

+96
-83
lines changed

sample/SampleServer/Program.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,6 @@ public TextDocumentHandler(ILanguageServer router)
6565
OpenClose = true
6666
};
6767

68-
public string Key => (string)_documentSelector;
69-
7068
public Task Handle(DidChangeTextDocumentParams notification)
7169
{
7270
_router.LogMessage(new LogMessageParams()

src/JsonRpc/IJsonRpcHandler.cs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
/// <summary>
44
/// A simple marker interface to use for storing handlings (which will be cast out later)
55
/// </summary>
6-
public interface IJsonRpcHandler
7-
{
8-
string Key { get; }
9-
}
6+
public interface IJsonRpcHandler { }
107
}

src/JsonRpc/IRequestRouter.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,5 @@ public interface IRequestRouter
88
{
99
void RouteNotification(Notification notification);
1010
Task<ErrorResponse> RouteRequest(Request request);
11-
12-
IDisposable Add(IJsonRpcHandler handler);
1311
}
14-
}
12+
}

src/Lsp/Abstractions/IRegistration.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
namespace OmniSharp.Extensions.LanguageServer.Abstractions
22
{
3-
public interface IRegistration<TOptions>
3+
public interface IRegistration<out TOptions>
44
{
55
TOptions GetRegistrationOptions();
66
}
7-
}
7+
}

src/Lsp/HandlerCollection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Reflection;
66
using OmniSharp.Extensions.JsonRpc;
77
using OmniSharp.Extensions.LanguageServer.Abstractions;
8+
using OmniSharp.Extensions.LanguageServer.Models;
89

910
namespace OmniSharp.Extensions.LanguageServer
1011
{
@@ -46,8 +47,16 @@ public IDisposable Add(IEnumerable<IJsonRpcHandler> handlers)
4647
@params = @interface.GetTypeInfo().GetGenericArguments()[0];
4748
}
4849

50+
var key = "default";
51+
if (handler is IRegistration<TextDocumentRegistrationOptions> textDocumentRegistration)
52+
{
53+
var options = textDocumentRegistration.GetRegistrationOptions();
54+
key = options.DocumentSelector;
55+
}
56+
4957
var h = new HandlerDescriptor(
5058
LspHelper.GetMethodName(implementedInterface),
59+
key,
5160
handler,
5261
@interface,
5362
@params,

src/Lsp/HandlerDescriptor.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ class HandlerDescriptor : ILspHandlerDescriptor, IDisposable
1111
{
1212
private readonly Action _disposeAction;
1313

14-
public HandlerDescriptor(string method, IJsonRpcHandler handler, Type handlerType, Type @params, Type registrationType, Type capabilityType, Action disposeAction)
14+
public HandlerDescriptor(string method, string key, IJsonRpcHandler handler, Type handlerType, Type @params, Type registrationType, Type capabilityType, Action disposeAction)
1515
{
1616
_disposeAction = disposeAction;
1717
Handler = handler;
1818
Method = method;
19+
Key = key;
1920
HandlerType = handlerType;
2021
Params = @params;
2122
RegistrationType = registrationType;
@@ -70,6 +71,7 @@ public void SetCapability(object instance)
7071
}
7172

7273
public string Method { get; }
74+
public string Key { get; }
7375
public Type Params { get; }
7476

7577
public bool IsDynamicCapability => typeof(DynamicCapability).GetTypeInfo().IsAssignableFrom(CapabilityType);
@@ -94,15 +96,14 @@ public override bool Equals(object obj)
9496
{
9597
if (obj is HandlerDescriptor handler)
9698
{
97-
return handler.HandlerType == HandlerType && handler.Handler.Key == Handler.Key;
99+
return handler.HandlerType == HandlerType && handler.Key == Key;
98100
}
99101
return false;
100102
}
101103

102104
public override int GetHashCode()
103105
{
104-
if (string.IsNullOrWhiteSpace(Handler.Key)) return HandlerType.GetHashCode();
105-
return Tuple.Create(HandlerType, Handler.Key).GetHashCode();
106+
return Tuple.Create(HandlerType, Key).GetHashCode();
106107
}
107108
}
108109
}

src/Lsp/Handlers/CancelRequestHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ internal CancelRequestHandler(LspRequestRouter requestRouter)
1313
_requestRouter = requestRouter;
1414
}
1515

16-
public string Key => nameof(ICancelRequestHandler);
17-
1816
public Task Handle(CancelParams notification)
1917
{
2018
_requestRouter.CancelRequest(notification.Id);

src/Lsp/Handlers/ExitHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ public ExitHandler(ShutdownHandler shutdownHandler)
1212
_shutdownHandler = shutdownHandler;
1313
}
1414

15-
public string Key => nameof(IExitHandler);
16-
1715
public Task Handle()
1816
{
1917
Exit?.Invoke(_shutdownHandler.ShutdownRequested ? 0 : 1);

src/Lsp/Handlers/ShutdownHandler.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ namespace OmniSharp.Extensions.LanguageServer.Handlers
66
{
77
public class ShutdownHandler : IShutdownHandler, IAwaitableTermination
88
{
9-
public string Key => nameof(IShutdownHandler);
10-
119
public Task Handle()
1210
{
1311
ShutdownRequested = true;

src/Lsp/LanguageServer.cs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,7 @@ public LanguageServer(Stream input, Stream output)
3838
{
3939
}
4040

41-
internal LanguageServer(
42-
Stream input,
43-
IOutputHandler output,
44-
LspReciever reciever,
45-
IRequestProcessIdentifier requestProcessIdentifier
46-
)
41+
internal LanguageServer(Stream input, IOutputHandler output, LspReciever reciever, IRequestProcessIdentifier requestProcessIdentifier)
4742
{
4843
_reciever = reciever;
4944
_requestRouter = new LspRequestRouter(_collection);
@@ -53,18 +48,13 @@ IRequestProcessIdentifier requestProcessIdentifier
5348
_exitHandler = new ExitHandler(_shutdownHandler);
5449

5550
_disposable.Add(
56-
AddHandler(this),
57-
AddHandler(_shutdownHandler),
58-
AddHandler(_exitHandler),
59-
AddHandler(new CancelRequestHandler(_requestRouter))
51+
AddHandlers(this, _shutdownHandler, _exitHandler, new CancelRequestHandler(_requestRouter))
6052
);
6153
}
6254

6355
public InitializeParams Client { get; private set; }
6456
public InitializeResult Server { get; private set; }
6557

66-
public string Key => nameof(ILanguageServer);
67-
6858
public IDisposable AddHandler(IJsonRpcHandler handler)
6959
{
7060
return AddHandler(handler);
@@ -91,7 +81,10 @@ public IDisposable AddHandlers(IEnumerable<IJsonRpcHandler> handlers)
9181
.Where(x => x != null))
9282
.ToArray();
9383

94-
Task.Run(() => this.UnregisterCapability(new UnregistrationParams() { Unregisterations = foundItems }));
84+
Task.Run(() => this.UnregisterCapability(new UnregistrationParams()
85+
{
86+
Unregisterations = foundItems
87+
}));
9588
}));
9689
}
9790

src/Lsp/LspRequestRouter.cs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,13 @@ private ILspHandlerDescriptor FindDescriptor(string method, JToken @params)
4646
var descriptor = _collection.FirstOrDefault(x => x.Method == method);
4747
if (descriptor is null) return null;
4848

49+
if (_textDocumentSyncHandler == null)
50+
{
51+
_textDocumentSyncHandler = _collection
52+
.Select(x => x.Handler is ITextDocumentSyncHandler r ? r : null)
53+
.FirstOrDefault(x => x != null);
54+
}
55+
4956
if (_textDocumentSyncHandler is null) return descriptor;
5057

5158
if (typeof(ITextDocumentIdentifierParams).GetTypeInfo().IsAssignableFrom(descriptor.Params))
@@ -160,20 +167,6 @@ public async Task<ErrorResponse> RouteRequest(Request request)
160167
}
161168
}
162169

163-
public IDisposable Add(IJsonRpcHandler handler)
164-
{
165-
// ITextDocumentSyncHandler textDocumentSyncHandler
166-
var result = _collection.Add(handler);
167-
168-
if (_textDocumentSyncHandler == null)
169-
{
170-
_textDocumentSyncHandler = _collection
171-
.Select(x => x.Handler is ITextDocumentSyncHandler r ? r : null)
172-
.FirstOrDefault(x => x != null);
173-
}
174-
return result;
175-
}
176-
177170
public void CancelRequest(object id)
178171
{
179172
if (_requests.TryGetValue(GetId(id), out var cts))
@@ -182,4 +175,4 @@ public void CancelRequest(object id)
182175
}
183176
}
184177
}
185-
}
178+
}

src/Lsp/Models/CodeActionParams.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
namespace OmniSharp.Extensions.LanguageServer.Models
55
{
6-
public interface ITextDocumentIdentifierParams { TextDocumentIdentifier TextDocument { get; } }
6+
public interface ITextDocumentIdentifierParams
7+
{
8+
TextDocumentIdentifier TextDocument { get; }
9+
}
710

811
/// <summary>
912
/// Params for the CodeActionRequest
@@ -26,4 +29,4 @@ public class CodeActionParams : ITextDocumentIdentifierParams
2629
/// </summary>
2730
public CodeActionContext Context { get; set; }
2831
}
29-
}
32+
}

src/Lsp/Models/DocumentSelector.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static implicit operator DocumentSelector(List<DocumentFilter> items)
3838
return new DocumentSelector(items);
3939
}
4040

41-
public static explicit operator string(DocumentSelector documentSelector)
41+
public static implicit operator string(DocumentSelector documentSelector)
4242
{
4343
return string.Join(", ", documentSelector.Select(x => (string)x));
4444
}

src/Lsp/Models/TextDocumentRegistrationOptions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using Newtonsoft.Json;
1+
using Newtonsoft.Json;
22
using Newtonsoft.Json.Serialization;
33

44
namespace OmniSharp.Extensions.LanguageServer.Models
@@ -10,6 +10,6 @@ public class TextDocumentRegistrationOptions : ITextDocumentRegistrationOptions
1010
/// A document selector to identify the scope of the registration. If set to null
1111
/// the document selector provided on the client side will be used.
1212
/// </summary>
13-
public DocumentSelector DocumentSelector { get; set; }
13+
public DocumentSelector DocumentSelector { get; set; } = new DocumentSelector();
1414
}
15-
}
15+
}

test/Lsp.Tests/HandlerResolverTests.cs

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Linq;
33
using System.Reflection;
44
using System.Threading;
@@ -10,6 +10,8 @@
1010
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
1111
using Xunit;
1212
using HandlerCollection = OmniSharp.Extensions.LanguageServer.HandlerCollection;
13+
using OmniSharp.Extensions.LanguageServer.Models;
14+
using OmniSharp.Extensions.LanguageServer.Abstractions;
1315

1416
namespace Lsp.Tests
1517
{
@@ -27,26 +29,39 @@ public void Should_Contain_AllDefinedMethods(Type requestHandler, string key, in
2729
{
2830
var handler = new HandlerCollection();
2931
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
30-
sub.Key.Returns("abcd");
32+
if (sub is IRegistration<TextDocumentRegistrationOptions> reg)
33+
reg.GetRegistrationOptions()
34+
.Returns(new TextDocumentRegistrationOptions());
35+
3136
handler.Add(sub);
3237
handler._handlers.Should().Contain(x => x.Method == key);
3338
handler._handlers.Count.Should().Be(count);
3439
}
3540

3641
[Theory]
37-
[InlineData(typeof(IInitializeHandler), "initialize", 2)]
38-
[InlineData(typeof(IInitializedHandler), "initialized", 2)]
39-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didOpen", 8)]
40-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didChange", 8)]
41-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didClose", 8)]
42-
[InlineData(typeof(ITextDocumentSyncHandler), "textDocument/didSave", 8)]
43-
public void Should_Contain_AllDefinedMethods_ForDifferentKeys(Type requestHandler, string key, int count)
42+
[InlineData("textDocument/didOpen", 8)]
43+
[InlineData("textDocument/didChange", 8)]
44+
[InlineData("textDocument/didClose", 8)]
45+
[InlineData("textDocument/didSave", 8)]
46+
public void Should_Contain_AllDefinedMethods_ForDifferentKeys(string key, int count)
4447
{
4548
var handler = new HandlerCollection();
46-
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
47-
sub.Key.Returns("abcd");
48-
var sub2 = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler }, new object[0]);
49-
sub2.Key.Returns("efgh");
49+
var sub = Substitute.For<ITextDocumentSyncHandler>();
50+
if (sub is IRegistration<TextDocumentRegistrationOptions> reg)
51+
reg.GetRegistrationOptions()
52+
.Returns(new TextDocumentRegistrationOptions() {
53+
DocumentSelector = new DocumentSelector(new DocumentFilter() {
54+
Pattern = "**/*.cs"
55+
})
56+
});
57+
58+
var sub2 = Substitute.For<ITextDocumentSyncHandler>();
59+
if (sub2 is IRegistration<TextDocumentRegistrationOptions> reg2)
60+
reg2.GetRegistrationOptions()
61+
.Returns(new TextDocumentRegistrationOptions() {
62+
DocumentSelector = new DocumentSelector(new DocumentFilter() { Pattern = "**/*.cake" })
63+
});
64+
5065
handler.Add(sub);
5166
handler.Add(sub2);
5267
handler._handlers.Should().Contain(x => x.Method == key);
@@ -59,22 +74,34 @@ public void Should_Contain_AllDefinedMethods_OnLanguageServer(Type requestHandle
5974
{
6075
var handler = new HandlerCollection();
6176
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
62-
sub.Key.Returns("abd3");
77+
if (sub is IRegistration<TextDocumentRegistrationOptions> reg)
78+
reg.GetRegistrationOptions()
79+
.Returns(new TextDocumentRegistrationOptions() {
80+
DocumentSelector = new DocumentSelector()
81+
});
6382
handler.Add(sub);
6483
handler._handlers.Should().Contain(x => x.Method == key);
6584
handler._handlers.Should().Contain(x => x.Method == key2);
6685
handler._handlers.Count.Should().Be(count);
6786
}
6887

6988
[Theory]
70-
[InlineData(typeof(IInitializeHandler), typeof(IInitializedHandler), "initialize", "initialized", 4)]
89+
[InlineData(typeof(IInitializeHandler), typeof(IInitializedHandler), "initialize", "initialized", 2)]
7190
public void Should_Contain_AllDefinedMethods_OnLanguageServer_WithDifferentKeys(Type requestHandler, Type type2, string key, string key2, int count)
7291
{
7392
var handler = new HandlerCollection();
7493
var sub = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
75-
sub.Key.Returns("abd3");
94+
if (sub is IRegistration<TextDocumentRegistrationOptions> reg)
95+
reg.GetRegistrationOptions()
96+
.Returns(new TextDocumentRegistrationOptions() {
97+
DocumentSelector = new DocumentSelector()
98+
});
7699
var sub2 = (IJsonRpcHandler)Substitute.For(new Type[] { requestHandler, type2 }, new object[0]);
77-
sub2.Key.Returns("efgh");
100+
if (sub2 is IRegistration<TextDocumentRegistrationOptions> reg2)
101+
reg2.GetRegistrationOptions()
102+
.Returns(new TextDocumentRegistrationOptions() {
103+
DocumentSelector = new DocumentSelector()
104+
});
78105
handler.Add(sub);
79106
handler.Add(sub2);
80107
handler._handlers.Should().Contain(x => x.Method == key);

test/Lsp.Tests/MediatorTestsRequestHandlerOfTRequestTResponse.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Reflection;
@@ -58,4 +58,4 @@ public async Task RequestsCancellation()
5858
}
5959

6060
}
61-
}
61+
}

0 commit comments

Comments
 (0)