Skip to content

Commit f1c675a

Browse files
Fixed an issue where the error object was incorrectly shaped as per the spec
1 parent 9869b91 commit f1c675a

24 files changed

+248
-119
lines changed

src/JsonRpc/Client/Response.cs

-10
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,10 @@ public Response(object id, object result)
1717
Result = result;
1818
}
1919

20-
public Response(object id, RpcError result)
21-
{
22-
Id = id;
23-
Result = result;
24-
}
25-
2620
public string ProtocolVersion { get; set; } = "2.0";
2721

2822
public object Id { get; set; }
2923

30-
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
3124
public object Result { get; set; }
32-
33-
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
34-
public RpcError Error { get; set; }
3525
}
3626
}

src/JsonRpc/HandlerCollection.cs

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections;
33
using System.Collections.Generic;
44
using System.Linq;
@@ -66,16 +66,6 @@ public IDisposable Add(IJsonRpcHandler handler)
6666
return h;
6767
}
6868

69-
public IHandlerInstance Get(IJsonRpcHandler handler)
70-
{
71-
return _handlers.Find(instance => instance.Handler == handler);
72-
}
73-
74-
public IHandlerInstance Get(string method)
75-
{
76-
return _handlers.Find(instance => instance.Method == method);
77-
}
78-
7969
private static readonly Type[] HandlerTypes = { typeof(INotificationHandler), typeof(INotificationHandler<>), typeof(IRequestHandler<>), typeof(IRequestHandler<,>), };
8070

8171
private string GetMethodName(Type type)
@@ -115,4 +105,4 @@ private Type GetHandlerInterface(Type type)
115105
.First(IsValidInterface);
116106
}
117107
}
118-
}
108+
}

src/JsonRpc/InputHandler.cs

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Newtonsoft.Json.Linq;
77
using Microsoft.Extensions.Logging;
88
using Newtonsoft.Json;
9+
using OmniSharp.Extensions.JsonRpc.Server;
910
using OmniSharp.Extensions.JsonRpc.Server.Messages;
1011

1112
namespace OmniSharp.Extensions.JsonRpc
@@ -148,13 +149,13 @@ private void HandleRequest(string request)
148149
var tcs = _responseRouter.GetRequest(id);
149150
if (tcs is null) continue;
150151

151-
if (response.Error is null)
152+
if (response is ServerResponse serverResponse)
152153
{
153-
tcs.SetResult(response.Result);
154+
tcs.SetResult(serverResponse.Result);
154155
}
155-
else
156+
else if (response is ServerError serverError)
156157
{
157-
tcs.SetException(new Exception(JsonConvert.SerializeObject(response.Error)));
158+
tcs.SetException(new Exception(JsonConvert.SerializeObject(serverError.Error)));
158159
}
159160
}
160161

