Skip to content

Commit 7c6bbaf

Browse files
Added the ability to use strongly typed data fields with codelens, completion and documentlink (#269)
* Added the ability to use strongly typed data fields with codelens, completion and documentlink. +semver:minor. * rollback change to data field, routing just won't work if it isn't an object * Roll back breaking chnages for textdocumentsync handler
1 parent a40c9ce commit 7c6bbaf

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3876
-878
lines changed

src/JsonRpc/AggregateResponse.cs

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace OmniSharp.Extensions.JsonRpc
6+
{
7+
public class AggregateResponse<T> where T : IEnumerable
8+
{
9+
public IEnumerable<T> Items { get; }
10+
11+
public AggregateResponse(IEnumerable<T> items)
12+
{
13+
Items = items.ToArray();
14+
}
15+
16+
public AggregateResponse(IEnumerable<object> items)
17+
{
18+
Items = items.OfType<T>().ToArray();
19+
}
20+
}
21+
}

src/JsonRpc/Client/OutgoingResponse.cs

-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ public OutgoingResponse(object id, object result, ServerRequest request)
1818
}
1919

2020
public object Id { get; set; }
21-
2221
public object Result { get; set; }
2322
public ServerRequest Request { get; }
2423
}

src/JsonRpc/IRequestDescriptor.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System.Collections.Generic;
2+
3+
namespace OmniSharp.Extensions.JsonRpc
4+
{
5+
public interface IRequestDescriptor<out TDescriptor> : IEnumerable<TDescriptor>
6+
{
7+
TDescriptor Default { get; }
8+
}
9+
}

src/JsonRpc/IRequestRouter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public interface IRequestRouter
1212

1313
public interface IRequestRouter<TDescriptor> : IRequestRouter
1414
{
15-
TDescriptor GetDescriptor(Notification notification);
16-
TDescriptor GetDescriptor(Request request);
17-
Task RouteNotification(TDescriptor descriptor, Notification notification, CancellationToken token);
18-
Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Request request, CancellationToken token);
15+
IRequestDescriptor<TDescriptor> GetDescriptors(Notification notification);
16+
IRequestDescriptor<TDescriptor> GetDescriptors(Request request);
17+
Task RouteNotification(IRequestDescriptor<TDescriptor> descriptors, Notification notification, CancellationToken token);
18+
Task<ErrorResponse> RouteRequest(IRequestDescriptor<TDescriptor> descriptors, Request request, CancellationToken token);
1919
}
2020
}

src/JsonRpc/InputHandler.cs

+13-13
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ public class InputHandler : IInputHandler, IDisposable
5454
private readonly CompositeDisposable _disposable;
5555
private readonly AsyncSubject<Unit> _inputActive;
5656

57-
private readonly ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IHandlerDescriptor descriptor)> _requests =
58-
new ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IHandlerDescriptor descriptor)>();
57+
private readonly ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IRequestDescriptor<IHandlerDescriptor> descriptor)> _requests =
58+
new ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IRequestDescriptor<IHandlerDescriptor> descriptor)>();
5959

6060
private readonly Subject<IObservable<Unit>> _inputQueue;
6161

@@ -410,15 +410,15 @@ private void HandleRequest(in ReadOnlySequence<byte> request)
410410
if (item.IsRequest)
411411
{
412412
// _logger.LogDebug("Handling Request {Method} {ResponseId}", item.Request.Method, item.Request.Id);
413-
var descriptor = _requestRouter.GetDescriptor(item.Request);
414-
if (descriptor is null)
413+
var descriptor = _requestRouter.GetDescriptors(item.Request);
414+
if (descriptor.Default is null)
415415
{
416416
_logger.LogDebug("Request handler was not found (or not setup) {Method} {ResponseId}", item.Request.Method, item.Request.Id);
417417
_outputHandler.Send(new MethodNotFound(item.Request.Id, item.Request.Method));
418418
return;
419419
}
420420

421-
var type = _requestProcessIdentifier.Identify(descriptor);
421+
var type = _requestProcessIdentifier.Identify(descriptor.Default);
422422
_scheduler.Add(type, $"{item.Request.Method}:{item.Request.Id}", RouteRequest(descriptor, item.Request));
423423
}
424424

@@ -445,16 +445,16 @@ private void HandleRequest(in ReadOnlySequence<byte> request)
445445
}
446446

