-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathprotocol.go
317 lines (299 loc) · 9.1 KB
/
protocol.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
package lsp
import (
"context"
"encoding/json"
"github.com/sourcegraph/jsonrpc2"
)
func ReadParams(method string, raw *json.RawMessage) (interface{}, error) {
switch method {
case "initialize":
params := new(InitializeParams)
err := json.Unmarshal(*raw, params)
return params, err
case "initialized":
return &InitializedParams{}, nil
case "textDocument/didOpen":
params := new(DidOpenTextDocumentParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/didChange":
params := new(DidChangeTextDocumentParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/didSave":
params := new(DidSaveTextDocumentParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/didClose":
params := new(DidCloseTextDocumentParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/completion":
params := new(CompletionParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/codeAction":
params := new(CodeActionParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/signatureHelp":
fallthrough
case "textDocument/hover":
params := new(HoverParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/definition":
fallthrough
case "textDocument/typeDefinition":
fallthrough
case "textDocument/implementation":
fallthrough
case "textDocument/documentHighlight":
params := new(TextDocumentPositionParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/references":
params := new(ReferenceParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/formatting":
params := new(DocumentFormattingParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/rangeFormatting":
params := new(DocumentRangeFormattingParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/onTypeFormatting":
params := new(DocumentOnTypeFormattingParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/documentSymbol":
params := new(DocumentSymbolParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/rename":
params := new(RenameParams)
err := json.Unmarshal(*raw, params)
return params, err
case "workspace/symbol":
params := new(WorkspaceSymbolParams)
err := json.Unmarshal(*raw, params)
return params, err
case "workspace/didChangeWatchedFiles":
params := new(DidChangeWatchedFilesParams)
err := json.Unmarshal(*raw, params)
return params, err
case "workspace/executeCommand":
params := new(ExecuteCommandParams)
err := json.Unmarshal(*raw, params)
return params, err
case "workspace/applyEdit":
params := new(ApplyWorkspaceEditParams)
err := json.Unmarshal(*raw, params)
return params, err
case "textDocument/publishDiagnostics":
params := new(PublishDiagnosticsParams)
err := json.Unmarshal(*raw, params)
return params, err
case "arduino/selectedBoard":
params := new(BoardConfig)
err := json.Unmarshal(*raw, params)
return params, err
}
return nil, nil
}
func SendRequest(ctx context.Context, conn *jsonrpc2.Conn, method string, params interface{}) (interface{}, error) {
switch method {
case "initialize":
result := new(InitializeResult)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/completion":
result := new(CompletionList)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/codeAction":
result := new([]CommandOrCodeAction)
err := conn.Call(ctx, method, params, result)
return result, err
case "completionItem/resolve":
result := new(CompletionItem)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/signatureHelp":
result := new(SignatureHelp)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/hover":
result := new(Hover)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/definition":
fallthrough
case "textDocument/typeDefinition":
fallthrough
case "textDocument/implementation":
fallthrough
case "textDocument/references":
result := new([]Location)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/documentHighlight":
result := new([]DocumentHighlight)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/formatting":
fallthrough
case "textDocument/rangeFormatting":
fallthrough
case "textDocument/onTypeFormatting":
result := new([]TextEdit)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/documentSymbol":
result := new(DocumentSymbolArrayOrSymbolInformationArray)
err := conn.Call(ctx, method, params, result)
return result, err
case "textDocument/rename":
result := new(WorkspaceEdit)
err := conn.Call(ctx, method, params, result)
return result, err
case "workspace/symbol":
result := new([]SymbolInformation)
err := conn.Call(ctx, method, params, result)
return result, err
case "window/showMessageRequest":
result := new(MessageActionItem)
err := conn.Call(ctx, method, params, result)
return result, err
case "workspace/executeCommand":
result := new(string)
err := conn.Call(ctx, method, params, result)
return result, err
case "workspace/applyEdit":
result := new(ApplyWorkspaceEditResponse)
err := conn.Call(ctx, method, params, result)
return result, err
}
var result interface{}
err := conn.Call(ctx, method, params, result)
return result, err
}
// CodeAction structure according to LSP
type CodeAction struct {
Title string `json:"title"`
Kind string `json:"kind,omitempty"`
Diagnostics []Diagnostic `json:"diagnostics,omitempty"`
Edit *WorkspaceEdit `json:"edit,omitempty"`
Command *Command `json:"command,omitempty"`
}
type CommandOrCodeAction struct {
Command *Command
CodeAction *CodeAction
}
func (entry *CommandOrCodeAction) UnmarshalJSON(raw []byte) error {
command := new(Command)
err := json.Unmarshal(raw, command)
if err == nil && len(command.Command) > 0 {
entry.Command = command
return nil
}
codeAction := new(CodeAction)
err = json.Unmarshal(raw, codeAction)
if err != nil {
return err
}
entry.CodeAction = codeAction
return nil
}
func (entry *CommandOrCodeAction) MarshalJSON() ([]byte, error) {
if entry.Command != nil {
return json.Marshal(entry.Command)
}
if entry.CodeAction != nil {
return json.Marshal(entry.CodeAction)
}
return nil, nil
}
// Hover structure according to LSP
type Hover struct {
Contents MarkupContent `json:"contents"`
Range *Range `json:"range,omitempty"`
}
// HoverParams structure according to LSP
type HoverParams struct {
TextDocumentPositionParams
// WorkDoneProgressParams
}
// MarkupContent structure according to LSP
type MarkupContent struct {
Kind string `json:"kind"`
Value string `json:"value"`
}
// DocumentSymbol structure according to LSP
type DocumentSymbol struct {
Name string `json:"name"`
Detail string `json:"detail,omitempty"`
Kind SymbolKind `json:"kind"`
Deprecated bool `json:"deprecated,omitempty"`
Range Range `json:"range"`
SelectionRange Range `json:"selectionRange"`
Children []DocumentSymbol `json:"children,omitempty"`
}
type DocumentSymbolArrayOrSymbolInformationArray struct {
DocumentSymbolArray *[]DocumentSymbol
SymbolInformationArray *[]SymbolInformation
}
func (entry *DocumentSymbolArrayOrSymbolInformationArray) UnmarshalJSON(raw []byte) error {
intermediate := []json.RawMessage{}
if err := json.Unmarshal(raw, &intermediate); err != nil {
return err
}
discriminator := struct {
Range *Range `json:"range,omitempty"`
Location *Location `json:"location,omitempty"`
}{}
if err := json.Unmarshal(intermediate[0], &discriminator); err != nil {
return err
}
if discriminator.Range != nil {
res := make([]DocumentSymbol, len(intermediate))
for i, item := range intermediate {
if err := json.Unmarshal(item, &res[i]); err != nil {
return err
}
}
entry.DocumentSymbolArray = &res
}
if discriminator.Location != nil {
res := make([]SymbolInformation, len(intermediate))
for i, item := range intermediate {
if err := json.Unmarshal(item, &res[i]); err != nil {
return err
}
}
entry.SymbolInformationArray = &res
}
return nil
}
// ApplyWorkspaceEditParams structure according to LSP
type ApplyWorkspaceEditParams struct {
Label string `json:"label,omitempty"`
Edit WorkspaceEdit `json:"edit"`
}
// ApplyWorkspaceEditResponse structure according to LSP
type ApplyWorkspaceEditResponse struct {
Applied bool `json:"applied"`
FailureReason string `json:"failureReason,omitempty"`
}
// BoardConfig describes the board and port selected by the user.
type BoardConfig struct {
SelectedBoard Board `json:"selectedBoard"`
SelectedPort string `json:"selectedPort"`
}
// Board structure.
type Board struct {
Name string `json:"name"`
Fqbn string `json:"fqbn"`
}