-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathProtocol.ml
368 lines (324 loc) · 10.8 KB
/
Protocol.ml
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
type position = {line: int; character: int}
type range = {start: position; end_: position}
type markupContent = {kind: string; value: string}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#command *)
type command = {title: string; command: string}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#codeLens *)
type codeLens = {range: range; command: command option}
type inlayHint = {
position: position;
label: string;
kind: int;
paddingLeft: bool;
paddingRight: bool;
}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#parameterInformation *)
type parameterInformation = {label: int * int; documentation: markupContent}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#signatureInformation *)
type signatureInformation = {
label: string;
parameters: parameterInformation list;
documentation: markupContent option;
}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#signatureHelp *)
type signatureHelp = {
signatures: signatureInformation list;
activeSignature: int option;
activeParameter: int option;
}
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#insertTextFormat *)
type insertTextFormat = Snippet
let insertTextFormatToInt f =
match f with
| Snippet -> 2
type textEdit = {range: range; newText: string}
type completionItem = {
label: string;
kind: int;
tags: int list;
detail: string;
sortText: string option;
filterText: string option;
insertTextFormat: insertTextFormat option;
insertText: string option;
documentation: markupContent option;
data: (string * string) list option;
additionalTextEdits: textEdit list option;
}
type location = {uri: string; range: range}
type documentSymbolItem = {
name: string;
kind: int;
range: range;
children: documentSymbolItem list;
}
type renameFile = {oldUri: string; newUri: string}
type diagnostic = {range: range; message: string; severity: int}
type optionalVersionedTextDocumentIdentifier = {
version: int option;
uri: string;
}
type textDocumentEdit = {
textDocument: optionalVersionedTextDocumentIdentifier;
edits: textEdit list;
}
type createFileOptions = {overwrite: bool option; ignoreIfExists: bool option}
type createFile = {uri: string; options: createFileOptions option}
type documentChange =
| TextDocumentEdit of textDocumentEdit
| CreateFile of createFile
type codeActionEdit = {documentChanges: documentChange list}
type codeActionKind = RefactorRewrite
type codeAction = {
title: string;
codeActionKind: codeActionKind;
edit: codeActionEdit;
}
let wrapInQuotes s = "\"" ^ Json.escape s ^ "\""
let null = "null"
let array l = "[" ^ String.concat ", " l ^ "]"
let stringifyPosition p =
Printf.sprintf {|{"line": %i, "character": %i}|} p.line p.character
let stringifyRange r =
Printf.sprintf {|{"start": %s, "end": %s}|}
(stringifyPosition r.start)
(stringifyPosition r.end_)
let stringifyTextEdit (te : textEdit) =
Printf.sprintf
{|{
"range": %s,
"newText": %s
}|}
(stringifyRange te.range) (wrapInQuotes te.newText)
let stringifyMarkupContent (m : markupContent) =
Printf.sprintf {|{"kind": %s, "value": %s}|} (wrapInQuotes m.kind)
(wrapInQuotes m.value)
(** None values are not emitted in the output. *)
let stringifyObject ?(startOnNewline = false) ?(indentation = 1) properties =
let indentationStr = String.make (indentation * 2) ' ' in
(if startOnNewline then "\n" ^ indentationStr else "")
^ {|{
|}
^ (properties
|> List.filter_map (fun (key, value) ->
match value with
| None -> None
| Some v ->
Some (Printf.sprintf {|%s "%s": %s|} indentationStr key v))
|> String.concat ",\n")
^ "\n" ^ indentationStr ^ "}"
let optWrapInQuotes s =
match s with
| None -> None
| Some s -> Some (wrapInQuotes s)
let stringifyCompletionItem c =
stringifyObject
[
("label", Some (wrapInQuotes c.label));
("kind", Some (string_of_int c.kind));
("tags", Some (c.tags |> List.map string_of_int |> array));
("detail", Some (wrapInQuotes c.detail));
( "documentation",
Some
(match c.documentation with
| None -> null
| Some doc -> stringifyMarkupContent doc) );
("sortText", optWrapInQuotes c.sortText);
("filterText", optWrapInQuotes c.filterText);
("insertText", optWrapInQuotes c.insertText);
( "insertTextFormat",
match c.insertTextFormat with
| None -> None
| Some insertTextFormat ->
Some (Printf.sprintf "%i" (insertTextFormatToInt insertTextFormat)) );
( "data",
match c.data with
| None -> None
| Some fields ->
Some
(fields
|> List.map (fun (key, value) -> (key, Some (wrapInQuotes value)))
|> stringifyObject ~indentation:2) );
( "additionalTextEdits",
match c.additionalTextEdits with
| Some additionalTextEdits ->
Some (additionalTextEdits |> List.map stringifyTextEdit |> array)
| None -> None );
]
let stringifyHover value =
Printf.sprintf {|{"contents": %s}|}
(stringifyMarkupContent {kind = "markdown"; value})
let stringifyLocation (h : location) =
Printf.sprintf {|{"uri": %s, "range": %s}|} (wrapInQuotes h.uri)
(stringifyRange h.range)
let stringifyDocumentSymbolItems items =
let buf = Buffer.create 10 in
let stringifyName name = Printf.sprintf "\"%s\"" (Json.escape name) in
let stringifyKind kind = string_of_int kind in
let emitStr = Buffer.add_string buf in
let emitSep () = emitStr ",\n" in
let rec emitItem ~indent item =
let openBrace = Printf.sprintf "%s{\n" indent in
let closeBrace = Printf.sprintf "\n%s}" indent in
let indent = indent ^ " " in
let emitField name s =
emitStr (Printf.sprintf "%s\"%s\": %s" indent name s)
in
emitStr openBrace;
emitField "name" (stringifyName item.name);
emitSep ();
emitField "kind" (stringifyKind item.kind);
emitSep ();
emitField "range" (stringifyRange item.range);
emitSep ();
emitField "selectionRange" (stringifyRange item.range);
if item.children <> [] then (
emitSep ();
emitField "children" "[\n";
emitBody ~indent (List.rev item.children);
emitStr "]");
emitStr closeBrace
and emitBody ~indent items =
match items with
| [] -> ()
| item :: rest ->
emitItem ~indent item;
if rest <> [] then emitSep ();
emitBody ~indent rest
in
let indent = "" in
emitStr "[\n";
emitBody ~indent (List.rev items);
emitStr "\n]";
Buffer.contents buf
let stringifyRenameFile {oldUri; newUri} =
Printf.sprintf {|{
"kind": "rename",
"oldUri": %s,
"newUri": %s
}|}
(wrapInQuotes oldUri) (wrapInQuotes newUri)
let stringifyoptionalVersionedTextDocumentIdentifier td =
Printf.sprintf {|{
"version": %s,
"uri": %s
}|}
(match td.version with
| None -> null
| Some v -> string_of_int v)
(wrapInQuotes td.uri)
let stringifyTextDocumentEdit tde =
Printf.sprintf {|{
"textDocument": %s,
"edits": %s
}|}
(stringifyoptionalVersionedTextDocumentIdentifier tde.textDocument)
(tde.edits |> List.map stringifyTextEdit |> array)
let stringifyCreateFile cf =
stringifyObject
[
("kind", Some (wrapInQuotes "create"));
("uri", Some (wrapInQuotes cf.uri));
( "options",
match cf.options with
| None -> None
| Some options ->
Some
(stringifyObject
[
( "overwrite",
match options.overwrite with
| None -> None
| Some ov -> Some (string_of_bool ov) );
( "ignoreIfExists",
match options.ignoreIfExists with
| None -> None
| Some i -> Some (string_of_bool i) );
]) );
]
let stringifyDocumentChange dc =
match dc with
| TextDocumentEdit tde -> stringifyTextDocumentEdit tde
| CreateFile cf -> stringifyCreateFile cf
let codeActionKindToString kind =
match kind with
| RefactorRewrite -> "refactor.rewrite"
let stringifyCodeActionEdit cae =
Printf.sprintf {|{"documentChanges": %s}|}
(cae.documentChanges |> List.map stringifyDocumentChange |> array)
let stringifyCodeAction ca =
Printf.sprintf {|{"title": %s, "kind": %s, "edit": %s}|}
(wrapInQuotes ca.title)
(wrapInQuotes (codeActionKindToString ca.codeActionKind))
(ca.edit |> stringifyCodeActionEdit)
let stringifyHint (hint : inlayHint) =
Printf.sprintf
{|{
"position": %s,
"label": %s,
"kind": %i,
"paddingLeft": %b,
"paddingRight": %b
}|}
(stringifyPosition hint.position)
(wrapInQuotes hint.label) hint.kind hint.paddingLeft hint.paddingRight
let stringifyCommand (command : command) =
Printf.sprintf {|{"title": %s, "command": %s}|}
(wrapInQuotes command.title)
(wrapInQuotes command.command)
let stringifyCodeLens (codeLens : codeLens) =
Printf.sprintf
{|{
"range": %s,
"command": %s
}|}
(stringifyRange codeLens.range)
(match codeLens.command with
| None -> ""
| Some command -> stringifyCommand command)
let stringifyParameterInformation (parameterInformation : parameterInformation)
=
Printf.sprintf {|{"label": %s, "documentation": %s}|}
(let line, chr = parameterInformation.label in
"[" ^ string_of_int line ^ ", " ^ string_of_int chr ^ "]")
(stringifyMarkupContent parameterInformation.documentation)
let stringifySignatureInformation (signatureInformation : signatureInformation)
=
Printf.sprintf
{|{
"label": %s,
"parameters": %s%s
}|}
(wrapInQuotes signatureInformation.label)
(signatureInformation.parameters
|> List.map stringifyParameterInformation
|> array)
(match signatureInformation.documentation with
| None -> ""
| Some docs ->
Printf.sprintf ",\n \"documentation\": %s"
(stringifyMarkupContent docs))
let stringifySignatureHelp (signatureHelp : signatureHelp) =
Printf.sprintf
{|{
"signatures": %s,
"activeSignature": %s,
"activeParameter": %s
}|}
(signatureHelp.signatures |> List.map stringifySignatureInformation |> array)
(match signatureHelp.activeSignature with
| None -> null
| Some activeSignature -> activeSignature |> string_of_int)
(match signatureHelp.activeParameter with
| None -> null
| Some activeParameter -> activeParameter |> string_of_int)
(* https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#diagnostic *)
let stringifyDiagnostic d =
Printf.sprintf
{|{
"range": %s,
"message": %s,
"severity": %d,
"source": "ReScript"
}|}
(stringifyRange d.range) (wrapInQuotes d.message) d.severity