author | haftmann |
Sun, 24 May 2020 19:57:13 +0000 | |
changeset 71886 | 4f4695757980 |
parent 71774 | 491f185fd705 |
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 |
{ |
|
67850 | 22 |
val empty: JSON.Object.T = JSON.Object("jsonrpc" -> "2.0") |
65922 | 23 |
|
24 |
def log(prefix: String, json: JSON.T, logger: Logger, verbose: Boolean) |
|
25 |
{ |
|
67851 | 26 |
val header = |
65922 | 27 |
json match { |
67851 | 28 |
case JSON.Object(obj) => obj -- (obj.keySet - "method" - "id") |
67850 | 29 |
case _ => JSON.Object.empty |
65922 | 30 |
} |
31 |
if (verbose || header.isEmpty) logger(prefix + "\n" + JSON.Format(json)) |
|
32 |
else logger(prefix + " " + JSON.Format(header)) |
|
33 |
} |
|
64605 | 34 |
} |
35 |
||
36 |
||
37 |
/* notification */ |
|
38 |
||
39 |
object Notification |
|
40 |
{ |
|
41 |
def apply(method: String, params: JSON.T): JSON.T = |
|
42 |
Message.empty + ("method" -> method) + ("params" -> params) |
|
43 |
||
44 |
def unapply(json: JSON.T): Option[(String, Option[JSON.T])] = |
|
45 |
for { |
|
46 |
method <- JSON.string(json, "method") |
|
47 |
params = JSON.value(json, "params") |
|
48 |
} yield (method, params) |
|
49 |
} |
|
50 |
||
51 |
class Notification0(name: String) |
|
52 |
{ |
|
53 |
def unapply(json: JSON.T): Option[Unit] = |
|
54 |
json match { |
|
55 |
case Notification(method, _) if method == name => Some(()) |
|
56 |
case _ => None |
|
57 |
} |
|
58 |
} |
|
59 |
||
60 |
||
61 |
/* request message */ |
|
62 |
||
66675 | 63 |
object Id |
64 |
{ |
|
65 |
def empty: Id = Id("") |
|
66 |
} |
|
67 |
||
64605 | 68 |
sealed case class Id(id: Any) |
69 |
{ |
|
70 |
require( |
|
71 |
id.isInstanceOf[Int] || |
|
72 |
id.isInstanceOf[Long] || |
|
73 |
id.isInstanceOf[Double] || |
|
74 |
id.isInstanceOf[String]) |
|
75 |
||
76 |
override def toString: String = id.toString |
|
77 |
} |
|
78 |
||
79 |
object RequestMessage |
|
80 |
{ |
|
81 |
def apply(id: Id, method: String, params: JSON.T): JSON.T = |
|
82 |
Message.empty + ("id" -> id.id) + ("method" -> method) + ("params" -> params) |
|
83 |
||
84 |
def unapply(json: JSON.T): Option[(Id, String, Option[JSON.T])] = |
|
85 |
for { |
|
86 |
id <- JSON.long(json, "id") orElse JSON.double(json, "id") orElse JSON.string(json, "id") |
|
87 |
method <- JSON.string(json, "method") |
|
88 |
params = JSON.value(json, "params") |
|
89 |
} yield (Id(id), method, params) |
|
90 |
} |
|
91 |
||
92 |
class Request0(name: String) |
|
93 |
{ |
|
94 |
def unapply(json: JSON.T): Option[Id] = |
|
95 |
json match { |
|
96 |
case RequestMessage(id, method, _) if method == name => Some(id) |
|
97 |
case _ => None |
|
98 |
} |
|
99 |
} |
|
100 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
101 |
class RequestTextDocumentPosition(name: String) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
102 |
{ |
64651 | 103 |
def unapply(json: JSON.T): Option[(Id, Line.Node_Position)] = |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
104 |
json match { |
64651 | 105 |
case RequestMessage(id, method, Some(TextDocumentPosition(node_pos))) if method == name => |
106 |
Some((id, node_pos)) |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
107 |
case _ => None |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
108 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
109 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
110 |
|
64605 | 111 |
|
112 |
/* response message */ |
|
113 |
||
114 |
object ResponseMessage |
|
115 |
{ |
|
116 |
def apply(id: Id, result: Option[JSON.T] = None, error: Option[ResponseError] = None): JSON.T = |
|
117 |
Message.empty + ("id" -> id.id) ++ |
|
64878 | 118 |
JSON.optional("result" -> result) ++ |
119 |
JSON.optional("error" -> error.map(_.json)) |
|
64605 | 120 |
|
121 |
def strict(id: Id, result: Option[JSON.T] = None, error: String = ""): JSON.T = |
|
122 |
if (error == "") apply(id, result = result) |
|
123 |
else apply(id, error = Some(ResponseError(code = ErrorCodes.serverErrorEnd, message = error))) |
|
66675 | 124 |
|
125 |
def is_empty(json: JSON.T): Boolean = |
|
126 |
JSON.string(json, "id") == Some("") && JSON.value(json, "result").isDefined |
|
64605 | 127 |
} |
128 |
||
129 |
sealed case class ResponseError(code: Int, message: String, data: Option[JSON.T] = None) |
|
130 |
{ |
|
131 |
def json: JSON.T = |
|
67850 | 132 |
JSON.Object("code" -> code, "message" -> message) ++ JSON.optional("data" -> data) |
64605 | 133 |
} |
134 |
||
135 |
object ErrorCodes |
|
136 |
{ |
|
137 |
val ParseError = -32700 |
|
138 |
val InvalidRequest = -32600 |
|
139 |
val MethodNotFound = -32601 |
|
140 |
val InvalidParams = -32602 |
|
141 |
val InternalError = -32603 |
|
142 |
val serverErrorStart = -32099 |
|
143 |
val serverErrorEnd = -32000 |
|
144 |
} |
|
145 |
||
146 |
||
147 |
/* init and exit */ |
|
148 |
||
149 |
object Initialize extends Request0("initialize") |
|
150 |
{ |
|
151 |
def reply(id: Id, error: String): JSON.T = |
|
67850 | 152 |
ResponseMessage.strict( |
153 |
id, Some(JSON.Object("capabilities" -> ServerCapabilities.json)), error) |
|
64605 | 154 |
} |
155 |
||
156 |
object ServerCapabilities |
|
157 |
{ |
|
158 |
val json: JSON.T = |
|
67850 | 159 |
JSON.Object( |
65160 | 160 |
"textDocumentSync" -> 2, |
67850 | 161 |
"completionProvider" -> JSON.Object("resolveProvider" -> false, "triggerCharacters" -> Nil), |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
162 |
"hoverProvider" -> true, |
64767 | 163 |
"definitionProvider" -> true, |
164 |
"documentHighlightProvider" -> true) |
|
64605 | 165 |
} |
166 |
||
65229 | 167 |
object Initialized extends Notification0("initialized") |
168 |
||
64605 | 169 |
object Shutdown extends Request0("shutdown") |
170 |
{ |
|
171 |
def reply(id: Id, error: String): JSON.T = |
|
172 |
ResponseMessage.strict(id, Some("OK"), error) |
|
173 |
} |
|
174 |
||
175 |
object Exit extends Notification0("exit") |
|
176 |
||
177 |
||
178 |
/* document positions */ |
|
179 |
||
180 |
object Position |
|
181 |
{ |
|
182 |
def apply(pos: Line.Position): JSON.T = |
|
67850 | 183 |
JSON.Object("line" -> pos.line, "character" -> pos.column) |
64605 | 184 |
|
185 |
def unapply(json: JSON.T): Option[Line.Position] = |
|
186 |
for { |
|
187 |
line <- JSON.int(json, "line") |
|
188 |
column <- JSON.int(json, "character") |
|
189 |
} yield Line.Position(line, column) |
|
190 |
} |
|
191 |
||
192 |
object Range |
|
193 |
{ |
|
65173 | 194 |
def compact(range: Line.Range): List[Int] = |
195 |
List(range.start.line, range.start.column, range.stop.line, range.stop.column) |
|
196 |
||
64605 | 197 |
def apply(range: Line.Range): JSON.T = |
67850 | 198 |
JSON.Object("start" -> Position(range.start), "end" -> Position(range.stop)) |
64605 | 199 |
|
200 |
def unapply(json: JSON.T): Option[Line.Range] = |
|
201 |
(JSON.value(json, "start"), JSON.value(json, "end")) match { |
|
202 |
case (Some(Position(start)), Some(Position(stop))) => Some(Line.Range(start, stop)) |
|
203 |
case _ => None |
|
204 |
} |
|
205 |
} |
|
206 |
||
207 |
object Location |
|
208 |
{ |
|
64651 | 209 |
def apply(loc: Line.Node_Range): JSON.T = |
67850 | 210 |
JSON.Object("uri" -> Url.print_file_name(loc.name), "range" -> Range(loc.range)) |
64605 | 211 |
|
64651 | 212 |
def unapply(json: JSON.T): Option[Line.Node_Range] = |
64605 | 213 |
for { |
214 |
uri <- JSON.string(json, "uri") |
|
215 |
range_json <- JSON.value(json, "range") |
|
216 |
range <- Range.unapply(range_json) |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
217 |
} yield Line.Node_Range(Url.absolute_file_name(uri), range) |
64605 | 218 |
} |
219 |
||
220 |
object TextDocumentPosition |
|
221 |
{ |
|
64651 | 222 |
def unapply(json: JSON.T): Option[Line.Node_Position] = |
64605 | 223 |
for { |
224 |
doc <- JSON.value(json, "textDocument") |
|
225 |
uri <- JSON.string(doc, "uri") |
|
226 |
pos_json <- JSON.value(json, "position") |
|
227 |
pos <- Position.unapply(pos_json) |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
228 |
} yield Line.Node_Position(Url.absolute_file_name(uri), pos) |
64605 | 229 |
} |
230 |
||
231 |
||
65103 | 232 |
/* marked strings */ |
65093 | 233 |
|
234 |
sealed case class MarkedString(text: String, language: String = "plaintext") |
|
235 |
{ |
|
67850 | 236 |
def json: JSON.T = JSON.Object("language" -> language, "value" -> text) |
65093 | 237 |
} |
238 |
||
65103 | 239 |
object MarkedStrings |
240 |
{ |
|
241 |
def json(msgs: List[MarkedString]): Option[JSON.T] = |
|
242 |
msgs match { |
|
243 |
case Nil => None |
|
244 |
case List(msg) => Some(msg.json) |
|
245 |
case _ => Some(msgs.map(_.json)) |
|
246 |
} |
|
247 |
} |
|
248 |
||
65093 | 249 |
|
64605 | 250 |
/* diagnostic messages */ |
251 |
||
252 |
object MessageType |
|
253 |
{ |
|
254 |
val Error = 1 |
|
255 |
val Warning = 2 |
|
256 |
val Info = 3 |
|
257 |
val Log = 4 |
|
258 |
} |
|
259 |
||
260 |
object DisplayMessage |
|
261 |
{ |
|
262 |
def apply(message_type: Int, message: String, show: Boolean = true): JSON.T = |
|
263 |
Notification(if (show) "window/showMessage" else "window/logMessage", |
|
67850 | 264 |
JSON.Object("type" -> message_type, "message" -> message)) |
64605 | 265 |
} |
266 |
||
267 |
||
66138 | 268 |
/* commands */ |
269 |
||
270 |
sealed case class Command(title: String, command: String, arguments: List[JSON.T] = Nil) |
|
271 |
{ |
|
272 |
def json: JSON.T = |
|
67850 | 273 |
JSON.Object("title" -> title, "command" -> command, "arguments" -> arguments) |
66138 | 274 |
} |
275 |
||
276 |
||
64605 | 277 |
/* document edits */ |
278 |
||
279 |
object DidOpenTextDocument |
|
280 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
281 |
def unapply(json: JSON.T): Option[(JFile, String, Long, String)] = |
64605 | 282 |
json match { |
283 |
case Notification("textDocument/didOpen", Some(params)) => |
|
284 |
for { |
|
285 |
doc <- JSON.value(params, "textDocument") |
|
286 |
uri <- JSON.string(doc, "uri") |
|
287 |
lang <- JSON.string(doc, "languageId") |
|
288 |
version <- JSON.long(doc, "version") |
|
289 |
text <- JSON.string(doc, "text") |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
290 |
} yield (Url.absolute_file(uri), lang, version, text) |
64605 | 291 |
case _ => None |
292 |
} |
|
293 |
} |
|
294 |
||
295 |
||
65160 | 296 |
sealed case class TextDocumentChange(range: Option[Line.Range], text: String) |
64605 | 297 |
|
298 |
object DidChangeTextDocument |
|
299 |
{ |
|
65160 | 300 |
def unapply_change(json: JSON.T): Option[TextDocumentChange] = |
301 |
for { text <- JSON.string(json, "text") } |
|
302 |
yield TextDocumentChange(JSON.value(json, "range", Range.unapply _), text) |
|
303 |
||
304 |
def unapply(json: JSON.T): Option[(JFile, Long, List[TextDocumentChange])] = |
|
64605 | 305 |
json match { |
306 |
case Notification("textDocument/didChange", Some(params)) => |
|
307 |
for { |
|
308 |
doc <- JSON.value(params, "textDocument") |
|
309 |
uri <- JSON.string(doc, "uri") |
|
310 |
version <- JSON.long(doc, "version") |
|
67843 | 311 |
changes <- JSON.list(params, "contentChanges", unapply_change _) |
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
312 |
} yield (Url.absolute_file(uri), version, changes) |
64605 | 313 |
case _ => None |
314 |
} |
|
315 |
} |
|
316 |
||
317 |
class TextDocumentNotification(name: String) |
|
318 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
319 |
def unapply(json: JSON.T): Option[JFile] = |
64605 | 320 |
json match { |
321 |
case Notification(method, Some(params)) if method == name => |
|
322 |
for { |
|
323 |
doc <- JSON.value(params, "textDocument") |
|
324 |
uri <- JSON.string(doc, "uri") |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
325 |
} yield Url.absolute_file(uri) |
64605 | 326 |
case _ => None |
327 |
} |
|
328 |
} |
|
329 |
||
330 |
object DidCloseTextDocument extends TextDocumentNotification("textDocument/didClose") |
|
331 |
object DidSaveTextDocument extends TextDocumentNotification("textDocument/didSave") |
|
332 |
||
333 |
||
66675 | 334 |
/* workspace edits */ |
335 |
||
336 |
sealed case class TextEdit(range: Line.Range, new_text: String) |
|
337 |
{ |
|
67850 | 338 |
def json: JSON.T = JSON.Object("range" -> Range(range), "newText" -> new_text) |
66675 | 339 |
} |
340 |
||
341 |
sealed case class TextDocumentEdit(file: JFile, version: Long, edits: List[TextEdit]) |
|
342 |
{ |
|
343 |
def json: JSON.T = |
|
67850 | 344 |
JSON.Object( |
345 |
"textDocument" -> JSON.Object("uri" -> Url.print_file(file), "version" -> version), |
|
66675 | 346 |
"edits" -> edits.map(_.json)) |
347 |
} |
|
348 |
||
349 |
object WorkspaceEdit |
|
350 |
{ |
|
351 |
def apply(edits: List[TextDocumentEdit]): JSON.T = |
|
352 |
RequestMessage(Id.empty, "workspace/applyEdit", |
|
67850 | 353 |
JSON.Object("edit" -> JSON.Object("documentChanges" -> edits.map(_.json)))) |
66675 | 354 |
} |
355 |
||
356 |
||
64877 | 357 |
/* completion */ |
358 |
||
359 |
sealed case class CompletionItem( |
|
360 |
label: String, |
|
361 |
kind: Option[Int] = None, |
|
362 |
detail: Option[String] = None, |
|
363 |
documentation: Option[String] = None, |
|
66140 | 364 |
text: Option[String] = None, |
66138 | 365 |
range: Option[Line.Range] = None, |
366 |
command: Option[Command] = None) |
|
64877 | 367 |
{ |
368 |
def json: JSON.T = |
|
67850 | 369 |
JSON.Object("label" -> label) ++ |
66061 | 370 |
JSON.optional("kind" -> kind) ++ |
371 |
JSON.optional("detail" -> detail) ++ |
|
372 |
JSON.optional("documentation" -> documentation) ++ |
|
66140 | 373 |
JSON.optional("insertText" -> text) ++ |
66062
175772371cd0
use old-style "textEdit" for the sake of the external protocol (see also vscode-languageserver-node/issues/188);
wenzelm
parents:
66061
diff
changeset
|
374 |
JSON.optional("range" -> range.map(Range(_))) ++ |
66675 | 375 |
JSON.optional("textEdit" -> range.map(r => TextEdit(r, text.getOrElse(label)).json)) ++ |
66138 | 376 |
JSON.optional("command" -> command.map(_.json)) |
64877 | 377 |
} |
378 |
||
379 |
object Completion extends RequestTextDocumentPosition("textDocument/completion") |
|
380 |
{ |
|
381 |
def reply(id: Id, result: List[CompletionItem]): JSON.T = |
|
382 |
ResponseMessage(id, Some(result.map(_.json))) |
|
383 |
} |
|
384 |
||
385 |
||
66138 | 386 |
/* spell checker */ |
387 |
||
388 |
object Include_Word extends Notification0("PIDE/include_word") |
|
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
389 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
390 |
val command = Command("Include word", "isabelle.include-word") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
391 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
392 |
|
66138 | 393 |
object Include_Word_Permanently extends Notification0("PIDE/include_word_permanently") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
394 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
395 |
val command = Command("Include word permanently", "isabelle.include-word-permanently") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
396 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
397 |
|
66138 | 398 |
object Exclude_Word extends Notification0("PIDE/exclude_word") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
399 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
400 |
val command = Command("Exclude word", "isabelle.exclude-word") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
401 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
402 |
|
66138 | 403 |
object Exclude_Word_Permanently extends Notification0("PIDE/exclude_word_permanently") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
404 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
405 |
val command = Command("Exclude word permanently", "isabelle.exclude-word-permanently") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
406 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
407 |
|
66138 | 408 |
object Reset_Words extends Notification0("PIDE/reset_words") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
409 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
410 |
val command = Command("Reset non-permanent words", "isabelle.reset-words") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
411 |
} |
66138 | 412 |
|
413 |
||
64605 | 414 |
/* hover request */ |
415 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
416 |
object Hover extends RequestTextDocumentPosition("textDocument/hover") |
64605 | 417 |
{ |
65093 | 418 |
def reply(id: Id, result: Option[(Line.Range, List[MarkedString])]): JSON.T = |
64605 | 419 |
{ |
420 |
val res = |
|
421 |
result match { |
|
64747 | 422 |
case Some((range, contents)) => |
67850 | 423 |
JSON.Object( |
424 |
"contents" -> MarkedStrings.json(contents).getOrElse(Nil), |
|
425 |
"range" -> Range(range)) |
|
426 |
case None => JSON.Object("contents" -> Nil) |
|
64605 | 427 |
} |
428 |
ResponseMessage(id, Some(res)) |
|
429 |
} |
|
430 |
} |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
431 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
432 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
433 |
/* goto definition request */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
434 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
435 |
object GotoDefinition extends RequestTextDocumentPosition("textDocument/definition") |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
436 |
{ |
64651 | 437 |
def reply(id: Id, result: List[Line.Node_Range]): JSON.T = |
64649 | 438 |
ResponseMessage(id, Some(result.map(Location.apply(_)))) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
439 |
} |
64675 | 440 |
|
441 |
||
64767 | 442 |
/* document highlights request */ |
443 |
||
444 |
object DocumentHighlight |
|
445 |
{ |
|
446 |
def text(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(1)) |
|
447 |
def read(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(2)) |
|
448 |
def write(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(3)) |
|
449 |
} |
|
450 |
||
451 |
sealed case class DocumentHighlight(range: Line.Range, kind: Option[Int] = None) |
|
452 |
{ |
|
453 |
def json: JSON.T = |
|
454 |
kind match { |
|
67850 | 455 |
case None => JSON.Object("range" -> Range(range)) |
456 |
case Some(k) => JSON.Object("range" -> Range(range), "kind" -> k) |
|
64767 | 457 |
} |
458 |
} |
|
459 |
||
460 |
object DocumentHighlights extends RequestTextDocumentPosition("textDocument/documentHighlight") |
|
461 |
{ |
|
462 |
def reply(id: Id, result: List[DocumentHighlight]): JSON.T = |
|
463 |
ResponseMessage(id, Some(result.map(_.json))) |
|
464 |
} |
|
465 |
||
466 |
||
64675 | 467 |
/* diagnostics */ |
468 |
||
469 |
sealed case class Diagnostic(range: Line.Range, message: String, |
|
470 |
severity: Option[Int] = None, code: Option[Int] = None, source: Option[String] = None) |
|
471 |
{ |
|
472 |
def json: JSON.T = |
|
473 |
Message.empty + ("range" -> Range(range)) + ("message" -> message) ++ |
|
64878 | 474 |
JSON.optional("severity" -> severity) ++ |
475 |
JSON.optional("code" -> code) ++ |
|
476 |
JSON.optional("source" -> source) |
|
64675 | 477 |
} |
478 |
||
479 |
object DiagnosticSeverity |
|
480 |
{ |
|
64762
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
481 |
val Error = 1 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
482 |
val Warning = 2 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
483 |
val Information = 3 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
484 |
val Hint = 4 |
64675 | 485 |
} |
486 |
||
487 |
object PublishDiagnostics |
|
488 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
489 |
def apply(file: JFile, diagnostics: List[Diagnostic]): JSON.T = |
64675 | 490 |
Notification("textDocument/publishDiagnostics", |
67850 | 491 |
JSON.Object("uri" -> Url.print_file(file), "diagnostics" -> diagnostics.map(_.json))) |
64675 | 492 |
} |
65094 | 493 |
|
494 |
||
495 |
/* decorations */ |
|
496 |
||
65173 | 497 |
sealed case class DecorationOpts(range: Line.Range, hover_message: List[MarkedString]) |
65094 | 498 |
{ |
499 |
def json: JSON.T = |
|
67850 | 500 |
JSON.Object("range" -> Range.compact(range)) ++ |
65173 | 501 |
JSON.optional("hover_message" -> MarkedStrings.json(hover_message)) |
65094 | 502 |
} |
503 |
||
65173 | 504 |
sealed case class Decoration(typ: String, content: List[DecorationOpts]) |
65094 | 505 |
{ |
65095 | 506 |
def json(file: JFile): JSON.T = |
65094 | 507 |
Notification("PIDE/decoration", |
67850 | 508 |
JSON.Object("uri" -> Url.print_file(file), "type" -> typ, "content" -> content.map(_.json))) |
65094 | 509 |
} |
65189 | 510 |
|
511 |
||
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
512 |
/* caret update: bidirectional */ |
65189 | 513 |
|
514 |
object Caret_Update |
|
515 |
{ |
|
66216 | 516 |
def apply(node_pos: Line.Node_Position, focus: Boolean): JSON.T = |
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
517 |
Notification("PIDE/caret_update", |
67850 | 518 |
JSON.Object( |
519 |
"uri" -> Url.print_file_name(node_pos.name), |
|
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
520 |
"line" -> node_pos.pos.line, |
66216 | 521 |
"character" -> node_pos.pos.column, |
522 |
"focus" -> focus)) |
|
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
523 |
|
65189 | 524 |
def unapply(json: JSON.T): Option[Option[(JFile, Line.Position)]] = |
525 |
json match { |
|
526 |
case Notification("PIDE/caret_update", Some(params)) => |
|
527 |
val caret = |
|
528 |
for { |
|
529 |
uri <- JSON.string(params, "uri") |
|
530 |
if Url.is_wellformed_file(uri) |
|
531 |
pos <- Position.unapply(params) |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
532 |
} yield (Url.absolute_file(uri), pos) |
65189 | 533 |
Some(caret) |
534 |
case _ => None |
|
535 |
} |
|
536 |
} |
|
537 |
||
65201 | 538 |
|
539 |
/* dynamic output */ |
|
540 |
||
65189 | 541 |
object Dynamic_Output |
542 |
{ |
|
66090 | 543 |
def apply(content: String): JSON.T = |
67850 | 544 |
Notification("PIDE/dynamic_output", JSON.Object("content" -> content)) |
65189 | 545 |
} |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
546 |
|
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
547 |
|
66096 | 548 |
/* state output */ |
549 |
||
550 |
object State_Output |
|
551 |
{ |
|
552 |
def apply(id: Counter.ID, content: String): JSON.T = |
|
67850 | 553 |
Notification("PIDE/state_output", JSON.Object("id" -> id, "content" -> content)) |
66096 | 554 |
} |
555 |
||
556 |
class State_Id_Notification(name: String) |
|
557 |
{ |
|
558 |
def unapply(json: JSON.T): Option[Counter.ID] = |
|
559 |
json match { |
|
560 |
case Notification(method, Some(params)) if method == name => JSON.long(params, "id") |
|
561 |
case _ => None |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
object State_Init extends Notification0("PIDE/state_init") |
|
566 |
object State_Exit extends State_Id_Notification("PIDE/state_exit") |
|
567 |
object State_Locate extends State_Id_Notification("PIDE/state_locate") |
|
568 |
object State_Update extends State_Id_Notification("PIDE/state_update") |
|
569 |
||
66211 | 570 |
object State_Auto_Update |
571 |
{ |
|
572 |
def unapply(json: JSON.T): Option[(Counter.ID, Boolean)] = |
|
573 |
json match { |
|
574 |
case Notification("PIDE/state_auto_update", Some(params)) => |
|
575 |
for { |
|
576 |
id <- JSON.long(params, "id") |
|
577 |
enabled <- JSON.bool(params, "enabled") |
|
578 |
} yield (id, enabled) |
|
579 |
case _ => None |
|
580 |
} |
|
581 |
} |
|
582 |
||
66096 | 583 |
|
65983 | 584 |
/* preview */ |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
585 |
|
65983 | 586 |
object Preview_Request |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
587 |
{ |
65983 | 588 |
def unapply(json: JSON.T): Option[(JFile, Int)] = |
589 |
json match { |
|
590 |
case Notification("PIDE/preview_request", Some(params)) => |
|
591 |
for { |
|
592 |
uri <- JSON.string(params, "uri") |
|
593 |
if Url.is_wellformed_file(uri) |
|
594 |
column <- JSON.int(params, "column") |
|
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
595 |
} yield (Url.absolute_file(uri), column) |
65983 | 596 |
case _ => None |
597 |
} |
|
598 |
} |
|
599 |
||
600 |
object Preview_Response |
|
601 |
{ |
|
602 |
def apply(file: JFile, column: Int, label: String, content: String): JSON.T = |
|
603 |
Notification("PIDE/preview_response", |
|
67850 | 604 |
JSON.Object( |
65983 | 605 |
"uri" -> Url.print_file(file), |
606 |
"column" -> column, |
|
607 |
"label" -> label, |
|
608 |
"content" -> content)) |
|
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
609 |
} |
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
610 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
611 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
612 |
/* Isabelle symbols */ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
613 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
614 |
object Symbols_Request extends Notification0("PIDE/symbols_request") |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
615 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
616 |
object Symbols |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
617 |
{ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
618 |
def apply(): JSON.T = |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
619 |
{ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
620 |
val entries = |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
621 |
for ((sym, code) <- Symbol.codes) |
67850 | 622 |
yield JSON.Object("symbol" -> sym, "name" -> Symbol.names(sym)._1, "code" -> code) |
623 |
Notification("PIDE/symbols", JSON.Object("entries" -> entries)) |
|
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
624 |
} |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
625 |
} |
64605 | 626 |
} |