447447
// _logger.LogDebug("Handling Request {Method}", item.Notification.Method);
448-
var descriptor = _requestRouter.GetDescriptor(item.Notification);
449-
if (descriptor is null)
448+
var descriptor = _requestRouter.GetDescriptors(item.Notification);
449+
if (descriptor.Default is null)
450450
{
451451
_logger.LogDebug("Notification handler was not found (or not setup) {Method}", item.Notification.Method);
452452
// TODO: Figure out a good way to send this feedback back.
453453
// _outputHandler.Send(new RpcError(null, new ErrorMessage(-32601, $"Method not found - {item.Notification.Method}")));
454454
return;
455455
}
456456

457-
var type = _requestProcessIdentifier.Identify(descriptor);
457+
var type = _requestProcessIdentifier.Identify(descriptor.Default);
458458
_scheduler.Add(type, item.Notification.Method, RouteNotification(descriptor, item.Notification));
459459
}
460460

@@ -465,11 +465,11 @@ private void HandleRequest(in ReadOnlySequence<byte> request)
465465
}
466466
}
467467

468-
private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request request)
468+
private SchedulerDelegate RouteRequest(IRequestDescriptor<IHandlerDescriptor> descriptors, Request request)
469469
{
470470
// start request, create cts, etc
471471
var cts = new CancellationTokenSource();
472-
_requests.TryAdd(request.Id, (cts, descriptor));
472+
_requests.TryAdd(request.Id, (cts, descriptors));
473473

474474
return (contentModifiedToken, scheduler) => Observable.Create<ErrorResponse>(observer => {
475475
// ITS A RACE!
@@ -485,7 +485,7 @@ private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request re
485485
// ObservableToToken(contentModifiedToken).Register(cts.Cancel);
486486
try
487487
{
488-
return await _requestRouter.RouteRequest(descriptor, request, cts.Token);
488+
return await _requestRouter.RouteRequest(descriptors, request, cts.Token);
489489
}
490490
catch (OperationCanceledException)
491491
{
@@ -521,7 +521,7 @@ private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request re
521521
});
522522
}
523523

524-
private SchedulerDelegate RouteNotification(IHandlerDescriptor descriptor, Notification notification)
524+
private SchedulerDelegate RouteNotification(IRequestDescriptor<IHandlerDescriptor> descriptors, Notification notification)
525525
{
526526
return (contentModifiedToken, scheduler) =>
527527
// ITS A RACE!
@@ -536,7 +536,7 @@ private SchedulerDelegate RouteNotification(IHandlerDescriptor descriptor, Notif
536536
using var timer = _logger.TimeDebug("Processing notification {Method}", notification.Method);
537537
try
538538
{
539-
await _requestRouter.RouteNotification(descriptor, notification, ct);
539+
await _requestRouter.RouteNotification(descriptors, notification, ct);
540540
}
541541
catch (OperationCanceledException)
542542
{

src/JsonRpc/RequestDescriptor.cs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
5+
namespace OmniSharp.Extensions.JsonRpc
6+
{
7+
class RequestDescriptor<TDescriptor> : IRequestDescriptor<TDescriptor>
8+
{
9+
private IEnumerable<TDescriptor> _descriptors;
10+
11+
public RequestDescriptor(IEnumerable<TDescriptor> descriptors)
12+
{
13+
var enumerable = descriptors as TDescriptor[] ?? descriptors.ToArray();
14+
_descriptors = enumerable;
15+
Default = enumerable.FirstOrDefault();
16+
}
17+
18+
public RequestDescriptor(params TDescriptor[] descriptors)
19+
{
20+
var enumerable = descriptors.ToArray();
21+
_descriptors = enumerable;
22+
Default = enumerable.FirstOrDefault();
23+
}
24+
25+
public IEnumerator<TDescriptor> GetEnumerator() => _descriptors.GetEnumerator();
26+
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable) _descriptors).GetEnumerator();
27+
28+
public TDescriptor Default { get; }
29+
}
30+
}

src/JsonRpc/RequestRouter.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ private IHandlerDescriptor FindDescriptor(IMethodWithParams instance)
2727
return _collection.FirstOrDefault(x => x.Method == instance.Method);
2828
}
2929

30-
public override IHandlerDescriptor GetDescriptor(Notification notification)
30+
public override IRequestDescriptor<IHandlerDescriptor> GetDescriptors(Notification notification)
3131
{
32-
return FindDescriptor(notification);
32+
return new RequestDescriptor<IHandlerDescriptor>(FindDescriptor(notification));
3333
}
3434

35-
public override IHandlerDescriptor GetDescriptor(Request request)
35+
public override IRequestDescriptor<IHandlerDescriptor> GetDescriptors(Request request)
3636
{
37-
return FindDescriptor(request);
37+
return new RequestDescriptor<IHandlerDescriptor>(FindDescriptor(request));
3838
}
3939
}
4040
}

0 commit comments

Comments
 (0)