author | wenzelm |
Thu, 03 Mar 2022 15:39:51 +0100 | |
changeset 75197 | 29e11ce79a52 |
parent 75195 | 596e77cda169 |
child 75199 | 1ced8ee860e2 |
permissions | -rw-r--r-- |
72761 | 1 |
/* Title: Tools/VSCode/src/lsp.scala |
64605 | 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 |
|
72761 | 16 |
object LSP |
64605 | 17 |
{ |
18 |
/* abstract message */ |
|
19 |
||
20 |
object Message |
|
21 |
{ |
|
67850 | 22 |
val empty: JSON.Object.T = JSON.Object("jsonrpc" -> "2.0") |
65922 | 23 |
|
73340 | 24 |
def log(prefix: String, json: JSON.T, logger: Logger, verbose: Boolean): Unit = |
65922 | 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] || |
|
73120
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents:
72761
diff
changeset
|
74 |
id.isInstanceOf[String], "bad id type") |
64605 | 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, |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
161 |
"completionProvider" -> JSON.Object( |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
162 |
"resolveProvider" -> false, |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
163 |
"triggerCharacters" -> |
75195 | 164 |
Symbol.symbols.abbrevs.values.flatMap(_.iterator).map(_.toString).toList.distinct |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
165 |
), |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
166 |
"hoverProvider" -> true, |
64767 | 167 |
"definitionProvider" -> true, |
168 |
"documentHighlightProvider" -> true) |
|
64605 | 169 |
} |
170 |
||
65229 | 171 |
object Initialized extends Notification0("initialized") |
172 |
||
64605 | 173 |
object Shutdown extends Request0("shutdown") |
174 |
{ |
|
175 |
def reply(id: Id, error: String): JSON.T = |
|
176 |
ResponseMessage.strict(id, Some("OK"), error) |
|
177 |
} |
|
178 |
||
179 |
object Exit extends Notification0("exit") |
|
180 |
||
181 |
||
182 |
/* document positions */ |
|
183 |
||
184 |
object Position |
|
185 |
{ |
|
186 |
def apply(pos: Line.Position): JSON.T = |
|
67850 | 187 |
JSON.Object("line" -> pos.line, "character" -> pos.column) |
64605 | 188 |
|
189 |
def unapply(json: JSON.T): Option[Line.Position] = |
|
190 |
for { |
|
191 |
line <- JSON.int(json, "line") |
|
192 |
column <- JSON.int(json, "character") |
|
193 |
} yield Line.Position(line, column) |
|
194 |
} |
|
195 |
||
196 |
object Range |
|
197 |
{ |
|
65173 | 198 |
def compact(range: Line.Range): List[Int] = |
199 |
List(range.start.line, range.start.column, range.stop.line, range.stop.column) |
|
200 |
||
64605 | 201 |
def apply(range: Line.Range): JSON.T = |
67850 | 202 |
JSON.Object("start" -> Position(range.start), "end" -> Position(range.stop)) |
64605 | 203 |
|
204 |
def unapply(json: JSON.T): Option[Line.Range] = |
|
205 |
(JSON.value(json, "start"), JSON.value(json, "end")) match { |
|
206 |
case (Some(Position(start)), Some(Position(stop))) => Some(Line.Range(start, stop)) |
|
207 |
case _ => None |
|
208 |
} |
|
209 |
} |
|
210 |
||
211 |
object Location |
|
212 |
{ |
|
64651 | 213 |
def apply(loc: Line.Node_Range): JSON.T = |
67850 | 214 |
JSON.Object("uri" -> Url.print_file_name(loc.name), "range" -> Range(loc.range)) |
64605 | 215 |
|
64651 | 216 |
def unapply(json: JSON.T): Option[Line.Node_Range] = |
64605 | 217 |
for { |
218 |
uri <- JSON.string(json, "uri") |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
219 |
if Url.is_wellformed_file(uri) |
64605 | 220 |
range_json <- JSON.value(json, "range") |
221 |
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
|
222 |
} yield Line.Node_Range(Url.absolute_file_name(uri), range) |
64605 | 223 |
} |
224 |
||
225 |
object TextDocumentPosition |
|
226 |
{ |
|
64651 | 227 |
def unapply(json: JSON.T): Option[Line.Node_Position] = |
64605 | 228 |
for { |
229 |
doc <- JSON.value(json, "textDocument") |
|
230 |
uri <- JSON.string(doc, "uri") |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
231 |
if Url.is_wellformed_file(uri) |
64605 | 232 |
pos_json <- JSON.value(json, "position") |
233 |
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
|
234 |
} yield Line.Node_Position(Url.absolute_file_name(uri), pos) |
64605 | 235 |
} |
236 |
||
237 |
||
65103 | 238 |
/* marked strings */ |
65093 | 239 |
|
240 |
sealed case class MarkedString(text: String, language: String = "plaintext") |
|
241 |
{ |
|
67850 | 242 |
def json: JSON.T = JSON.Object("language" -> language, "value" -> text) |
65093 | 243 |
} |
244 |
||
65103 | 245 |
object MarkedStrings |
246 |
{ |
|
247 |
def json(msgs: List[MarkedString]): Option[JSON.T] = |
|
248 |
msgs match { |
|
249 |
case Nil => None |
|
250 |
case List(msg) => Some(msg.json) |
|
251 |
case _ => Some(msgs.map(_.json)) |
|
252 |
} |
|
253 |
} |
|
254 |
||
65093 | 255 |
|
64605 | 256 |
/* diagnostic messages */ |
257 |
||
258 |
object MessageType |
|
259 |
{ |
|
260 |
val Error = 1 |
|
261 |
val Warning = 2 |
|
262 |
val Info = 3 |
|
263 |
val Log = 4 |
|
264 |
} |
|
265 |
||
266 |
object DisplayMessage |
|
267 |
{ |
|
73541 | 268 |
def apply(message_type: Int, message: String, show: Boolean): JSON.T = |
64605 | 269 |
Notification(if (show) "window/showMessage" else "window/logMessage", |
67850 | 270 |
JSON.Object("type" -> message_type, "message" -> message)) |
64605 | 271 |
} |
272 |
||
273 |
||
66138 | 274 |
/* commands */ |
275 |
||
276 |
sealed case class Command(title: String, command: String, arguments: List[JSON.T] = Nil) |
|
277 |
{ |
|
278 |
def json: JSON.T = |
|
67850 | 279 |
JSON.Object("title" -> title, "command" -> command, "arguments" -> arguments) |
66138 | 280 |
} |
281 |
||
282 |
||
64605 | 283 |
/* document edits */ |
284 |
||
285 |
object DidOpenTextDocument |
|
286 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
287 |
def unapply(json: JSON.T): Option[(JFile, String, Long, String)] = |
64605 | 288 |
json match { |
289 |
case Notification("textDocument/didOpen", Some(params)) => |
|
290 |
for { |
|
291 |
doc <- JSON.value(params, "textDocument") |
|
292 |
uri <- JSON.string(doc, "uri") |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
293 |
if Url.is_wellformed_file(uri) |
64605 | 294 |
lang <- JSON.string(doc, "languageId") |
295 |
version <- JSON.long(doc, "version") |
|
296 |
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
|
297 |
} yield (Url.absolute_file(uri), lang, version, text) |
64605 | 298 |
case _ => None |
299 |
} |
|
300 |
} |
|
301 |
||
302 |
||
65160 | 303 |
sealed case class TextDocumentChange(range: Option[Line.Range], text: String) |
64605 | 304 |
|
305 |
object DidChangeTextDocument |
|
306 |
{ |
|
65160 | 307 |
def unapply_change(json: JSON.T): Option[TextDocumentChange] = |
308 |
for { text <- JSON.string(json, "text") } |
|
309 |
yield TextDocumentChange(JSON.value(json, "range", Range.unapply _), text) |
|
310 |
||
311 |
def unapply(json: JSON.T): Option[(JFile, Long, List[TextDocumentChange])] = |
|
64605 | 312 |
json match { |
313 |
case Notification("textDocument/didChange", Some(params)) => |
|
314 |
for { |
|
315 |
doc <- JSON.value(params, "textDocument") |
|
316 |
uri <- JSON.string(doc, "uri") |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
317 |
if Url.is_wellformed_file(uri) |
64605 | 318 |
version <- JSON.long(doc, "version") |
67843 | 319 |
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
|
320 |
} yield (Url.absolute_file(uri), version, changes) |
64605 | 321 |
case _ => None |
322 |
} |
|
323 |
} |
|
324 |
||
325 |
class TextDocumentNotification(name: String) |
|
326 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
327 |
def unapply(json: JSON.T): Option[JFile] = |
64605 | 328 |
json match { |
329 |
case Notification(method, Some(params)) if method == name => |
|
330 |
for { |
|
331 |
doc <- JSON.value(params, "textDocument") |
|
332 |
uri <- JSON.string(doc, "uri") |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
333 |
if Url.is_wellformed_file(uri) |
66235
d4fa51e7c4ff
retain symlinks in file names from VSCode: relevant for proper file locations in decorations etc.;
wenzelm
parents:
66216
diff
changeset
|
334 |
} yield Url.absolute_file(uri) |
64605 | 335 |
case _ => None |
336 |
} |
|
337 |
} |
|
338 |
||
339 |
object DidCloseTextDocument extends TextDocumentNotification("textDocument/didClose") |
|
340 |
object DidSaveTextDocument extends TextDocumentNotification("textDocument/didSave") |
|
341 |
||
342 |
||
66675 | 343 |
/* workspace edits */ |
344 |
||
345 |
sealed case class TextEdit(range: Line.Range, new_text: String) |
|
346 |
{ |
|
67850 | 347 |
def json: JSON.T = JSON.Object("range" -> Range(range), "newText" -> new_text) |
66675 | 348 |
} |
349 |
||
350 |
sealed case class TextDocumentEdit(file: JFile, version: Long, edits: List[TextEdit]) |
|
351 |
{ |
|
352 |
def json: JSON.T = |
|
67850 | 353 |
JSON.Object( |
354 |
"textDocument" -> JSON.Object("uri" -> Url.print_file(file), "version" -> version), |
|
66675 | 355 |
"edits" -> edits.map(_.json)) |
356 |
} |
|
357 |
||
358 |
object WorkspaceEdit |
|
359 |
{ |
|
360 |
def apply(edits: List[TextDocumentEdit]): JSON.T = |
|
361 |
RequestMessage(Id.empty, "workspace/applyEdit", |
|
67850 | 362 |
JSON.Object("edit" -> JSON.Object("documentChanges" -> edits.map(_.json)))) |
66675 | 363 |
} |
364 |
||
365 |
||
64877 | 366 |
/* completion */ |
367 |
||
368 |
sealed case class CompletionItem( |
|
369 |
label: String, |
|
370 |
kind: Option[Int] = None, |
|
371 |
detail: Option[String] = None, |
|
372 |
documentation: Option[String] = None, |
|
66140 | 373 |
text: Option[String] = None, |
66138 | 374 |
range: Option[Line.Range] = None, |
375 |
command: Option[Command] = None) |
|
64877 | 376 |
{ |
377 |
def json: JSON.T = |
|
67850 | 378 |
JSON.Object("label" -> label) ++ |
66061 | 379 |
JSON.optional("kind" -> kind) ++ |
380 |
JSON.optional("detail" -> detail) ++ |
|
381 |
JSON.optional("documentation" -> documentation) ++ |
|
66140 | 382 |
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
|
383 |
JSON.optional("range" -> range.map(Range(_))) ++ |
66675 | 384 |
JSON.optional("textEdit" -> range.map(r => TextEdit(r, text.getOrElse(label)).json)) ++ |
66138 | 385 |
JSON.optional("command" -> command.map(_.json)) |
64877 | 386 |
} |
387 |
||
388 |
object Completion extends RequestTextDocumentPosition("textDocument/completion") |
|
389 |
{ |
|
390 |
def reply(id: Id, result: List[CompletionItem]): JSON.T = |
|
391 |
ResponseMessage(id, Some(result.map(_.json))) |
|
392 |
} |
|
393 |
||
394 |
||
66138 | 395 |
/* spell checker */ |
396 |
||
397 |
object Include_Word extends Notification0("PIDE/include_word") |
|
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
398 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
399 |
val command = Command("Include word", "isabelle.include-word") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
400 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
401 |
|
66138 | 402 |
object Include_Word_Permanently extends Notification0("PIDE/include_word_permanently") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
403 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
404 |
val command = Command("Include word permanently", "isabelle.include-word-permanently") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
405 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
406 |
|
66138 | 407 |
object Exclude_Word extends Notification0("PIDE/exclude_word") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
408 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
409 |
val command = Command("Exclude word", "isabelle.exclude-word") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
410 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
411 |
|
66138 | 412 |
object Exclude_Word_Permanently extends Notification0("PIDE/exclude_word_permanently") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
413 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
414 |
val command = Command("Exclude word permanently", "isabelle.exclude-word-permanently") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
415 |
} |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
416 |
|
66138 | 417 |
object Reset_Words extends Notification0("PIDE/reset_words") |
66139
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
418 |
{ |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
419 |
val command = Command("Reset non-permanent words", "isabelle.reset-words") |
6a8f8be2741c
provide spell-checker menu via completion commands;
wenzelm
parents:
66138
diff
changeset
|
420 |
} |
66138 | 421 |
|
422 |
||
64605 | 423 |
/* hover request */ |
424 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
425 |
object Hover extends RequestTextDocumentPosition("textDocument/hover") |
64605 | 426 |
{ |
65093 | 427 |
def reply(id: Id, result: Option[(Line.Range, List[MarkedString])]): JSON.T = |
64605 | 428 |
{ |
429 |
val res = |
|
430 |
result match { |
|
64747 | 431 |
case Some((range, contents)) => |
67850 | 432 |
JSON.Object( |
433 |
"contents" -> MarkedStrings.json(contents).getOrElse(Nil), |
|
434 |
"range" -> Range(range)) |
|
435 |
case None => JSON.Object("contents" -> Nil) |
|
64605 | 436 |
} |
437 |
ResponseMessage(id, Some(res)) |
|
438 |
} |
|
439 |
} |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
440 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
441 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
442 |
/* goto definition request */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
443 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
444 |
object GotoDefinition extends RequestTextDocumentPosition("textDocument/definition") |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
445 |
{ |
64651 | 446 |
def reply(id: Id, result: List[Line.Node_Range]): JSON.T = |
64649 | 447 |
ResponseMessage(id, Some(result.map(Location.apply(_)))) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64605
diff
changeset
|
448 |
} |
64675 | 449 |
|
450 |
||
64767 | 451 |
/* document highlights request */ |
452 |
||
453 |
object DocumentHighlight |
|
454 |
{ |
|
455 |
def text(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(1)) |
|
456 |
def read(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(2)) |
|
457 |
def write(range: Line.Range): DocumentHighlight = DocumentHighlight(range, Some(3)) |
|
458 |
} |
|
459 |
||
460 |
sealed case class DocumentHighlight(range: Line.Range, kind: Option[Int] = None) |
|
461 |
{ |
|
462 |
def json: JSON.T = |
|
463 |
kind match { |
|
67850 | 464 |
case None => JSON.Object("range" -> Range(range)) |
465 |
case Some(k) => JSON.Object("range" -> Range(range), "kind" -> k) |
|
64767 | 466 |
} |
467 |
} |
|
468 |
||
469 |
object DocumentHighlights extends RequestTextDocumentPosition("textDocument/documentHighlight") |
|
470 |
{ |
|
471 |
def reply(id: Id, result: List[DocumentHighlight]): JSON.T = |
|
472 |
ResponseMessage(id, Some(result.map(_.json))) |
|
473 |
} |
|
474 |
||
475 |
||
64675 | 476 |
/* diagnostics */ |
477 |
||
478 |
sealed case class Diagnostic(range: Line.Range, message: String, |
|
479 |
severity: Option[Int] = None, code: Option[Int] = None, source: Option[String] = None) |
|
480 |
{ |
|
481 |
def json: JSON.T = |
|
482 |
Message.empty + ("range" -> Range(range)) + ("message" -> message) ++ |
|
64878 | 483 |
JSON.optional("severity" -> severity) ++ |
484 |
JSON.optional("code" -> code) ++ |
|
485 |
JSON.optional("source" -> source) |
|
64675 | 486 |
} |
487 |
||
488 |
object DiagnosticSeverity |
|
489 |
{ |
|
64762
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
490 |
val Error = 1 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
491 |
val Warning = 2 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
492 |
val Information = 3 |
cd545bec3fd0
clarified message severity, based on empirical observation of VSCode 1.8.1;
wenzelm
parents:
64747
diff
changeset
|
493 |
val Hint = 4 |
64675 | 494 |
} |
495 |
||
496 |
object PublishDiagnostics |
|
497 |
{ |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
498 |
def apply(file: JFile, diagnostics: List[Diagnostic]): JSON.T = |
64675 | 499 |
Notification("textDocument/publishDiagnostics", |
67850 | 500 |
JSON.Object("uri" -> Url.print_file(file), "diagnostics" -> diagnostics.map(_.json))) |
64675 | 501 |
} |
65094 | 502 |
|
503 |
||
504 |
/* decorations */ |
|
505 |
||
65173 | 506 |
sealed case class DecorationOpts(range: Line.Range, hover_message: List[MarkedString]) |
65094 | 507 |
{ |
508 |
def json: JSON.T = |
|
67850 | 509 |
JSON.Object("range" -> Range.compact(range)) ++ |
65173 | 510 |
JSON.optional("hover_message" -> MarkedStrings.json(hover_message)) |
65094 | 511 |
} |
512 |
||
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
513 |
sealed case class Decoration(decorations: List[(String, List[DecorationOpts])]) |
65094 | 514 |
{ |
65095 | 515 |
def json(file: JFile): JSON.T = |
65094 | 516 |
Notification("PIDE/decoration", |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
517 |
JSON.Object( |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
518 |
"uri" -> Url.print_file(file), |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
519 |
"entries" -> decorations.map(decoration => JSON.Object( |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
520 |
"type" -> decoration._1, |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
521 |
"content" -> decoration._2.map(_.json)) |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
522 |
)) |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
523 |
) |
65094 | 524 |
} |
65189 | 525 |
|
526 |
||
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
527 |
/* caret update: bidirectional */ |
65189 | 528 |
|
529 |
object Caret_Update |
|
530 |
{ |
|
66216 | 531 |
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
|
532 |
Notification("PIDE/caret_update", |
67850 | 533 |
JSON.Object( |
534 |
"uri" -> Url.print_file_name(node_pos.name), |
|
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
535 |
"line" -> node_pos.pos.line, |
66216 | 536 |
"character" -> node_pos.pos.column, |
537 |
"focus" -> focus)) |
|
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
538 |
|
65189 | 539 |
def unapply(json: JSON.T): Option[Option[(JFile, Line.Position)]] = |
540 |
json match { |
|
541 |
case Notification("PIDE/caret_update", Some(params)) => |
|
542 |
val caret = |
|
543 |
for { |
|
544 |
uri <- JSON.string(params, "uri") |
|
545 |
if Url.is_wellformed_file(uri) |
|
546 |
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
|
547 |
} yield (Url.absolute_file(uri), pos) |
65189 | 548 |
Some(caret) |
549 |
case _ => None |
|
550 |
} |
|
551 |
} |
|
552 |
||
65201 | 553 |
|
554 |
/* dynamic output */ |
|
555 |
||
65189 | 556 |
object Dynamic_Output |
557 |
{ |
|
66090 | 558 |
def apply(content: String): JSON.T = |
67850 | 559 |
Notification("PIDE/dynamic_output", JSON.Object("content" -> content)) |
65189 | 560 |
} |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
561 |
|
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
562 |
|
66096 | 563 |
/* state output */ |
564 |
||
565 |
object State_Output |
|
566 |
{ |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
567 |
def apply(id: Counter.ID, content: String, auto_update: Boolean): JSON.T = |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
568 |
Notification("PIDE/state_output", JSON.Object("id" -> id, "content" -> content, "auto_update" -> auto_update)) |
66096 | 569 |
} |
570 |
||
571 |
class State_Id_Notification(name: String) |
|
572 |
{ |
|
573 |
def unapply(json: JSON.T): Option[Counter.ID] = |
|
574 |
json match { |
|
575 |
case Notification(method, Some(params)) if method == name => JSON.long(params, "id") |
|
576 |
case _ => None |
|
577 |
} |
|
578 |
} |
|
579 |
||
580 |
object State_Init extends Notification0("PIDE/state_init") |
|
581 |
object State_Exit extends State_Id_Notification("PIDE/state_exit") |
|
582 |
object State_Locate extends State_Id_Notification("PIDE/state_locate") |
|
583 |
object State_Update extends State_Id_Notification("PIDE/state_update") |
|
584 |
||
66211 | 585 |
object State_Auto_Update |
586 |
{ |
|
587 |
def unapply(json: JSON.T): Option[(Counter.ID, Boolean)] = |
|
588 |
json match { |
|
589 |
case Notification("PIDE/state_auto_update", Some(params)) => |
|
590 |
for { |
|
591 |
id <- JSON.long(params, "id") |
|
592 |
enabled <- JSON.bool(params, "enabled") |
|
593 |
} yield (id, enabled) |
|
594 |
case _ => None |
|
595 |
} |
|
596 |
} |
|
597 |
||
66096 | 598 |
|
65983 | 599 |
/* preview */ |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
600 |
|
65983 | 601 |
object Preview_Request |
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
602 |
{ |
65983 | 603 |
def unapply(json: JSON.T): Option[(JFile, Int)] = |
604 |
json match { |
|
605 |
case Notification("PIDE/preview_request", Some(params)) => |
|
606 |
for { |
|
607 |
uri <- JSON.string(params, "uri") |
|
608 |
if Url.is_wellformed_file(uri) |
|
609 |
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
|
610 |
} yield (Url.absolute_file(uri), column) |
65983 | 611 |
case _ => None |
612 |
} |
|
613 |
} |
|
614 |
||
615 |
object Preview_Response |
|
616 |
{ |
|
617 |
def apply(file: JFile, column: Int, label: String, content: String): JSON.T = |
|
618 |
Notification("PIDE/preview_response", |
|
67850 | 619 |
JSON.Object( |
65983 | 620 |
"uri" -> Url.print_file(file), |
621 |
"column" -> column, |
|
622 |
"label" -> label, |
|
623 |
"content" -> content)) |
|
65977
c51b74be23b6
provide preview content on Scala side (similar to output);
wenzelm
parents:
65924
diff
changeset
|
624 |
} |
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
625 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
626 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
627 |
/* Isabelle symbols */ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
628 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
629 |
object Symbols_Request extends Notification0("PIDE/symbols_request") |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
630 |
|
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
631 |
object Symbols |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
632 |
{ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
633 |
def apply(): JSON.T = |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
634 |
{ |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
635 |
val entries = |
75197 | 636 |
for (entry <- Symbol.symbols.entries; code <- entry.code) |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
637 |
yield JSON.Object( |
75197 | 638 |
"symbol" -> entry.symbol, |
639 |
"name" -> entry.name, |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
640 |
"code" -> code, |
75197 | 641 |
"abbrevs" -> entry.abbrevs |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
642 |
) |
67850 | 643 |
Notification("PIDE/symbols", JSON.Object("entries" -> entries)) |
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
644 |
} |
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
645 |
} |
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
646 |
|
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
647 |
/* Session structure */ |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
648 |
|
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
649 |
object Session_Theories_Request extends Notification0("PIDE/session_theories_request") |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
650 |
|
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
651 |
object Session_Theories { |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
652 |
def apply(session_theories: Map[String, List[JFile]]): JSON.T = { |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
653 |
val entries = session_theories.map { case(session_name, theories) => JSON.Object( |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
654 |
"session_name" -> session_name, |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
655 |
"theories" -> theories.map(Url.print_file) |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
656 |
)} |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
657 |
Notification("PIDE/session_theories", JSON.Object("entries" -> entries)) |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
658 |
} |
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73541
diff
changeset
|
659 |
} |
64605 | 660 |
} |