4
4
using System . Threading ;
5
5
using System . Threading . Tasks ;
6
6
using Newtonsoft . Json . Linq ;
7
+ using OmniSharp . Extensions . JsonRpc ;
7
8
using OmniSharp . Extensions . LanguageServer . Client . Handlers ;
8
9
9
10
namespace OmniSharp . Extensions . LanguageServer . Client . Dispatcher
@@ -21,10 +22,22 @@ public class LspDispatcher
21
22
/// <summary>
22
23
/// Create a new <see cref="LspDispatcher"/>.
23
24
/// </summary>
24
- public LspDispatcher ( )
25
+ /// <param name="serializer">
26
+ /// The JSON serialiser for notification / request / response payloads.
27
+ /// </param>
28
+ public LspDispatcher ( ISerializer serializer )
25
29
{
30
+ if ( serializer == null )
31
+ throw new ArgumentNullException ( nameof ( serializer ) ) ;
32
+
33
+ Serializer = serializer ;
26
34
}
27
35
36
+ /// <summary>
37
+ /// The JSON serialiser to use for notification / request / response payloads.
38
+ /// </summary>
39
+ public ISerializer Serializer { get ; set ; }
40
+
28
41
/// <summary>
29
42
/// Register a handler invoker.
30
43
/// </summary>
@@ -92,7 +105,9 @@ public async Task<bool> TryHandleNotification(string method, JObject notificatio
92
105
93
106
if ( _handlers . TryGetValue ( method , out IHandler handler ) && handler is IInvokeNotificationHandler notificationHandler )
94
107
{
95
- await notificationHandler . Invoke ( notification ) ;
108
+ object notificationPayload = DeserializePayload ( notificationHandler . PayloadType , notification ) ;
109
+
110
+ await notificationHandler . Invoke ( notificationPayload ) ;
96
111
97
112
return true ;
98
113
}
@@ -121,9 +136,36 @@ public Task<object> TryHandleRequest(string method, JObject request, Cancellatio
121
136
throw new ArgumentException ( $ "Argument cannot be null, empty, or entirely composed of whitespace: { nameof ( method ) } .", nameof ( method ) ) ;
122
137
123
138
if ( _handlers . TryGetValue ( method , out IHandler handler ) && handler is IInvokeRequestHandler requestHandler )
124
- return requestHandler . Invoke ( request , cancellationToken ) ;
139
+ {
140
+ object requestPayload = DeserializePayload ( requestHandler . PayloadType , request ) ;
141
+
142
+ return requestHandler . Invoke ( requestPayload , cancellationToken ) ;
143
+ }
125
144
126
145
return null ;
127
146
}
147
+
148
+ /// <summary>
149
+ /// Deserialise a notification / request payload from JSON.
150
+ /// </summary>
151
+ /// <param name="payloadType">
152
+ /// The payload's CLR type.
153
+ /// </param>
154
+ /// <param name="payload">
155
+ /// JSON representing the payload.
156
+ /// </param>
157
+ /// <returns>
158
+ /// The deserialised payload (if one is present and expected).
159
+ /// </returns>
160
+ object DeserializePayload ( Type payloadType , JObject payload )
161
+ {
162
+ if ( payloadType == null )
163
+ throw new ArgumentNullException ( nameof ( payloadType ) ) ;
164
+
165
+ if ( payloadType == null || payload == null )
166
+ return null ;
167
+
168
+ return payload . ToObject ( payloadType , Serializer . JsonSerializer ) ;
169
+ }
128
170
}
129
171
}
0 commit comments