-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathProtocol.ml
315 lines (275 loc) · 9.19 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
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 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;
}
type location = {uri: string; range: range}
type documentSymbolItem = {
name: string;
kind: int;
range: range;
children: documentSymbolItem list;
}
type renameFile = {oldUri: string; newUri: string}
type textEdit = {range: range; newText: string}
type diagnostic = {range: range; message: string; severity: int}
type optionalVersionedTextDocumentIdentifier = {
version: int option;
uri: string;
}
type textDocumentEdit = {
textDocument: optionalVersionedTextDocumentIdentifier;
edits: textEdit list;
}
type codeActionEdit = {documentChanges: textDocumentEdit list}
type codeActionKind = RefactorRewrite
type codeAction = {
title: string;
codeActionKind: codeActionKind;
edit: codeActionEdit;
}
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 stringifyMarkupContent (m : markupContent) =
Printf.sprintf {|{"kind": "%s", "value": "%s"}|} m.kind (Json.escape 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 wrapInQuotes s = "\"" ^ Json.escape s ^ "\""
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)) );
]
let stringifyHover value =
Printf.sprintf {|{"contents": %s}|}
(stringifyMarkupContent {kind = "markdown"; value})
let stringifyLocation (h : location) =
Printf.sprintf {|{"uri": "%s", "range": %s}|} (Json.escape 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"
}|}
(Json.escape oldUri) (Json.escape newUri)
let stringifyTextEdit (te : textEdit) =
Printf.sprintf {|{
"range": %s,
"newText": "%s"
}|}
(stringifyRange te.range) (Json.escape te.newText)
let stringifyoptionalVersionedTextDocumentIdentifier td =
Printf.sprintf {|{
"version": %s,
"uri": "%s"
}|}
(match td.version with
| None -> null
| Some v -> string_of_int v)
(Json.escape td.uri)
let stringifyTextDocumentEdit tde =
Printf.sprintf {|{
"textDocument": %s,
"edits": %s
}|}
(stringifyoptionalVersionedTextDocumentIdentifier tde.textDocument)
(tde.edits |> List.map stringifyTextEdit |> array)
let codeActionKindToString kind =
match kind with
| RefactorRewrite -> "refactor.rewrite"
let stringifyCodeActionEdit cae =
Printf.sprintf {|{"documentChanges": %s}|}
(cae.documentChanges |> List.map stringifyTextDocumentEdit |> array)
let stringifyCodeAction ca =
Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title
(codeActionKindToString ca.codeActionKind)
(ca.edit |> stringifyCodeActionEdit)
let stringifyHint hint =
Printf.sprintf
{|{
"position": %s,
"label": "%s",
"kind": %i,
"paddingLeft": %b,
"paddingRight": %b
}|}
(stringifyPosition hint.position)
(Json.escape hint.label) hint.kind hint.paddingLeft hint.paddingRight
let stringifyCommand (command : command) =
Printf.sprintf {|{"title": "%s", "command": "%s"}|}
(Json.escape command.title)
(Json.escape 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
}|}
(Json.escape 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) (Json.escape d.message) d.severity