@@ -168,8 +169,7 @@ private void HandleRequest(string request)
168169
_scheduler.Add(
169170
type,
170171
item.Request.Method,
171-
async () =>
172-
{
172+
async () => {
173173
try
174174
{
175175
var result = await _requestRouter.RouteRequest(item.Request);
@@ -190,8 +190,7 @@ private void HandleRequest(string request)
190190
_scheduler.Add(
191191
type,
192192
item.Notification.Method,
193-
() =>
194-
{
193+
() => {
195194
try
196195
{
197196
_requestRouter.RouteNotification(item.Notification);

src/JsonRpc/Reciever.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ protected virtual Renor GetRenor(JToken @object)
6363

6464
if (hasRequestId && request.TryGetValue("result", out var response))
6565
{
66-
return new Response(requestId, response);
66+
return new ServerResponse(requestId, response);
6767
}
6868

6969
if (hasRequestId && request.TryGetValue("error", out var errorResponse))
7070
{
7171
// TODO: this doesn't seem right.
72-
return new RpcError(requestId, new ErrorMessage<object>(-1337, "Unknown error response", errorResponse));
72+
return new ServerError(requestId, errorResponse);
7373
}
7474

7575
var method = request["method"]?.Value<string>();

src/JsonRpc/RequestRouter.cs

+9-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using System;
2+
using System.Linq;
23
using System.Reflection;
34
using System.Threading;
45
using System.Threading.Tasks;
@@ -23,7 +24,7 @@ public IDisposable Add(IJsonRpcHandler handler)
2324

2425
public async void RouteNotification(Notification notification)
2526
{
26-
var handler = _collection.Get(notification.Method);
27+
var handler = _collection.FirstOrDefault(x => x.Method == notification.Method);
2728

2829
Task result;
2930
if (handler.Params is null)
@@ -46,16 +47,14 @@ public Task<ErrorResponse> RouteRequest(Request request)
4647

4748
protected virtual async Task<ErrorResponse> RouteRequest(Request request, CancellationToken token)
4849
{
49-
var handler = _collection.Get(request.Method);
50-
51-
var method = _collection.Get(request.Method);
52-
if (method is null)
50+
var handler = _collection.FirstOrDefault(x => x.Method == request.Method);
51+
if (request.Method is null)
5352
{
54-
return new MethodNotFound(request.Id);
53+
return new MethodNotFound(request.Id, request.Method);
5554
}
5655

5756
Task result;
58-
if (method.Params is null)
57+
if (handler.Params is null)
5958
{
6059
result = ReflectionRequestHandlers.HandleRequest(handler, token);
6160
}
@@ -64,7 +63,7 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
6463
object @params;
6564
try
6665
{
67-
@params = request.Params.ToObject(method.Params);
66+
@params = request.Params.ToObject(handler.Params);
6867
}
6968
catch
7069
{
@@ -89,4 +88,4 @@ protected virtual async Task<ErrorResponse> RouteRequest(Request request, Cancel
8988
return new Client.Response(request.Id, responseValue);
9089
}
9190
}
92-
}
91+
}

src/JsonRpc/RpcError.cs

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ public RpcError(object id, ErrorMessage<T> message, string protocolVersion)
2525
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2626
public object Id { get; }
2727

28-
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
2928
public ErrorMessage<T> Error { get; }
3029
}
3130

src/JsonRpc/Server/Messages/MethodNotFound.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
{
33
public class MethodNotFound : RpcError
44
{
5-
public MethodNotFound(object id) : base(id, new ErrorMessage(-32601, "Method not found")) { }
5+
public MethodNotFound(object id, string method) : base(id, new ErrorMessage(-32601, $"Method not found - {method}")) { }
66
}
77
}

src/JsonRpc/Server/Renor.cs

+8-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal Renor(RpcError errorMessage)
3030
Response = null;
3131
}
3232

33-
internal Renor(Response response)
33+
internal Renor(ResponseBase response)
3434
{
3535
Notification = null;
3636
Request = null;
@@ -48,7 +48,7 @@ internal Renor(Response response)
4848
public RpcError Error { get; }
4949

5050
public bool IsResponse => Response != null;
51-
public Response Response { get; }
51+
public ResponseBase Response { get; }
5252

5353
public static implicit operator Renor(Notification notification)
5454
{
@@ -65,7 +65,12 @@ public static implicit operator Renor(RpcError error)
6565
return new Renor(error);
6666
}
6767

68-
public static implicit operator Renor(Response response)
68+
public static implicit operator Renor(ServerResponse response)
69+
{
70+
return new Renor(response);
71+
}
72+
73+
public static implicit operator Renor(ServerError response)
6974
{
7075
return new Renor(response);
7176
}

src/JsonRpc/Server/Response.cs

-31
This file was deleted.

src/JsonRpc/Server/ServerResponse.cs

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Linq;
3+
using OmniSharp.Extensions.JsonRpc.Server.Messages;
4+
5+
namespace OmniSharp.Extensions.JsonRpc.Server
6+
{
7+
public class ResponseBase
8+
{
9+
public ResponseBase(object id)
10+
{
11+
Id = id;
12+
}
13+
14+
public string ProtocolVersion { get; set; } = "2.0";
15+
16+
public object Id { get; set; }
17+
}
18+
19+
public class ServerResponse : ResponseBase
20+
{
21+
public ServerResponse(object id, JToken result) : base(id)
22+
{
23+
Result = result;
24+
}
25+
26+
public JToken Result { get; set; }
27+
}
28+
29+
public class ServerError : ResponseBase
30+
{
31+
public ServerError(object id, JToken result) : base(id)
32+
{
33+
Error = result;
34+
}
35+
36+
public JToken Error { get; set; }
37+
}
38+
}

src/Lsp/Capabilities/Client/SynchronizationCapability.cs

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client
1+
using OmniSharp.Extensions.LanguageServer.Protocol.Document;
2+
3+
namespace OmniSharp.Extensions.LanguageServer.Capabilities.Client
24
{
3-
public class SynchronizationCapability : DynamicCapability
5+
public class SynchronizationCapability : DynamicCapability, ConnectedCapability<ITextDocumentSyncHandler>
46
{
57
/// <summary>
68
/// The client supports sending will save notifications.

src/Lsp/Capabilities/Server/SignatureHelpOptions.cs

+2-2
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
using OmniSharp.Extensions.LanguageServer.Models;
44

@@ -22,4 +22,4 @@ public static SignatureHelpOptions Of(ISignatureHelpOptions options)
2222
return new SignatureHelpOptions() { TriggerCharacters = options.TriggerCharacters };
2323
}
2424
}
25-
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
using Newtonsoft.Json;
2+
using Newtonsoft.Json.Serialization;
3+
using OmniSharp.Extensions.LanguageServer.Models;
4+
5+
namespace OmniSharp.Extensions.LanguageServer.Capabilities.Server
6+
{
7+
/// <summary>
8+
/// Signature help options.
9+
/// </summary>
10+
[JsonObject(NamingStrategyType = typeof(CamelCaseNamingStrategy))]
11+
public class SynchronizationOptions : ISynchronizationOptions
12+
{
13+
/// <summary>
14+
/// The client supports sending will save notifications.
15+
/// </summary>
16+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
17+
public bool WillSave { get; set; }
18+
19+
/// <summary>
20+
/// The client supports sending a will save request and
21+
/// waits for a response providing text edits which will
22+
/// be applied to the document before it is saved.
23+
/// </summary>
24+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
25+
public bool WillSaveWaitUntil { get; set; }
26+
27+
/// <summary>
28+
/// The client supports did save notifications.
29+
/// </summary>
30+
[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
31+
public bool DidSave { get; set; }
32+
33+
public static SynchronizationOptions Of(ISynchronizationOptions options)
34+
{
35+
return new SynchronizationOptions() { WillSave = options.WillSave, DidSave = options.DidSave, WillSaveWaitUntil = options.WillSaveWaitUntil };
36+
}
37+
}
38+
}

src/Lsp/LanguageServer.cs

+13-9
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ internal LanguageServer(Stream input, IOutputHandler output, LspReciever recieve
4545

4646
_reciever = reciever;
4747
_loggerFactory = loggerFactory;
48-
_requestRouter = new LspRequestRouter(_collection);
48+
_requestRouter = new LspRequestRouter(_collection, loggerFactory);
4949
_responseRouter = new ResponseRouter(output);
5050
_connection = new Connection(input, output, reciever, requestProcessIdentifier, _requestRouter, _responseRouter, loggerFactory);
5151

@@ -168,14 +168,17 @@ async Task<InitializeResult> IRequestHandler<InitializeParams, InitializeResult>
168168
}
169169
else
170170
{
171-
// TODO: Merge options
172-
serverCapabilities.TextDocumentSync = textSyncHandlers.FirstOrDefault()?.Options ?? new TextDocumentSyncOptions() {
173-
Change = TextDocumentSyncKind.None,
174-
OpenClose = false,
175-
Save = new SaveOptions() { IncludeText = false },
176-
WillSave = false,
177-
WillSaveWaitUntil = false
178-
};
171+
if (ccp.HasHandler(textDocumentCapabilities.Synchronization))
172+
{
173+
// TODO: Merge options
174+
serverCapabilities.TextDocumentSync = textSyncHandlers.FirstOrDefault()?.Options ?? new TextDocumentSyncOptions() {
175+
Change = TextDocumentSyncKind.None,
176+
OpenClose = false,
177+
Save = new SaveOptions() { IncludeText = false },
178+
WillSave = false,
179+
WillSaveWaitUntil = false
180+
};
181+
}
179182
}
180183

181184
_reciever.Initialized();
@@ -286,3 +289,4 @@ public void Dispose()
286289
public IDictionary<string, JToken> Experimental { get; } = new Dictionary<string, JToken>();
287290
}
288291
}
292+

0 commit comments

Comments
 (0)