author | wenzelm |
Sun, 12 Mar 2017 18:05:06 +0100 | |
changeset 65200 | 1227a68fac7a |
parent 65189 | 41d2452845fc |
child 65201 | 2d01b30e6ac6 |
permissions | -rw-r--r-- |
64605 | 1 |
/* Title: Tools/VSCode/src/protocol.scala |
2 |
Author: Makarius |
|
3 |
||
65165 | 4 |
Message formats for Language Server Protocol 3.0, see |
64605 | 5 |
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md |
6 |
*/ |
|
7 |
||
8 |
package isabelle.vscode |
|
9 |
||
10 |
||
11 |
import isabelle._ |
|
12 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
13 |
import java.io.{File => JFile} |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
14 |
|
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
15 |
|
64605 | 16 |
object Protocol |
17 |
{ |
|
18 |
/* abstract message */ |
|
19 |
||
20 |
object Message |
|
21 |
{ |
|
22 |
val empty: Map[String, JSON.T] = Map("jsonrpc" -> "2.0") |
|
23 |
} |
|
24 |
||
25 |
||
26 |
/* notification */ |
|
27 |
||
28 |
object Notification |
|
29 |
{ |
|
30 |
def apply(method: String, params: JSON.T): JSON.T = |
|
31 |
Message.empty + ("method" -> method) + ("params" -> params) |
|
32 |
||
33 |
def unapply(json: JSON.T): Option[(String, Option[JSON.T])] = |
|
34 |
for { |
|
35 |
method <- JSON.string(json, "method") |
|
36 |
params = JSON.value(json, "params") |
|
37 |
} yield (method, params) |
|
38 |
} |
|
39 |
||
40 |
class Notification0(name: String) |
|
41 |
{ |
|
42 |
def unapply(json: JSON.T): Option[Unit] = |
|
43 |
json match { |
|
44 |
case Notification(method, _) if method == name => Some(()) |
|
45 |
case _ => None |
|
46 |
} |
|
47 |
} |
|
48 |
||
49 |
||
50 |
/* request message */ |
|
51 |
||
52 |
sealed case class Id(id: Any) |
|
53 |
{ |
|
54 |
require( |
|
55 |
id.isInstanceOf[Int] || |
|
56 |
id.isInstanceOf[Long] || |
|
57 |
id.isInstanceOf[Double] || |
|
58 |
id.isInstanceOf[String]) |
|
59 |
||
60 |
override def toString: String = id.toString |
|
61 |
} |
|
62 |
||
63 |
object RequestMessage |
|
64 |
{ |
|
65 |
def apply(id: Id, method: String, params: JSON.T): JSON.T = |
|
66 |
Message.empty + ("id" -> id.id) + ("method" -> method) + ("params" -> params) |
|
67 |
||
68 |
def unapply(json: JSON.T): Option[(Id, String, Option[JSON.T])] = |
|
69 |
for { |
|
70 |
id <- JSON.long(json, "id") orElse JSON.double(json, "id") orElse JSON.string(json, "id") |
|
71 |
method <- JSON.string(json, "method") |
|
72 |
params = JSON.value(json, "params") |
|
73 |
} yield (Id(id), method, params) |
|
74 |
} |
|
75 |
||
76 |
class Request0(name: String) |
|
77 |
{ |
|
78 |
def unapply(json: JSON.T): Option[Id] = |
|
79 |
json match { |
|
80 |
case RequestMessage(id, method, _) if method == name => Some(id) |
|
81 |
case _ => None |
|
82 |
} |
|
83 |
} |
|
84 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
85 |
class RequestTextDocumentPosition(name: String) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
86 |
{ |
64651 | 87 |
def unapply(json: JSON.T): Option[(Id, Line.Node_Position)] = |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
88 |
json match { |
64651 | 89 |
case RequestMessage(id, method, Some(TextDocumentPosition(node_pos))) if method == name => |
90 |
Some((id, node_pos)) |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
91 |
case _ => None |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
92 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
93 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
94 |
|
64605 | 95 |
|
96 |
/* response message */ |
|
97 |
||
98 |
object ResponseMessage |
|
99 |
{ |
|
100 |
def apply(id: Id, result: Option[JSON.T] = None, error: Option[ResponseError] = None): JSON.T = |
|
101 |
Message.empty + ("id" -> id.id) ++ |
|
64878 | 102 |
JSON.optional("result" -> result) ++ |
103 |
JSON.optional("error" -> error.map(_.json)) |
|
64605 | 104 |
|
105 |
def strict(id: Id, result: Option[JSON.T] = None, error: String = ""): JSON.T = |
|
106 |
if (error == "") apply(id, result = result) |
|
107 |
else apply(id, error = Some(ResponseError(code = ErrorCodes.serverErrorEnd, message = error))) |
|
108 |
} |
|
109 |
||
110 |
sealed case class ResponseError(code: Int, message: String, data: Option[JSON.T] = None) |
|
111 |
{ |
|
112 |
def json: JSON.T = |
|
64878 | 113 |
Map("code" -> code, "message" -> message) ++ JSON.optional("data" -> data) |
64605 | 114 |
} |
115 |
||
116 |
object ErrorCodes |
|
117 |
{ |
|
118 |
val ParseError = -32700 |
|
119 |
val InvalidRequest = -32600 |
|
120 |
val MethodNotFound = -32601 |
|
121 |
val InvalidParams = -32602 |
|
122 |
val InternalError = -32603 |
|
123 |
val serverErrorStart = -32099 |
|
124 |
val serverErrorEnd = -32000 |
|
125 |
} |
|
126 |
||
127 |
||
128 |
/* init and exit */ |
|
129 |
||
130 |
object Initialize extends Request0("initialize") |
|
131 |
{ |
|
132 |
def reply(id: Id, error: String): JSON.T = |
|
133 |
ResponseMessage.strict(id, Some(Map("capabilities" -> ServerCapabilities.json)), error) |
|
134 |
} |
|
135 |
||
136 |
object ServerCapabilities |
|
137 |
{ |
|
138 |
val json: JSON.T = |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
139 |
Map( |
65160 | 140 |
"textDocumentSync" -> 2, |
64877 | 141 |
"completionProvider" -> Map("resolveProvider" -> false, "triggerCharacters" -> Nil), |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
142 |
"hoverProvider" -> true, |
64767 | 143 |
"definitionProvider" -> true, |
144 |
"documentHighlightProvider" -> true) |
|
64605 | 145 |
} |
146 |
||
147 |
object Shutdown extends Request0("shutdown") |
|
148 |
{ |
|
149 |
def reply(id: Id, error: String): JSON.T = |
|
150 |
ResponseMessage.strict(id, Some("OK"), error) |
|
151 |
} |
|
152 |
||
153 |
object Exit extends Notification0("exit") |
|
154 |
||
155 |
||
156 |
/* document positions */ |
|
157 |
||
158 |
object Position |
|
159 |
{ |
|
160 |
def apply(pos: Line.Position): JSON.T = |
|
161 |
Map("line" -> pos.line, "character" -> pos.column) |
|
162 |
||
163 |
def unapply(json: JSON.T): Option[Line.Position] = |
|
164 |
for { |
|
165 |
line <- JSON.int(json, "line") |
|
166 |
column <- JSON.int(json, "character") |
|
167 |
} yield Line.Position(line, column) |
|
168 |
} |
|
169 |
||
170 |
object Range |
|
171 |
{ |
|
65173 | 172 |
def compact(range: Line.Range): List[Int] = |
173 |
List(range.start.line, range.start.column, range.stop.line, range.stop.column) |
|
174 |
||
64605 | 175 |
def apply(range: Line.Range): JSON.T = |
176 |
Map("start" -> Position(range.start), "end" -> Position(range.stop)) |
|
177 |
||
178 |
def unapply(json: JSON.T): Option[Line.Range] = |
|
179 |
(JSON.value(json, "start"), JSON.value(json, "end")) match { |
|
180 |
case (Some(Position(start)), Some(Position(stop))) => Some(Line.Range(start, stop)) |
|
181 |
case _ => None |
|
182 |
} |
|
183 |
} |
|
184 |
||
185 |
object Location |
|
186 |
{ |
|
64651 | 187 |
def apply(loc: Line.Node_Range): JSON.T = |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
188 |
Map("uri" -> Url.print_file_name(loc.name), "range" -> Range(loc.range)) |
64605 | 189 |
|
64651 | 190 |
def unapply(json: JSON.T): Option[Line.Node_Range] = |
64605 | 191 |
for { |
192 |
uri <- JSON.string(json, "uri") |
|
193 |
range_json <- JSON.value(json, "range") |
|
194 |
range <- Range.unapply(range_json) |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
195 |
} yield Line.Node_Range(Url.canonical_file_name(uri), range) |
64605 | 196 |
} |
197 |
||
198 |
object TextDocumentPosition |
|
199 |
{ |
|
64651 | 200 |
def unapply(json: JSON.T): Option[Line.Node_Position] = |
64605 | 201 |
for { |
202 |
doc <- JSON.value(json, "textDocument") |
|
203 |
uri <- JSON.string(doc, "uri") |
|
204 |
pos_json <- JSON.value(json, "position") |
|
205 |
pos <- Position.unapply(pos_json) |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
206 |
} yield Line.Node_Position(Url.canonical_file_name(uri), pos) |
64605 | 207 |
} |
208 |
||
209 |
||
65103 | 210 |
/* marked strings */ |
65093 | 211 |
|
212 |
sealed case class MarkedString(text: String, language: String = "plaintext") |
|
213 |
{ |
|
214 |
def json: JSON.T = Map("language" -> language, "value" -> text) |
|
215 |
} |
|
216 |
||
65103 | 217 |
object MarkedStrings |
218 |
{ |
|
219 |
def json(msgs: List[MarkedString]): Option[JSON.T] = |
|
220 |
msgs match { |
|
221 |
case Nil => None |
|
222 |
case List(msg) => Some(msg.json) |
|
223 |
case _ => Some(msgs.map(_.json)) |
|
224 |
} |
|
225 |
} |
|
226 |
||
65093 | 227 |
|
64605 | 228 |
/* diagnostic messages */ |
229 |
||
230 |
object MessageType |
|
231 |
{ |
|
232 |
val Error = 1 |
|
233 |
val Warning = 2 |
|
234 |
val Info = 3 |
|
235 |
val Log = 4 |
|
236 |
} |
|
237 |
||
238 |
object DisplayMessage |
|
239 |
{ |
|
240 |
def apply(message_type: Int, message: String, show: Boolean = true): JSON.T = |
|
241 |
Notification(if (show) "window/showMessage" else "window/logMessage", |
|
242 |
Map("type" -> message_type, "message" -> message)) |
|
243 |
} |
|
244 |
||
245 |
||
246 |
/* document edits */ |
|
247 |
||
248 |
object DidOpenTextDocument |
|
249 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
250 |
def unapply(json: JSON.T): Option[(JFile, String, Long, String)] = |
64605 | 251 |
json match { |
252 |
case Notification("textDocument/didOpen", Some(params)) => |
|
253 |
for { |
|
254 |
doc <- JSON.value(params, "textDocument") |
|
255 |
uri <- JSON.string(doc, "uri") |
|
256 |
lang <- JSON.string(doc, "languageId") |
|
257 |
version <- JSON.long(doc, "version") |
|
258 |
text <- JSON.string(doc, "text") |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
259 |
} yield (Url.canonical_file(uri), lang, version, text) |
64605 | 260 |
case _ => None |
261 |
} |
|
262 |
} |
|
263 |
||
264 |
||
65160 | 265 |
sealed case class TextDocumentChange(range: Option[Line.Range], text: String) |
64605 | 266 |
|
267 |
object DidChangeTextDocument |
|
268 |
{ |
|
65160 | 269 |
def unapply_change(json: JSON.T): Option[TextDocumentChange] = |
270 |
for { text <- JSON.string(json, "text") } |
|
271 |
yield TextDocumentChange(JSON.value(json, "range", Range.unapply _), text) |
|
272 |
||
273 |
def unapply(json: JSON.T): Option[(JFile, Long, List[TextDocumentChange])] = |
|
64605 | 274 |
json match { |
275 |
case Notification("textDocument/didChange", Some(params)) => |
|
276 |
for { |
|
277 |
doc <- JSON.value(params, "textDocument") |
|
278 |
uri <- JSON.string(doc, "uri") |
|
279 |
version <- JSON.long(doc, "version") |
|
65160 | 280 |
changes <- JSON.array(params, "contentChanges", unapply_change _) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
281 |
} yield (Url.canonical_file(uri), version, changes) |
64605 | 282 |
case _ => None |
283 |
} |
|
284 |
} |
|
285 |
||
286 |
class TextDocumentNotification(name: String) |
|
287 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
288 |
def unapply(json: JSON.T): Option[JFile] = |
64605 | 289 |
json match { |
290 |
case Notification(method, Some(params)) if method == name => |
|
291 |
for { |
|
292 |
doc <- JSON.value(params, "textDocument") |
|
293 |
uri <- JSON.string(doc, "uri") |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
294 |
} yield Url.canonical_file(uri) |
64605 | 295 |
case _ => None |
296 |
} |
|
297 |
} |
|
298 |
||
299 |
object DidCloseTextDocument extends TextDocumentNotification("textDocument/didClose") |
|
300 |
object DidSaveTextDocument extends TextDocumentNotification("textDocument/didSave") |
|
301 |
||
302 |
||
64877 | 303 |
/* completion */ |
304 |
||
305 |
sealed case class CompletionItem( |
|
306 |
label: String, |
|
307 |
kind: Option[Int] = None, |
|
308 |
detail: Option[String] = None, |
|
309 |
documentation: Option[String] = None, |
|
310 |
insertText: Option[String] = None, |
|
311 |
range: Option[Line.Range] = None) |
|
312 |
{ |
|
313 |
def json: JSON.T = |
|
314 |
Message.empty + ("label" -> label) ++ |
|
64878 | 315 |
JSON.optional("kind" -> kind) ++ |
316 |
JSON.optional("detail" -> detail) ++ |
|
317 |
JSON.optional("documentation" -> documentation) ++ |
|
318 |
JSON.optional("insertText" -> insertText) ++ |
|
319 |
JSON.optional("range" -> range.map(Range(_))) |
|
64877 | 320 |
} |
321 |
||
322 |
object Completion extends RequestTextDocumentPosition("textDocument/completion") |
|
323 |
{ |
|
324 |
def reply(id: Id, result: List[CompletionItem]): JSON.T = |
|
325 |
ResponseMessage(id, Some(result.map(_.json))) |
|
326 |
} |
|
327 |
||
328 |
||
64605 | 329 |
/* hover request */ |
330 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
331 |
object Hover extends RequestTextDocumentPosition("textDocument/hover") |
64605 | 332 |
{ |
65093 | 333 |
def reply(id: Id, result: Option[(Line.Range, List[MarkedString])]): JSON.T = |
64605 | 334 |
{ |
335 |
val res = |
|
336 |
result match { |
|
64747 | 337 |
case Some((range, contents)) => |
65103 | 338 |
Map("contents" -> MarkedStrings.json(contents).getOrElse(Nil), "range" -> Range(range)) |
64605 | 339 |
case None => Map("contents" -> Nil) |
340 |
} |
|
341 |
ResponseMessage(id, Some(res)) |
|
342 |
} |
|
343 |
} |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
344 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
345 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
346 |
/* goto definition request */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
347 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
348 |
object GotoDefinition extends RequestTextDocumentPosition("textDocument/definition") |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
349 |
{ |
64651 | 350 |
def reply(id: Id, result: List[Line.Node_Range]): JSON.T = |
64649 | 351 |
ResponseMessage(id, Some(result.map(Location.apply(_)))) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
352 |
} |
64675 | 353 |
|
354 |
||
64767 | 355 |
/* document highlights request */ |
356 |
||
357 |
object DocumentHighlight |
|
358 |
{ |
|
359 |
def text(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(1)) |
|
360 |
def read(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(2)) |
|
361 |
def write(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(3)) |
|
362 |
} |
|
363 |
||
364 |
sealed case class DocumentHighlight(range: Line.Range, kind: Option[Int] = None) |
|
365 |
{ |
|
366 |
def json: JSON.T = |
|
367 |
kind match { |
|
368 |
case None => Map("range" -> Range(range)) |
|
369 |
case Some(k) => Map("range" -> Range(range), "kind" -> k) |
|
370 |
} |
|
371 |
} |
|
372 |
||
373 |
object DocumentHighlights extends RequestTextDocumentPosition("textDocument/documentHighlight") |
|
374 |
{ |
|
375 |
def reply(id: Id, result: List[DocumentHighlight]): JSON.T = |
|
376 |
ResponseMessage(id, Some(result.map(_.json))) |
|
377 |
} |
|
378 |
||
379 |
||
64675 | 380 |
/* diagnostics */ |
381 |
||
382 |
sealed case class Diagnostic(range: Line.Range, message: String, |
|
383 |
severity: Option[Int] = None, code: Option[Int] = None, source: Option[String] = None) |
|
384 |
{ |
|
385 |
def json: JSON.T = |
|
386 |
Message.empty + ("range" -> Range(range)) + ("message" -> message) ++ |
|
64878 | 387 |
JSON.optional("severity" -> severity) ++ |
388 |
JSON.optional("code" -> code) ++ |
|
389 |
JSON.optional("source" -> source) |
|
64675 | 390 |
} |
391 |
||
392 |
object DiagnosticSeverity |
|
393 |
{ |
|
64762
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
394 |
val Error = 1 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
395 |
val Warning = 2 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
396 |
val Information = 3 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
397 |
val Hint = 4 |
64675 | 398 |
} |
399 |
||
400 |
object PublishDiagnostics |
|
401 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
402 |
def apply(file: JFile, diagnostics: List[Diagnostic]): JSON.T = |
64675 | 403 |
Notification("textDocument/publishDiagnostics", |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
404 |
Map("uri" -> Url.print_file(file), "diagnostics" -> diagnostics.map(_.json))) |
64675 | 405 |
} |
65094 | 406 |
|
407 |
||
408 |
/* decorations */ |
|
409 |
||
65173 | 410 |
sealed case class DecorationOpts(range: Line.Range, hover_message: List[MarkedString]) |
65094 | 411 |
{ |
412 |
def json: JSON.T = |
|
65173 | 413 |
Map("range" -> Range.compact(range)) ++ |
414 |
JSON.optional("hover_message" -> MarkedStrings.json(hover_message)) |
|
65094 | 415 |
} |
416 |
||
65173 | 417 |
sealed case class Decoration(typ: String, content: List[DecorationOpts]) |
65094 | 418 |
{ |
65095 | 419 |
def json(file: JFile): JSON.T = |
65094 | 420 |
Notification("PIDE/decoration", |
65173 | 421 |
Map("uri" -> Url.print_file(file), "type" -> typ, "content" -> content.map(_.json))) |
65094 | 422 |
} |
65189 | 423 |
|
424 |
||
425 |
/* caret handling and dynamic output */ |
|
426 |
||
427 |
object Caret_Update |
|
428 |
{ |
|
429 |
def unapply(json: JSON.T): Option[Option[(JFile, Line.Position)]] = |
|
430 |
json match { |
|
431 |
case Notification("PIDE/caret_update", Some(params)) => |
|
432 |
val caret = |
|
433 |
for { |
|
434 |
uri <- JSON.string(params, "uri") |
|
435 |
if Url.is_wellformed_file(uri) |
|
436 |
pos <- Position.unapply(params) |
|
437 |
} yield (Url.canonical_file(uri), pos) |
|
438 |
Some(caret) |
|
439 |
case _ => None |
|
440 |
} |
|
441 |
} |
|
442 |
||
443 |
object Dynamic_Output |
|
444 |
{ |
|
445 |
def apply(body: String): JSON.T = |
|
65200
1227a68fac7a
more explicit message type: allows body to become empty;
wenzelm
parents:
65189
diff
changeset
|
446 |
Notification("PIDE/dynamic_output", Map("body" -> body)) |
65189 | 447 |
} |
64605 | 448 |
} |