-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlsp_msg_convert.go
623 lines (614 loc) · 19 KB
/
lsp_msg_convert.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
//
// Copyright 2024 Cristian Maglie. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
package lsp
import (
"go.bug.st/json"
)
// DecodeClientRequestParams parse a CLIENT-REQUEST (↩)
func DecodeClientRequestParams(method string, req json.RawMessage) (any, error) {
switch method {
case "initialize":
var res InitializeParams
return &res, json.Unmarshal(req, &res)
case "shutdown":
return nil, nil
case "workspace/symbol":
var res WorkspaceSymbolParams
return &res, json.Unmarshal(req, &res)
case "workspace/executeCommand":
var res ExecuteCommandParams
return &res, json.Unmarshal(req, &res)
case "workspace/willCreateFiles":
var res CreateFilesParams
return &res, json.Unmarshal(req, &res)
case "workspace/willRenameFiles":
var res RenameFilesParams
return &res, json.Unmarshal(req, &res)
case "workspace/willDeleteFiles":
var res DeleteFilesParams
return &res, json.Unmarshal(req, &res)
case "textDocument/willSaveWaitUntil":
var res WillSaveTextDocumentParams
return &res, json.Unmarshal(req, &res)
case "textDocument/completion":
var res CompletionParams
return &res, json.Unmarshal(req, &res)
case "completionItem/resolve":
var res CompletionItem
return &res, json.Unmarshal(req, &res)
case "textDocument/hover":
var res HoverParams
return &res, json.Unmarshal(req, &res)
case "textDocument/signatureHelp":
var res SignatureHelpParams
return &res, json.Unmarshal(req, &res)
case "textDocument/declaration":
var res DeclarationParams
return &res, json.Unmarshal(req, &res)
case "textDocument/definition":
var res DefinitionParams
return &res, json.Unmarshal(req, &res)
case "textDocument/typeDefinition":
var res TypeDefinitionParams
return &res, json.Unmarshal(req, &res)
case "textDocument/implementation":
var res ImplementationParams
return &res, json.Unmarshal(req, &res)
case "textDocument/references":
var res ReferenceParams
return &res, json.Unmarshal(req, &res)
case "textDocument/documentHighlight":
var res DocumentHighlightParams
return &res, json.Unmarshal(req, &res)
case "textDocument/documentSymbol":
var res DocumentSymbolParams
return &res, json.Unmarshal(req, &res)
case "textDocument/codeAction":
var res CodeActionParams
return &res, json.Unmarshal(req, &res)
case "codeAction/resolve":
var res CodeAction
return &res, json.Unmarshal(req, &res)
case "textDocument/codeLens":
var res CodeLensParams
return &res, json.Unmarshal(req, &res)
case "codeLens/resolve":
var res CodeLens
return &res, json.Unmarshal(req, &res)
case "textDocument/documentLink":
var res DocumentLinkParams
return &res, json.Unmarshal(req, &res)
case "documentLink/resolve":
var res DocumentLink
return &res, json.Unmarshal(req, &res)
case "textDocument/documentColor":
var res DocumentColorParams
return &res, json.Unmarshal(req, &res)
case "textDocument/colorPresentation":
var res ColorPresentationParams
return &res, json.Unmarshal(req, &res)
case "textDocument/formatting":
var res DocumentFormattingParams
return &res, json.Unmarshal(req, &res)
case "textDocument/rangeFormatting":
var res DocumentRangeFormattingParams
return &res, json.Unmarshal(req, &res)
case "textDocument/onTypeFormatting":
var res DocumentOnTypeFormattingParams
return &res, json.Unmarshal(req, &res)
case "textDocument/rename":
var res RenameParams
return &res, json.Unmarshal(req, &res)
case "textDocument/prepareRename":
var res PrepareRenameParams
return &res, json.Unmarshal(req, &res)
case "textDocument/foldingRange":
var res FoldingRangeParams
return &res, json.Unmarshal(req, &res)
case "textDocument/selectionRange":
var res SelectionRangeParams
return &res, json.Unmarshal(req, &res)
case "textDocument/prepareCallHierarchy":
var res CallHierarchyPrepareParams
return &res, json.Unmarshal(req, &res)
case "callHierarchy/incomingCalls":
var res CallHierarchyIncomingCallsParams
return &res, json.Unmarshal(req, &res)
case "callHierarchy/outgoingCalls":
var res CallHierarchyOutgoingCallsParams
return &res, json.Unmarshal(req, &res)
case "textDocument/semanticTokens/full":
var res SemanticTokensParams
return &res, json.Unmarshal(req, &res)
case "textDocument/semanticTokens/full/delta":
var res SemanticTokensDeltaParams
return &res, json.Unmarshal(req, &res)
case "textDocument/semanticTokens/range":
var res SemanticTokensRangeParams
return &res, json.Unmarshal(req, &res)
case "workspace/semanticTokens/refresh":
return nil, nil
case "textDocument/linkedEditingRange":
var res LinkedEditingRangeParams
return &res, json.Unmarshal(req, &res)
case "textDocument/moniker":
var res MonikerParams
return &res, json.Unmarshal(req, &res)
default:
panic("unimplemented message")
}
}
// DecodeServerResponseResult parse a SERVER-RESPONSE to a CLIENT-REQUEST (↩)
func DecodeServerResponseResult(method string, resp json.RawMessage) (interface{}, error) {
switch method {
case "initialize":
var res InitializeResult
return &res, json.Unmarshal(resp, &res)
case "shutdown":
return nil, nil
case "workspace/symbol":
// result: SymbolInformation[] | null
if string(resp) == "null" {
return []SymbolInformation{}, nil
}
var res []SymbolInformation
return &res, json.Unmarshal(resp, &res)
case "workspace/executeCommand":
// result: any | null
return resp, nil // passthrough
case "workspace/willCreateFiles":
// result: WorkspaceEdit | null
if string(resp) == "null" {
return nil, nil
}
var res WorkspaceEdit
return &res, json.Unmarshal(resp, &res)
case "workspace/willRenameFiles":
// result: WorkspaceEdit | null
if string(resp) == "null" {
return nil, nil
}
var res WorkspaceEdit
return &res, json.Unmarshal(resp, &res)
case "workspace/willDeleteFiles":
// result: WorkspaceEdit | null
if string(resp) == "null" {
return nil, nil
}
var res WorkspaceEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/willSaveWaitUntil":
// result: TextEdit[] | null
if string(resp) == "null" {
return nil, nil
}
var res []TextEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/completion":
// result: CompletionItem[] | CompletionList | null.
// If a CompletionItem[] is provided it is interpreted to be complete. So it is the same as { isIncomplete: false, items }
var completionItems []CompletionItem
if err := json.Unmarshal(resp, &completionItems); err == nil {
return &CompletionList{
IsIncomplete: false,
Items: completionItems,
}, nil
}
if string(resp) == "null" {
return &CompletionList{
IsIncomplete: false,
Items: []CompletionItem{},
}, nil
}
var res CompletionList
return &res, json.Unmarshal(resp, &res)
case "completionItem/resolve":
var res CompletionItem
return &res, json.Unmarshal(resp, &res)
case "textDocument/hover":
// result: Hover | null
if string(resp) == "null" {
return nil, nil
}
var res Hover
return &res, json.Unmarshal(resp, &res)
case "textDocument/signatureHelp":
// result: SignatureHelp | null
if string(resp) == "null" {
return nil, nil
}
var res SignatureHelp
return &res, json.Unmarshal(resp, &res)
case "textDocument/declaration":
// result: Location | Location[] | LocationLink[] |null
if string(resp) == "null" {
return nil, nil
}
var location Location
if err := json.Unmarshal(resp, &location); err == nil {
return &location, nil
}
var locations []Location
if err := json.Unmarshal(resp, &locations); err == nil {
return &locations, nil
}
var locationLinks []LocationLink
return &locationLinks, json.Unmarshal(resp, &locationLinks)
case "textDocument/definition":
// result: Location | Location[] | LocationLink[] | null
if string(resp) == "null" {
return nil, nil
}
var location Location
if err := json.Unmarshal(resp, &location); err == nil {
return &location, nil
}
var locations []Location
if err := json.Unmarshal(resp, &locations); err == nil {
return &locations, nil
}
var locationLinks []LocationLink
return &locationLinks, json.Unmarshal(resp, &locationLinks)
case "textDocument/typeDefinition":
// result: Location | Location[] | LocationLink[] | null
if string(resp) == "null" {
return nil, nil
}
var location Location
if err := json.Unmarshal(resp, &location); err == nil {
return &location, nil
}
var locations []Location
if err := json.Unmarshal(resp, &locations); err == nil {
return &locations, nil
}
var locationLinks []LocationLink
return &locationLinks, json.Unmarshal(resp, &locationLinks)
case "textDocument/implementation":
// result: Location | Location[] | LocationLink[] | null
if string(resp) == "null" {
return nil, nil
}
var location Location
if err := json.Unmarshal(resp, &location); err == nil {
return &location, nil
}
var locations []Location
if err := json.Unmarshal(resp, &locations); err == nil {
return &locations, nil
}
var locationLinks []LocationLink
return &locationLinks, json.Unmarshal(resp, &locationLinks)
case "textDocument/references":
// result: Location[] | null
if string(resp) == "null" {
return nil, nil
}
var locations []Location
return &locations, json.Unmarshal(resp, &locations)
case "textDocument/documentHighlight":
// result: DocumentHighlight[] | null
if string(resp) == "null" {
return nil, nil
}
var res []DocumentHighlight
return &res, json.Unmarshal(resp, &res)
case "textDocument/documentSymbol":
// result: DocumentSymbol[] | SymbolInformation[] | null
if string(resp) == "null" {
return nil, nil
}
var documentSymbols []DocumentSymbol
if err := json.Unmarshal(resp, &documentSymbols); err == nil {
return documentSymbols, nil
}
var symbolInfomation []SymbolInformation
return &symbolInfomation, json.Unmarshal(resp, &symbolInfomation)
case "textDocument/codeAction":
// result: (Command | CodeAction)[] | null
if string(resp) == "null" {
return nil, nil
}
var res []CommandOrCodeAction
return res, json.Unmarshal(resp, &res)
case "codeAction/resolve":
var res CodeAction
return &res, json.Unmarshal(resp, &res)
case "textDocument/codeLens":
// result: CodeLens[] | null
if string(resp) == "null" {
return nil, nil
}
var res []CodeLens
return &res, json.Unmarshal(resp, &res)
case "codeLens/resolve":
var res CodeLens
return &res, json.Unmarshal(resp, &res)
case "textDocument/documentLink":
// result: DocumentLink[] | null
if string(resp) == "null" {
return nil, nil
}
var res []DocumentLink
return &res, json.Unmarshal(resp, &res)
case "documentLink/resolve":
var res DocumentLink
return &res, json.Unmarshal(resp, &res)
case "textDocument/documentColor":
var res []ColorInformation
return &res, json.Unmarshal(resp, &res)
case "textDocument/colorPresentation":
var res []ColorPresentation
return &res, json.Unmarshal(resp, &res)
case "textDocument/formatting":
// result: TextEdit[] | null
if string(resp) == "null" {
return nil, nil
}
var res []TextEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/rangeFormatting":
// result: TextEdit[] | null
if string(resp) == "null" {
return nil, nil
}
var res []TextEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/onTypeFormatting":
// result: TextEdit[] | null
if string(resp) == "null" {
return nil, nil
}
var res []TextEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/rename":
// result: WorkspaceEdit | null
if string(resp) == "null" {
return nil, nil
}
var res WorkspaceEdit
return &res, json.Unmarshal(resp, &res)
case "textDocument/prepareRename":
// result: Range | { range: Range, placeholder: string } | { defaultBehavior: boolean } | null
panic("unimplemented")
case "textDocument/foldingRange":
// result: FoldingRange[] | null
if string(resp) == "null" {
return nil, nil
}
var res []FoldingRange
return &res, json.Unmarshal(resp, &res)
case "textDocument/selectionRange":
// result: SelectionRange[] | null
if string(resp) == "null" {
return nil, nil
}
var res []SelectionRange
return &res, json.Unmarshal(resp, &res)
case "textDocument/prepareCallHierarchy":
// result: CallHierarchyItem[] | null
if string(resp) == "null" {
return nil, nil
}
var res []CallHierarchyItem
return &res, json.Unmarshal(resp, &res)
case "callHierarchy/incomingCalls":
// result: CallHierarchyIncomingCall[] | null
if string(resp) == "null" {
return nil, nil
}
var res []CallHierarchyIncomingCall
return &res, json.Unmarshal(resp, &res)
case "callHierarchy/outgoingCalls":
// result: CallHierarchyOutgoingCall[] | null
if string(resp) == "null" {
return nil, nil
}
var res []CallHierarchyOutgoingCall
return &res, json.Unmarshal(resp, &res)
case "textDocument/semanticTokens/full":
// result: SemanticTokens | null
if string(resp) == "null" {
return nil, nil
}
var res SemanticTokens
return &res, json.Unmarshal(resp, &res)
case "textDocument/semanticTokens/full/delta":
// result: SemanticTokens | SemanticTokensDelta | null
if string(resp) == "null" {
return nil, nil
}
var delta SemanticTokensDelta
if err := json.Unmarshal(resp, &delta); err == nil {
return &delta, nil
}
var res SemanticTokens
return &res, json.Unmarshal(resp, &res)
case "textDocument/semanticTokens/range":
// result: SemanticTokens | null
if string(resp) == "null" {
return nil, nil
}
var res SemanticTokens
return &res, json.Unmarshal(resp, &res)
case "workspace/semanticTokens/refresh":
return nil, nil
case "textDocument/linkedEditingRange":
// result: LinkedEditingRanges | null
if string(resp) == "null" {
return nil, nil
}
var res LinkedEditingRanges
return &res, json.Unmarshal(resp, &res)
case "textDocument/moniker":
// result: Moniker[] | null
if string(resp) == "null" {
return nil, nil
}
var res []Moniker
return &res, json.Unmarshal(resp, &res)
default:
panic("unimplemented message")
}
}
// DecodeServerRequestParams parse a SERVER-REQUEST (↪)
func DecodeServerRequestParams(method string, req json.RawMessage) (interface{}, error) {
switch method {
case "window/showMessageRequest":
var res ShowMessageRequestParams
return &res, json.Unmarshal(req, &res)
case "window/showDocument":
var res ShowDocumentParams
return &res, json.Unmarshal(req, &res)
case "window/workDoneProgress/create":
var res WorkDoneProgressCreateParams
return &res, json.Unmarshal(req, &res)
case "client/registerCapability":
var res RegistrationParams
return &res, json.Unmarshal(req, &res)
case "client/unregisterCapability":
var res UnregistrationParams
return &res, json.Unmarshal(req, &res)
case "workspace/workspaceFolders":
return nil, nil
case "workspace/configuration":
var res ConfigurationParams
return &res, json.Unmarshal(req, &res)
case "workspace/applyEdit":
var res ApplyWorkspaceEditParams
return &res, json.Unmarshal(req, &res)
case "workspace/codeLens/refresh":
return nil, nil
default:
panic("unimplemented message")
}
}
// DecodeClientResponseResult parse a CLIENT-RESPONSE to a SERVER-REQUEST (↪)
func DecodeClientResponseResult(method string, resp json.RawMessage) (interface{}, error) {
switch method {
case "window/showMessageRequest":
// result: the selected MessageActionItem | null if none got selected
if string(resp) == "null" {
return nil, nil
}
var res MessageActionItem
return &res, json.Unmarshal(resp, &res)
case "window/showDocument":
var res ShowDocumentResult
return &res, json.Unmarshal(resp, &res)
case "window/workDoneProgress/create":
return nil, nil
case "client/registerCapability":
return nil, nil
case "client/unregisterCapability":
return nil, nil
case "workspace/workspaceFolders":
// result: WorkspaceFolder[] | null
if string(resp) == "null" {
return nil, nil
}
var res []WorkspaceFolder
return res, json.Unmarshal(resp, &res)
case "workspace/configuration":
// result: any[]
var res []json.RawMessage
return res, json.Unmarshal(resp, &res)
case "workspace/applyEdit":
var res ApplyWorkspaceEditResult
return &res, json.Unmarshal(resp, &res)
case "workspace/codeLens/refresh":
return nil, nil
default:
panic("unimplemented message")
}
}
// DecodeClientNotificationParams parse a CLIENT-NOTIFICATION (→)
func DecodeClientNotificationParams(method string, req json.RawMessage) (interface{}, error) {
switch method {
case "$/progress":
var res ProgressParams
return &res, json.Unmarshal(req, &res)
case "$/cancelRequrest":
panic("should not reach here")
case "initialized":
var res InitializedParams
return &res, json.Unmarshal(req, &res)
case "exit":
return nil, nil
case "$/setTrace", "$/setTraceNotification":
var res SetTraceParams
return &res, json.Unmarshal(req, &res)
case "window/workDoneProgress/cancel":
var res WorkDoneProgressCancelParams
return &res, json.Unmarshal(req, &res)
case "workspace/didChangeWorkspaceFolders":
var res DidChangeWorkspaceFoldersParams
return &res, json.Unmarshal(req, &res)
case "workspace/didChangeConfiguration":
var res DidChangeConfigurationParams
return &res, json.Unmarshal(req, &res)
case "workspace/didChangeWatchedFiles":
var res DidChangeWatchedFilesParams
return &res, json.Unmarshal(req, &res)
case "workspace/didCreateFiles":
var res CreateFilesParams
return &res, json.Unmarshal(req, &res)
case "workspace/didRenameFiles":
var res RenameFilesParams
return &res, json.Unmarshal(req, &res)
case "workspace/didDeleteFiles":
var res DeleteFilesParams
return &res, json.Unmarshal(req, &res)
case "textDocument/didOpen":
var res DidOpenTextDocumentParams
return &res, json.Unmarshal(req, &res)
case "textDocument/didChange":
var res DidChangeTextDocumentParams
return &res, json.Unmarshal(req, &res)
case "textDocument/willSave":
var res WillSaveTextDocumentParams
return &res, json.Unmarshal(req, &res)
case "textDocument/didSave":
var res DidSaveTextDocumentParams
return &res, json.Unmarshal(req, &res)
case "textDocument/didClose":
var res DidCloseTextDocumentParams
return &res, json.Unmarshal(req, &res)
default:
panic("unimplemented message")
}
}
// DecodeServerNotificationParams parse a SERVER-NOTIFICATION (←)
func DecodeServerNotificationParams(method string, req json.RawMessage) (interface{}, error) {
switch method {
case "$/progress":
var res ProgressParams
return &res, json.Unmarshal(req, &res)
case "$/cancelRequrest":
panic("should not reach here")
case "$/logTrace":
var res LogTraceParams
return &res, json.Unmarshal(req, &res)
case "window/showMessage":
var res ShowMessageParams
return &res, json.Unmarshal(req, &res)
case "window/logMessage":
var res LogMessageParams
return &res, json.Unmarshal(req, &res)
case "telemetry/event":
// params: ‘object’ | ‘number’ | ‘boolean’ | ‘string’;
return req, nil // passthrough
case "textDocument/publishDiagnostics":
var res PublishDiagnosticsParams
return &res, json.Unmarshal(req, &res)
default:
panic("unimplemented message: " + method)
}
}
func EncodeMessage(msg interface{}) json.RawMessage {
raw, _ := json.Marshal(msg)
return raw
}