Skip to content

Added the ability to use strongly typed data fields with codelens, completion and documentlink #269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/JsonRpc/AggregateResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace OmniSharp.Extensions.JsonRpc
{
public class AggregateResponse<T> where T : IEnumerable
{
public IEnumerable<T> Items { get; }

public AggregateResponse(IEnumerable<T> items)
{
Items = items.ToArray();
}

public AggregateResponse(IEnumerable<object> items)
{
Items = items.OfType<T>().ToArray();
}
}
}
1 change: 0 additions & 1 deletion src/JsonRpc/Client/OutgoingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ public OutgoingResponse(object id, object result, ServerRequest request)
}

public object Id { get; set; }

public object Result { get; set; }
public ServerRequest Request { get; }
}
Expand Down
9 changes: 9 additions & 0 deletions src/JsonRpc/IRequestDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Collections.Generic;

namespace OmniSharp.Extensions.JsonRpc
{
public interface IRequestDescriptor<out TDescriptor> : IEnumerable<TDescriptor>
{
TDescriptor Default { get; }
}
}
8 changes: 4 additions & 4 deletions src/JsonRpc/IRequestRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public interface IRequestRouter

public interface IRequestRouter<TDescriptor> : IRequestRouter
{
TDescriptor GetDescriptor(Notification notification);
TDescriptor GetDescriptor(Request request);
Task RouteNotification(TDescriptor descriptor, Notification notification, CancellationToken token);
Task<ErrorResponse> RouteRequest(TDescriptor descriptor, Request request, CancellationToken token);
IRequestDescriptor<TDescriptor> GetDescriptors(Notification notification);
IRequestDescriptor<TDescriptor> GetDescriptors(Request request);
Task RouteNotification(IRequestDescriptor<TDescriptor> descriptors, Notification notification, CancellationToken token);
Task<ErrorResponse> RouteRequest(IRequestDescriptor<TDescriptor> descriptors, Request request, CancellationToken token);
}
}
26 changes: 13 additions & 13 deletions src/JsonRpc/InputHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ public class InputHandler : IInputHandler, IDisposable
private readonly CompositeDisposable _disposable;
private readonly AsyncSubject<Unit> _inputActive;

private readonly ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IHandlerDescriptor descriptor)> _requests =
new ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IHandlerDescriptor descriptor)>();
private readonly ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IRequestDescriptor<IHandlerDescriptor> descriptor)> _requests =
new ConcurrentDictionary<object, (CancellationTokenSource cancellationTokenSource, IRequestDescriptor<IHandlerDescriptor> descriptor)>();

private readonly Subject<IObservable<Unit>> _inputQueue;

Expand Down Expand Up @@ -410,15 +410,15 @@ private void HandleRequest(in ReadOnlySequence<byte> request)
if (item.IsRequest)
{
// _logger.LogDebug("Handling Request {Method} {ResponseId}", item.Request.Method, item.Request.Id);
var descriptor = _requestRouter.GetDescriptor(item.Request);
if (descriptor is null)
var descriptor = _requestRouter.GetDescriptors(item.Request);
if (descriptor.Default is null)
{
_logger.LogDebug("Request handler was not found (or not setup) {Method} {ResponseId}", item.Request.Method, item.Request.Id);
_outputHandler.Send(new MethodNotFound(item.Request.Id, item.Request.Method));
return;
}

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

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

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

var type = _requestProcessIdentifier.Identify(descriptor);
var type = _requestProcessIdentifier.Identify(descriptor.Default);
_scheduler.Add(type, item.Notification.Method, RouteNotification(descriptor, item.Notification));
}

Expand All @@ -465,11 +465,11 @@ private void HandleRequest(in ReadOnlySequence<byte> request)
}
}

private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request request)
private SchedulerDelegate RouteRequest(IRequestDescriptor<IHandlerDescriptor> descriptors, Request request)
{
// start request, create cts, etc
var cts = new CancellationTokenSource();
_requests.TryAdd(request.Id, (cts, descriptor));
_requests.TryAdd(request.Id, (cts, descriptors));

return (contentModifiedToken, scheduler) => Observable.Create<ErrorResponse>(observer => {
// ITS A RACE!
Expand All @@ -485,7 +485,7 @@ private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request re
// ObservableToToken(contentModifiedToken).Register(cts.Cancel);
try
{
return await _requestRouter.RouteRequest(descriptor, request, cts.Token);
return await _requestRouter.RouteRequest(descriptors, request, cts.Token);
}
catch (OperationCanceledException)
{
Expand Down Expand Up @@ -521,7 +521,7 @@ private SchedulerDelegate RouteRequest(IHandlerDescriptor descriptor, Request re
});
}

private SchedulerDelegate RouteNotification(IHandlerDescriptor descriptor, Notification notification)
private SchedulerDelegate RouteNotification(IRequestDescriptor<IHandlerDescriptor> descriptors, Notification notification)
{
return (contentModifiedToken, scheduler) =>
// ITS A RACE!
Expand All @@ -536,7 +536,7 @@ private SchedulerDelegate RouteNotification(IHandlerDescriptor descriptor, Notif
using var timer = _logger.TimeDebug("Processing notification {Method}", notification.Method);
try
{
await _requestRouter.RouteNotification(descriptor, notification, ct);
await _requestRouter.RouteNotification(descriptors, notification, ct);
}
catch (OperationCanceledException)
{
Expand Down
30 changes: 30 additions & 0 deletions src/JsonRpc/RequestDescriptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace OmniSharp.Extensions.JsonRpc
{
class RequestDescriptor<TDescriptor> : IRequestDescriptor<TDescriptor>
{
private IEnumerable<TDescriptor> _descriptors;

public RequestDescriptor(IEnumerable<TDescriptor> descriptors)
{
var enumerable = descriptors as TDescriptor[] ?? descriptors.ToArray();
_descriptors = enumerable;
Default = enumerable.FirstOrDefault();
}

public RequestDescriptor(params TDescriptor[] descriptors)
{
var enumerable = descriptors.ToArray();
_descriptors = enumerable;
Default = enumerable.FirstOrDefault();
}

public IEnumerator<TDescriptor> GetEnumerator() => _descriptors.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable) _descriptors).GetEnumerator();

public TDescriptor Default { get; }
}
}
8 changes: 4 additions & 4 deletions src/JsonRpc/RequestRouter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ private IHandlerDescriptor FindDescriptor(IMethodWithParams instance)
return _collection.FirstOrDefault(x => x.Method == instance.Method);
}

public override IHandlerDescriptor GetDescriptor(Notification notification)
public override IRequestDescriptor<IHandlerDescriptor> GetDescriptors(Notification notification)
{
return FindDescriptor(notification);
return new RequestDescriptor<IHandlerDescriptor>(FindDescriptor(notification));
}

public override IHandlerDescriptor GetDescriptor(Request request)
public override IRequestDescriptor<IHandlerDescriptor> GetDescriptors(Request request)
{
return FindDescriptor(request);
return new RequestDescriptor<IHandlerDescriptor>(FindDescriptor(request));
}
}
}
Loading