-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathProtocol.ml
191 lines (161 loc) · 4.96 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
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;
}
type completionItem = {
label: string;
kind: int;
tags: int list;
detail: string;
documentation: markupContent option;
}
type location = {uri: string; range: range}
type documentSymbolItem = {name: string; kind: int; location: location}
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 documentChange =
| TextDocumentEdit of textDocumentEdit
| RenameFile of renameFile
type workspaceEdit = {documentChanges: documentChange list}
type codeActionKind = RefactorRewrite
type codeAction = {
title: string;
codeActionKind: codeActionKind;
edit: workspaceEdit;
}
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)
let stringifyCompletionItem c =
Printf.sprintf
{|{
"label": "%s",
"kind": %i,
"tags": %s,
"detail": "%s",
"documentation": %s
}|}
(Json.escape c.label) c.kind
(c.tags |> List.map string_of_int |> array)
(Json.escape c.detail)
(match c.documentation with
| None -> null
| Some doc -> stringifyMarkupContent doc)
let stringifyHover s = Printf.sprintf {|{"contents": "%s"}|} (Json.escape s)
let stringifyLocation (h : location) =
Printf.sprintf {|{"uri": "%s", "range": %s}|} (Json.escape h.uri)
(stringifyRange h.range)
let stringifyDocumentSymbolItem i =
Printf.sprintf
{|{
"name": "%s",
"kind": %i,
"location": %s
}|}
(Json.escape i.name) i.kind
(stringifyLocation i.location)
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 stringifyWorkspaceEdit we =
Printf.sprintf {|{"documentChanges": %s}|}
(we.documentChanges
|> List.map (fun documentChange ->
match documentChange with
| TextDocumentEdit textEdit -> textEdit |> stringifyTextDocumentEdit
| RenameFile renameFile -> renameFile |> stringifyRenameFile)
|> array)
let stringifyCodeAction ca =
Printf.sprintf {|{"title": "%s", "kind": "%s", "edit": %s}|} ca.title
(codeActionKindToString ca.codeActionKind)
(ca.edit |> stringifyWorkspaceEdit)
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)
(* 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