Skip to content

Commit 2311cbb

Browse files
IHandlersManager and Handler linking improvments (#334)
* Added extension method to get all the unique handlers contained in the IHandlersManager * renamed link parameter names to better reflect their meaning. Also added exceptions to help users diagnose issues * Added unit tests * Added dap tests
1 parent 085ef9c commit 2311cbb

18 files changed

+209
-29
lines changed

src/Client/LanguageClientOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHand
6464
ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHandler(string method, Type type, JsonRpcHandlerOptions options) =>
6565
AddHandler(method, type, options);
6666

67-
ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHandlerLink(string sourceMethod, string destinationMethod) =>
68-
AddHandlerLink(sourceMethod, destinationMethod);
67+
ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.AddHandlerLink(string fromMethod, string toMethod) =>
68+
AddHandlerLink(fromMethod, toMethod);
6969

7070
ILanguageClientRegistry IJsonRpcHandlerRegistry<ILanguageClientRegistry>.OnJsonRequest(string method, Func<JToken, Task<JToken>> handler, JsonRpcHandlerOptions options) =>
7171
OnJsonRequest(method, handler, options);

src/Dap.Client/DebugAdapterClientOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ IDebugAdapterClientRegistry IJsonRpcHandlerRegistry<IDebugAdapterClientRegistry>
5454
IDebugAdapterClientRegistry IJsonRpcHandlerRegistry<IDebugAdapterClientRegistry>.AddHandler(string method, Type type, JsonRpcHandlerOptions options) =>
5555
AddHandler(method, type, options);
5656

57-
IDebugAdapterClientRegistry IJsonRpcHandlerRegistry<IDebugAdapterClientRegistry>.AddHandlerLink(string sourceMethod, string destinationMethod) =>
58-
AddHandlerLink(sourceMethod, destinationMethod);
57+
IDebugAdapterClientRegistry IJsonRpcHandlerRegistry<IDebugAdapterClientRegistry>.AddHandlerLink(string fromMethod, string toMethod) =>
58+
AddHandlerLink(fromMethod, toMethod);
5959

6060
IDebugAdapterClientRegistry IJsonRpcHandlerRegistry<IDebugAdapterClientRegistry>.OnJsonRequest(
6161
string method, Func<JToken, Task<JToken>> handler, JsonRpcHandlerOptions options

src/Dap.Server/DebugAdapterServerOptions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>
4242
IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.AddHandler(string method, Type type, JsonRpcHandlerOptions options) =>
4343
AddHandler(method, type, options);
4444

45-
IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.AddHandlerLink(string sourceMethod, string destinationMethod) =>
46-
AddHandlerLink(sourceMethod, destinationMethod);
45+
IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.AddHandlerLink(string fromMethod, string toMethod) =>
46+
AddHandlerLink(fromMethod, toMethod);
4747

4848
IDebugAdapterServerRegistry IJsonRpcHandlerRegistry<IDebugAdapterServerRegistry>.OnJsonRequest(
4949
string method, Func<JToken, Task<JToken>> handler, JsonRpcHandlerOptions options

src/Dap.Shared/DebugAdapterHandlerCollection.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,26 @@ public IDisposable Add(string method, Type handlerType, JsonRpcHandlerOptions op
4343
method, ActivatorUtilities.CreateInstance(_serviceProvider, handlerType) as IJsonRpcHandler, options
4444
);
4545

46-
IDisposable IHandlersManager.AddLink(string sourceMethod, string destinationMethod)
46+
IDisposable IHandlersManager.AddLink(string fromMethod, string toMethod)
4747
{
48-
var source = _descriptors.First(z => z.Method == sourceMethod);
48+
var source = _descriptors.FirstOrDefault(z => z.Method == fromMethod);
49+
if (source == null)
50+
{
51+
if (_descriptors.Any(z => z.Method == toMethod))
52+
{
53+
throw new ArgumentException(
54+
$"Could not find descriptor for '{fromMethod}', but I did find one for '{toMethod}'. Did you mean to link '{toMethod}' to '{fromMethod}' instead?", fromMethod
55+
);
56+
}
57+
58+
throw new ArgumentException(
59+
$"Could not find descriptor for '{fromMethod}', has it been registered yet? Descriptors must be registered before links can be created!", nameof(fromMethod)
60+
);
61+
}
62+
4963
HandlerDescriptor descriptor = null;
5064
descriptor = GetDescriptor(
51-
destinationMethod,
65+
toMethod,
5266
source.HandlerType,
5367
source.Handler,
5468
source.RequestProcessType.HasValue ? new JsonRpcHandlerOptions { RequestProcessType = source.RequestProcessType.Value } : null,

src/JsonRpc/CompositeHandlersManager.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public IDisposable Add(string method, Type handlerType, JsonRpcHandlerOptions op
5555
return result;
5656
}
5757

58-
public IDisposable AddLink(string sourceMethod, string destinationMethod)
58+
public IDisposable AddLink(string fromMethod, string toMethod)
5959
{
60-
var result = _parent.AddLink(sourceMethod,destinationMethod);
60+
var result = _parent.AddLink(fromMethod,toMethod);
6161
_compositeDisposable.Add(result);
6262
return result;
6363
}

src/JsonRpc/HandlerCollection.cs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,24 @@ public IDisposable Add(string method, Type handlerType, JsonRpcHandlerOptions op
9191
method, ActivatorUtilities.CreateInstance(_serviceProvider, handlerType) as IJsonRpcHandler, options
9292
);
9393

94-
public IDisposable AddLink(string sourceMethod, string destinationMethod)
94+
public IDisposable AddLink(string fromMethod, string toMethod)
9595
{
96-
var source = _descriptors.FirstOrDefault(z => z.Method == sourceMethod);
97-
var descriptor = new LinkedHandler(destinationMethod, source, () => _descriptors.RemoveAll(z => z.Method == destinationMethod));
96+
var source = _descriptors.FirstOrDefault(z => z.Method == fromMethod);
97+
if (source == null)
98+
{
99+
if (_descriptors.Any(z => z.Method == toMethod))
100+
{
101+
throw new ArgumentException(
102+
$"Could not find descriptor for '{fromMethod}', but I did find one for '{toMethod}'. Did you mean to link '{toMethod}' to '{fromMethod}' instead?", fromMethod
103+
);
104+
}
105+
106+
throw new ArgumentException(
107+
$"Could not find descriptor for '{fromMethod}', has it been registered yet? Descriptors must be registered before links can be created!", nameof(fromMethod)
108+
);
109+
}
110+
111+
var descriptor = new LinkedHandler(toMethod, source, () => _descriptors.RemoveAll(z => z.Method == toMethod));
98112
ImmutableInterlocked.InterlockedExchange(ref _descriptors, _descriptors.Add(descriptor));
99113
return descriptor;
100114
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
4+
namespace OmniSharp.Extensions.JsonRpc
5+
{
6+
public static class HandlersManagerExtensions
7+
{
8+
/// <summary>
9+
/// Gets all the unique handlers currently registered with the manager
10+
/// </summary>
11+
/// <param name="handlersManager"></param>
12+
/// <returns></returns>
13+
public static IEnumerable<IJsonRpcHandler> GetHandlers(this IHandlersManager handlersManager)
14+
{
15+
return handlersManager.Descriptors.Select(z => z.Handler).Distinct();
16+
}
17+
}
18+
}

src/JsonRpc/IHandlersManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public interface IHandlersManager
1111
IDisposable Add(string method, JsonRpcHandlerFactory factory, JsonRpcHandlerOptions options);
1212
IDisposable Add(Type handlerType, JsonRpcHandlerOptions options);
1313
IDisposable Add(string method, Type handlerType, JsonRpcHandlerOptions options);
14-
IDisposable AddLink(string sourceMethod, string destinationMethod);
14+
IDisposable AddLink(string fromMethod, string toMethod);
1515
IEnumerable<IHandlerDescriptor> Descriptors { get; }
1616
}
1717
}

src/JsonRpc/IJsonRpcHandlerRegistry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public interface IJsonRpcHandlerRegistry<out T> : IJsonRpcHandlerRegistry where
2525
T AddHandler<TTHandler>(string method, JsonRpcHandlerOptions options = null) where TTHandler : IJsonRpcHandler;
2626
T AddHandler(Type type, JsonRpcHandlerOptions options = null);
2727
T AddHandler(string method, Type type, JsonRpcHandlerOptions options = null);
28-
T AddHandlerLink(string sourceMethod, string destinationMethod);
28+
T AddHandlerLink(string fromMethod, string toMethod);
2929

3030
T OnJsonRequest(string method, Func<JToken, Task<JToken>> handler, JsonRpcHandlerOptions options = null);
3131
T OnJsonRequest(string method, Func<JToken, CancellationToken, Task<JToken>> handler, JsonRpcHandlerOptions options = null);

src/JsonRpc/InterimJsonRpcServerRegistry.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ public sealed override T AddHandler(string method, Type type, JsonRpcHandlerOpti
5858
return (T) (object) this;
5959
}
6060

61-
public sealed override T AddHandlerLink(string sourceMethod, string destinationMethod)
61+
public sealed override T AddHandlerLink(string fromMethod, string toMethod)
6262
{
63-
_handlersManager.AddLink(sourceMethod, destinationMethod);
63+
_handlersManager.AddLink(fromMethod, toMethod);
6464
return (T) (object) this;
6565
}
6666
}

0 commit comments

Comments
 (0)