-
-
Notifications
You must be signed in to change notification settings - Fork 15
/
client.go
413 lines (309 loc) · 15.2 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// SPDX-FileCopyrightText: 2019 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package protocol
import (
"bytes"
"context"
"fmt"
"github.com/segmentio/encoding/json"
"go.uber.org/zap"
"go.lsp.dev/jsonrpc2"
"go.lsp.dev/pkg/xcontext"
)
// ClientDispatcher returns a Client that dispatches LSP requests across the
// given jsonrpc2 connection.
func ClientDispatcher(conn jsonrpc2.Conn, logger *zap.Logger) Client {
return &client{
Conn: conn,
logger: logger,
}
}
// ClientHandler handler of LSP client.
func ClientHandler(client Client, handler jsonrpc2.Handler) jsonrpc2.Handler {
h := func(ctx context.Context, reply jsonrpc2.Replier, req jsonrpc2.Request) error {
if ctx.Err() != nil {
xctx := xcontext.Detach(ctx)
return reply(xctx, nil, ErrRequestCancelled)
}
handled, err := clientDispatch(ctx, client, reply, req)
if handled || err != nil {
return err
}
return handler(ctx, reply, req)
}
return h
}
// clientDispatch implements jsonrpc2.Handler.
//
//nolint:funlen,cyclop
func clientDispatch(ctx context.Context, client Client, reply jsonrpc2.Replier, req jsonrpc2.Request) (handled bool, err error) {
if ctx.Err() != nil {
return true, reply(ctx, nil, ErrRequestCancelled)
}
dec := json.NewDecoder(bytes.NewReader(req.Params()))
logger := LoggerFromContext(ctx)
switch req.Method() {
case MethodProgress: // notification
defer logger.Debug(MethodProgress, zap.Error(err))
var params ProgressParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.Progress(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkDoneProgressCreate: // request
defer logger.Debug(MethodWorkDoneProgressCreate, zap.Error(err))
var params WorkDoneProgressCreateParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.WorkDoneProgressCreate(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowLogMessage: // notification
defer logger.Debug(MethodWindowLogMessage, zap.Error(err))
var params LogMessageParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.LogMessage(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodTextDocumentPublishDiagnostics: // notification
defer logger.Debug(MethodTextDocumentPublishDiagnostics, zap.Error(err))
var params PublishDiagnosticsParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.PublishDiagnostics(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowShowMessage: // notification
defer logger.Debug(MethodWindowShowMessage, zap.Error(err))
var params ShowMessageParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.ShowMessage(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWindowShowMessageRequest: // request
defer logger.Debug(MethodWindowShowMessageRequest, zap.Error(err))
var params ShowMessageRequestParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.ShowMessageRequest(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodTelemetryEvent: // notification
defer logger.Debug(MethodTelemetryEvent, zap.Error(err))
var params interface{}
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.Telemetry(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodClientRegisterCapability: // request
defer logger.Debug(MethodClientRegisterCapability, zap.Error(err))
var params RegistrationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.RegisterCapability(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodClientUnregisterCapability: // request
defer logger.Debug(MethodClientUnregisterCapability, zap.Error(err))
var params UnregistrationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
err := client.UnregisterCapability(ctx, ¶ms)
return true, reply(ctx, nil, err)
case MethodWorkspaceApplyEdit: // request
defer logger.Debug(MethodWorkspaceApplyEdit, zap.Error(err))
var params ApplyWorkspaceEditParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.ApplyEdit(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceConfiguration: // request
defer logger.Debug(MethodWorkspaceConfiguration, zap.Error(err))
var params ConfigurationParams
if err := dec.Decode(¶ms); err != nil {
return true, replyParseError(ctx, reply, err)
}
resp, err := client.Configuration(ctx, ¶ms)
return true, reply(ctx, resp, err)
case MethodWorkspaceWorkspaceFolders: // request
defer logger.Debug(MethodWorkspaceWorkspaceFolders, zap.Error(err))
if len(req.Params()) > 0 {
return true, reply(ctx, nil, fmt.Errorf("expected no params: %w", jsonrpc2.ErrInvalidParams))
}
resp, err := client.WorkspaceFolders(ctx)
return true, reply(ctx, resp, err)
default:
return false, nil
}
}
// Client represents a Language Server Protocol client.
type Client interface {
Progress(ctx context.Context, params *ProgressParams) (err error)
WorkDoneProgressCreate(ctx context.Context, params *WorkDoneProgressCreateParams) (err error)
LogMessage(ctx context.Context, params *LogMessageParams) (err error)
PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error)
ShowMessage(ctx context.Context, params *ShowMessageParams) (err error)
ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (result *MessageActionItem, err error)
Telemetry(ctx context.Context, params interface{}) (err error)
RegisterCapability(ctx context.Context, params *RegistrationParams) (err error)
UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error)
ApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result *ApplyWorkspaceEditResponse, err error)
Configuration(ctx context.Context, params *ConfigurationParams) (result []interface{}, err error)
WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error)
}
// list of client methods.
const (
// MethodProgress method name of "$/progress".
MethodProgress = "$/progress"
// MethodWorkDoneProgressCreate method name of "window/workDoneProgress/create".
MethodWorkDoneProgressCreate = "window/workDoneProgress/create"
// MethodWindowShowMessage method name of "window/showMessage".
MethodWindowShowMessage = "window/showMessage"
// MethodWindowShowMessageRequest method name of "window/showMessageRequest.
MethodWindowShowMessageRequest = "window/showMessageRequest"
// MethodWindowLogMessage method name of "window/logMessage.
MethodWindowLogMessage = "window/logMessage"
// MethodTelemetryEvent method name of "telemetry/event.
MethodTelemetryEvent = "telemetry/event"
// MethodClientRegisterCapability method name of "client/registerCapability.
MethodClientRegisterCapability = "client/registerCapability"
// MethodClientUnregisterCapability method name of "client/unregisterCapability.
MethodClientUnregisterCapability = "client/unregisterCapability"
// MethodTextDocumentPublishDiagnostics method name of "textDocument/publishDiagnostics.
MethodTextDocumentPublishDiagnostics = "textDocument/publishDiagnostics"
// MethodWorkspaceApplyEdit method name of "workspace/applyEdit.
MethodWorkspaceApplyEdit = "workspace/applyEdit"
// MethodWorkspaceConfiguration method name of "workspace/configuration.
MethodWorkspaceConfiguration = "workspace/configuration"
// MethodWorkspaceWorkspaceFolders method name of "workspace/workspaceFolders".
MethodWorkspaceWorkspaceFolders = "workspace/workspaceFolders"
)
// client implements a Language Server Protocol client.
type client struct {
jsonrpc2.Conn
logger *zap.Logger
}
// compiler time check whether the Client implements ClientInterface interface.
var _ Client = (*client)(nil)
// Progress is the base protocol offers also support to report progress in a generic fashion.
//
// This mechanism can be used to report any kind of progress including work done progress (usually used to report progress in the user interface using a progress bar) and
// partial result progress to support streaming of results.
//
// @since 3.16.0.
func (c *client) Progress(ctx context.Context, params *ProgressParams) (err error) {
c.logger.Debug("call " + MethodProgress)
defer c.logger.Debug("end "+MethodProgress, zap.Error(err))
return c.Conn.Notify(ctx, MethodProgress, params)
}
// WorkDoneProgressCreate sends the request is sent from the server to the client to ask the client to create a work done progress.
//
// @since 3.16.0.
func (c *client) WorkDoneProgressCreate(ctx context.Context, params *WorkDoneProgressCreateParams) (err error) {
c.logger.Debug("call " + MethodWorkDoneProgressCreate)
defer c.logger.Debug("end "+MethodWorkDoneProgressCreate, zap.Error(err))
return Call(ctx, c.Conn, MethodWorkDoneProgressCreate, params, nil)
}
// LogMessage sends the notification from the server to the client to ask the client to log a particular message.
func (c *client) LogMessage(ctx context.Context, params *LogMessageParams) (err error) {
c.logger.Debug("call " + MethodWindowLogMessage)
defer c.logger.Debug("end "+MethodWindowLogMessage, zap.Error(err))
return c.Conn.Notify(ctx, MethodWindowLogMessage, params)
}
// PublishDiagnostics sends the notification from the server to the client to signal results of validation runs.
//
// Diagnostics are “owned” by the server so it is the server’s responsibility to clear them if necessary. The following rule is used for VS Code servers that generate diagnostics:
//
// - if a language is single file only (for example HTML) then diagnostics are cleared by the server when the file is closed.
// - if a language has a project system (for example C#) diagnostics are not cleared when a file closes. When a project is opened all diagnostics for all files are recomputed (or read from a cache).
//
// When a file changes it is the server’s responsibility to re-compute diagnostics and push them to the client.
// If the computed set is empty it has to push the empty array to clear former diagnostics.
// Newly pushed diagnostics always replace previously pushed diagnostics. There is no merging that happens on the client side.
func (c *client) PublishDiagnostics(ctx context.Context, params *PublishDiagnosticsParams) (err error) {
c.logger.Debug("call " + MethodTextDocumentPublishDiagnostics)
defer c.logger.Debug("end "+MethodTextDocumentPublishDiagnostics, zap.Error(err))
return c.Conn.Notify(ctx, MethodTextDocumentPublishDiagnostics, params)
}
// ShowMessage sends the notification from a server to a client to ask the
// client to display a particular message in the user interface.
func (c *client) ShowMessage(ctx context.Context, params *ShowMessageParams) (err error) {
return c.Conn.Notify(ctx, MethodWindowShowMessage, params)
}
// ShowMessageRequest sends the request from a server to a client to ask the client to display a particular message in the user interface.
//
// In addition to the show message notification the request allows to pass actions and to wait for an answer from the client.
func (c *client) ShowMessageRequest(ctx context.Context, params *ShowMessageRequestParams) (_ *MessageActionItem, err error) {
c.logger.Debug("call " + MethodWindowShowMessageRequest)
defer c.logger.Debug("end "+MethodWindowShowMessageRequest, zap.Error(err))
var result *MessageActionItem
if err := Call(ctx, c.Conn, MethodWindowShowMessageRequest, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Telemetry sends the notification from the server to the client to ask the client to log a telemetry event.
func (c *client) Telemetry(ctx context.Context, params interface{}) (err error) {
c.logger.Debug("call " + MethodTelemetryEvent)
defer c.logger.Debug("end "+MethodTelemetryEvent, zap.Error(err))
return c.Conn.Notify(ctx, MethodTelemetryEvent, params)
}
// RegisterCapability sends the request from the server to the client to register for a new capability on the client side.
//
// Not all clients need to support dynamic capability registration.
//
// A client opts in via the dynamicRegistration property on the specific client capabilities.
// A client can even provide dynamic registration for capability A but not for capability B (see TextDocumentClientCapabilities as an example).
func (c *client) RegisterCapability(ctx context.Context, params *RegistrationParams) (err error) {
c.logger.Debug("call " + MethodClientRegisterCapability)
defer c.logger.Debug("end "+MethodClientRegisterCapability, zap.Error(err))
return Call(ctx, c.Conn, MethodClientRegisterCapability, params, nil)
}
// UnregisterCapability sends the request from the server to the client to unregister a previously registered capability.
func (c *client) UnregisterCapability(ctx context.Context, params *UnregistrationParams) (err error) {
c.logger.Debug("call " + MethodClientUnregisterCapability)
defer c.logger.Debug("end "+MethodClientUnregisterCapability, zap.Error(err))
return Call(ctx, c.Conn, MethodClientUnregisterCapability, params, nil)
}
// ApplyEdit sends the request from the server to the client to modify resource on the client side.
func (c *client) ApplyEdit(ctx context.Context, params *ApplyWorkspaceEditParams) (result *ApplyWorkspaceEditResponse, err error) {
c.logger.Debug("call " + MethodWorkspaceApplyEdit)
defer c.logger.Debug("end "+MethodWorkspaceApplyEdit, zap.Error(err))
if err := Call(ctx, c.Conn, MethodWorkspaceApplyEdit, params, &result); err != nil {
return nil, err
}
return result, nil
}
// Configuration sends the request from the server to the client to fetch configuration settings from the client.
//
// The request can fetch several configuration settings in one roundtrip.
// The order of the returned configuration settings correspond to the order of the
// passed ConfigurationItems (e.g. the first item in the response is the result for the first configuration item in the params).
func (c *client) Configuration(ctx context.Context, params *ConfigurationParams) (_ []interface{}, err error) {
c.logger.Debug("call " + MethodWorkspaceConfiguration)
defer c.logger.Debug("end "+MethodWorkspaceConfiguration, zap.Error(err))
var result []interface{}
if err := Call(ctx, c.Conn, MethodWorkspaceConfiguration, params, &result); err != nil {
return nil, err
}
return result, nil
}
// WorkspaceFolders sends the request from the server to the client to fetch the current open list of workspace folders.
//
// Returns null in the response if only a single file is open in the tool. Returns an empty array if a workspace is open but no folders are configured.
//
// @since 3.6.0.
func (c *client) WorkspaceFolders(ctx context.Context) (result []WorkspaceFolder, err error) {
c.logger.Debug("call " + MethodWorkspaceWorkspaceFolders)
defer c.logger.Debug("end "+MethodWorkspaceWorkspaceFolders, zap.Error(err))
if err := Call(ctx, c.Conn, MethodWorkspaceWorkspaceFolders, nil, &result); err != nil {
return nil, err
}
return result, nil
}