author | wenzelm |
Wed, 11 Jan 2017 20:01:55 +0100 | |
changeset 64877 | 31e9920a0dc1 |
parent 64870 | 41e2797af496 |
child 65093 | 5f08197206ce |
permissions | -rw-r--r-- |
64605 | 1 |
/* Title: Tools/VSCode/src/server.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Server for VS Code Language Server Protocol 2.0, see also |
|
5 |
https://github.com/Microsoft/language-server-protocol |
|
6 |
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md |
|
7 |
*/ |
|
8 |
||
9 |
package isabelle.vscode |
|
10 |
||
11 |
||
12 |
import isabelle._ |
|
13 |
||
64710 | 14 |
import java.io.{PrintStream, OutputStream, File => JFile} |
64605 | 15 |
|
16 |
import scala.annotation.tailrec |
|
17 |
||
18 |
||
19 |
object Server |
|
20 |
{ |
|
21 |
/* Isabelle tool wrapper */ |
|
22 |
||
64618 | 23 |
private val default_text_length = "UTF-16" |
64605 | 24 |
private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
25 |
||
26 |
val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args => |
|
27 |
{ |
|
64644 | 28 |
try { |
29 |
var log_file: Option[Path] = None |
|
64682 | 30 |
var text_length = Text.Length.encoding(default_text_length) |
64644 | 31 |
var dirs: List[Path] = Nil |
32 |
var logic = default_logic |
|
33 |
var modes: List[String] = Nil |
|
34 |
var options = Options.init() |
|
64733 | 35 |
var system_mode = false |
64605 | 36 |
|
64644 | 37 |
def text_length_choice: String = |
64682 | 38 |
commas(Text.Length.encodings.map( |
64644 | 39 |
{ case (a, _) => if (a == default_text_length) a + " (default)" else a })) |
64618 | 40 |
|
64644 | 41 |
val getopts = Getopts(""" |
64605 | 42 |
Usage: isabelle vscode_server [OPTIONS] |
43 |
||
44 |
Options are: |
|
45 |
-L FILE enable logging on FILE |
|
64618 | 46 |
-T LENGTH text length encoding: """ + text_length_choice + """ |
64605 | 47 |
-d DIR include session directory |
64737 | 48 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(default_logic) + """) |
64605 | 49 |
-m MODE add print mode for output |
50 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
64733 | 51 |
-s system build mode for session image |
64605 | 52 |
|
53 |
Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout. |
|
54 |
""", |
|
64755 | 55 |
"L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))), |
64682 | 56 |
"T:" -> (arg => Text.Length.encoding(arg)), |
64755 | 57 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(File.standard_path(arg)))), |
64644 | 58 |
"l:" -> (arg => logic = arg), |
59 |
"m:" -> (arg => modes = arg :: modes), |
|
64733 | 60 |
"o:" -> (arg => options = options + arg), |
61 |
"s" -> (_ => system_mode = true)) |
|
64605 | 62 |
|
64644 | 63 |
val more_args = getopts(args) |
64 |
if (more_args.nonEmpty) getopts.usage() |
|
65 |
||
64717 | 66 |
val log = Logger.make(log_file) |
67 |
val channel = new Channel(System.in, System.out, log) |
|
64733 | 68 |
val server = new Server(channel, options, text_length, logic, dirs, modes, system_mode, log) |
64605 | 69 |
|
64644 | 70 |
// prevent spurious garbage on the main protocol channel |
71 |
val orig_out = System.out |
|
72 |
try { |
|
73 |
System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} })) |
|
74 |
server.start() |
|
75 |
} |
|
76 |
finally { System.setOut(orig_out) } |
|
64605 | 77 |
} |
64644 | 78 |
catch { |
79 |
case exn: Throwable => |
|
64717 | 80 |
val channel = new Channel(System.in, System.out, No_Logger) |
64644 | 81 |
channel.error_message(Exn.message(exn)) |
82 |
throw(exn) |
|
83 |
} |
|
64605 | 84 |
}) |
85 |
} |
|
86 |
||
87 |
class Server( |
|
88 |
channel: Channel, |
|
89 |
options: Options, |
|
64682 | 90 |
text_length: Text.Length = Text.Length.encoding(Server.default_text_length), |
64605 | 91 |
session_name: String = Server.default_logic, |
92 |
session_dirs: List[Path] = Nil, |
|
64717 | 93 |
modes: List[String] = Nil, |
64733 | 94 |
system_mode: Boolean = false, |
64717 | 95 |
log: Logger = No_Logger) |
64605 | 96 |
{ |
64725 | 97 |
/* prover session */ |
64605 | 98 |
|
64703 | 99 |
private val session_ = Synchronized(None: Option[Session]) |
100 |
def session: Session = session_.value getOrElse error("Server inactive") |
|
64623 | 101 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
64605 | 102 |
|
64651 | 103 |
def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] = |
64649 | 104 |
for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
105 |
model <- resources.get_model(new JFile(node_pos.name)) |
64704 | 106 |
rendering = model.rendering() |
64830 | 107 |
offset <- model.content.doc.offset(node_pos.pos) |
64649 | 108 |
} yield (rendering, offset) |
109 |
||
64605 | 110 |
|
64725 | 111 |
/* input from client or file-system */ |
112 |
||
64800 | 113 |
private val delay_input: Standard_Thread.Delay = |
64810 | 114 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger) |
64725 | 115 |
{ resources.flush_input(session) } |
116 |
||
64800 | 117 |
private val delay_load: Standard_Thread.Delay = |
64810 | 118 |
Standard_Thread.delay_last(options.seconds("vscode_load_delay"), channel.Error_Logger) { |
64857 | 119 |
val (invoke_input, invoke_load) = resources.resolve_dependencies(session, file_watcher) |
64800 | 120 |
if (invoke_input) delay_input.invoke() |
121 |
if (invoke_load) delay_load.invoke |
|
122 |
} |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
123 |
|
64857 | 124 |
private val file_watcher = |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
125 |
File_Watcher(sync_documents(_), options.seconds("vscode_load_delay")) |
64725 | 126 |
|
127 |
private def sync_documents(changed: Set[JFile]): Unit = |
|
128 |
if (resources.sync_models(changed)) delay_input.invoke() |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
129 |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
130 |
private def update_document(file: JFile, text: String) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
131 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
132 |
resources.update_model(session, file, text) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
133 |
delay_input.invoke() |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
134 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
135 |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
136 |
private def close_document(file: JFile) |
64710 | 137 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
138 |
resources.close_model(file) match { |
64710 | 139 |
case Some(model) => |
64857 | 140 |
file_watcher.register_parent(file) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
141 |
sync_documents(Set(file)) |
64710 | 142 |
case None => |
143 |
} |
|
144 |
} |
|
145 |
||
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
146 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
147 |
/* output to client */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
148 |
|
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
149 |
private val delay_output: Standard_Thread.Delay = |
64810 | 150 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger) |
64703 | 151 |
{ |
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
152 |
if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke() |
64703 | 153 |
else resources.flush_output(channel) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
154 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
155 |
|
64725 | 156 |
private val prover_output = |
157 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
|
158 |
case changed if changed.nodes.nonEmpty => |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
159 |
resources.update_output(changed.nodes.toList.map(resources.node_file(_))) |
64725 | 160 |
delay_output.invoke() |
161 |
case _ => |
|
162 |
} |
|
64710 | 163 |
|
64725 | 164 |
private val syslog = |
64692 | 165 |
Session.Consumer[Prover.Message](getClass.getName) { |
166 |
case output: Prover.Output if output.is_syslog => |
|
64870 | 167 |
channel.log_writeln(resources.output_xml(output.message)) |
64692 | 168 |
case _ => |
169 |
} |
|
170 |
||
171 |
||
64605 | 172 |
/* init and exit */ |
173 |
||
64641 | 174 |
def init(id: Protocol.Id) |
64605 | 175 |
{ |
64641 | 176 |
def reply(err: String) |
177 |
{ |
|
178 |
channel.write(Protocol.Initialize.reply(id, err)) |
|
64642
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
179 |
if (err == "") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
180 |
channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
181 |
else channel.error_message(err) |
64641 | 182 |
} |
183 |
||
64643 | 184 |
val try_session = |
185 |
try { |
|
64733 | 186 |
if (!Build.build(options = options, build_heap = true, no_build = true, |
187 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
188 |
{ |
|
189 |
val start_msg = "Build started for Isabelle/" + session_name + " ..." |
|
190 |
val fail_msg = "Session build failed -- prover process remains inactive!" |
|
191 |
||
192 |
val progress = channel.make_progress(verbose = true) |
|
193 |
progress.echo(start_msg); channel.writeln(start_msg) |
|
194 |
||
195 |
if (!Build.build(options = options, progress = progress, build_heap = true, |
|
196 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
197 |
{ |
|
198 |
progress.echo(fail_msg); error(fail_msg) |
|
199 |
} |
|
200 |
} |
|
201 |
||
64856 | 202 |
val base = Build.session_base(options, false, session_dirs, session_name) |
64643 | 203 |
val resources = |
64856 | 204 |
new VSCode_Resources(options, text_length, base, log) |
64854 | 205 |
{ |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
206 |
override def commit(change: Session.Change): Unit = |
64800 | 207 |
if (change.deps_changed || undefined_blobs(change.version.nodes).nonEmpty) |
208 |
delay_load.invoke() |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
209 |
} |
64605 | 210 |
|
64643 | 211 |
Some(new Session(resources) { |
212 |
override def output_delay = options.seconds("editor_output_delay") |
|
213 |
override def prune_delay = options.seconds("editor_prune_delay") |
|
214 |
override def syslog_limit = options.int("editor_syslog_limit") |
|
215 |
override def reparse_limit = options.int("editor_reparse_limit") |
|
216 |
}) |
|
64605 | 217 |
} |
64643 | 218 |
catch { case ERROR(msg) => reply(msg); None } |
64605 | 219 |
|
64643 | 220 |
for (session <- try_session) { |
221 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
222 |
session_phase = |
|
223 |
Session.Consumer(getClass.getName) { |
|
224 |
case Session.Ready => |
|
225 |
session.phase_changed -= session_phase |
|
226 |
session.update_options(options) |
|
227 |
reply("") |
|
228 |
case Session.Failed => |
|
229 |
session.phase_changed -= session_phase |
|
230 |
reply("Prover startup failed") |
|
231 |
case _ => |
|
232 |
} |
|
233 |
session.phase_changed += session_phase |
|
64605 | 234 |
|
64725 | 235 |
session.commands_changed += prover_output |
236 |
session.all_messages += syslog |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
237 |
|
64643 | 238 |
session.start(receiver => |
239 |
Isabelle_Process(options = options, logic = session_name, dirs = session_dirs, |
|
240 |
modes = modes, receiver = receiver)) |
|
64605 | 241 |
|
64703 | 242 |
session_.change(_ => Some(session)) |
64643 | 243 |
} |
64605 | 244 |
} |
245 |
||
64641 | 246 |
def shutdown(id: Protocol.Id) |
64605 | 247 |
{ |
64641 | 248 |
def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err)) |
249 |
||
64703 | 250 |
session_.change({ |
251 |
case Some(session) => |
|
252 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
253 |
session_phase = |
|
254 |
Session.Consumer(getClass.getName) { |
|
255 |
case Session.Inactive => |
|
256 |
session.phase_changed -= session_phase |
|
64725 | 257 |
session.commands_changed -= prover_output |
258 |
session.all_messages -= syslog |
|
64703 | 259 |
reply("") |
260 |
case _ => |
|
261 |
} |
|
262 |
session.phase_changed += session_phase |
|
263 |
session.stop() |
|
264 |
delay_input.revoke() |
|
265 |
delay_output.revoke() |
|
64857 | 266 |
file_watcher.shutdown() |
64703 | 267 |
None |
268 |
case None => |
|
269 |
reply("Prover inactive") |
|
270 |
None |
|
271 |
}) |
|
64605 | 272 |
} |
273 |
||
64646 | 274 |
def exit() { |
64717 | 275 |
log("\n") |
64703 | 276 |
sys.exit(if (session_.value.isDefined) 1 else 0) |
64646 | 277 |
} |
64605 | 278 |
|
279 |
||
64877 | 280 |
/* completion */ |
281 |
||
282 |
def completion(id: Protocol.Id, node_pos: Line.Node_Position) |
|
283 |
{ |
|
284 |
val result = |
|
285 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
286 |
yield rendering.completion(Text.Range(offset - 1, offset))) getOrElse Nil |
|
287 |
channel.write(Protocol.Completion.reply(id, result)) |
|
288 |
} |
|
289 |
||
290 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
291 |
/* hover */ |
64605 | 292 |
|
64651 | 293 |
def hover(id: Protocol.Id, node_pos: Line.Node_Position) |
64641 | 294 |
{ |
295 |
val result = |
|
296 |
for { |
|
64651 | 297 |
(rendering, offset) <- rendering_offset(node_pos) |
64748 | 298 |
info <- rendering.tooltips(Text.Range(offset, offset + 1)) |
64641 | 299 |
} yield { |
64830 | 300 |
val doc = rendering.model.content.doc |
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
301 |
val range = doc.range(info.range) |
64748 | 302 |
val contents = |
64870 | 303 |
info.info.map(tree => resources.output_pretty(List(tree), rendering.tooltip_margin)) |
64748 | 304 |
(range, contents) |
64641 | 305 |
} |
306 |
channel.write(Protocol.Hover.reply(id, result)) |
|
307 |
} |
|
64605 | 308 |
|
309 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
310 |
/* goto definition */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
311 |
|
64651 | 312 |
def goto_definition(id: Protocol.Id, node_pos: Line.Node_Position) |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
313 |
{ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
314 |
val result = |
64651 | 315 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
64649 | 316 |
yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
317 |
channel.write(Protocol.GotoDefinition.reply(id, result)) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
318 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
319 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
320 |
|
64767 | 321 |
/* document highlights */ |
322 |
||
323 |
def document_highlights(id: Protocol.Id, node_pos: Line.Node_Position) |
|
324 |
{ |
|
325 |
val result = |
|
326 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
327 |
yield { |
|
64830 | 328 |
val doc = rendering.model.content.doc |
64821 | 329 |
rendering.caret_focus_ranges(Text.Range(offset, offset + 1), doc.text_range) |
64767 | 330 |
.map(r => Protocol.DocumentHighlight.text(doc.range(r))) |
331 |
}) getOrElse Nil |
|
332 |
channel.write(Protocol.DocumentHighlights.reply(id, result)) |
|
333 |
} |
|
334 |
||
335 |
||
64605 | 336 |
/* main loop */ |
337 |
||
338 |
def start() |
|
339 |
{ |
|
64717 | 340 |
log("Server started " + Date.now()) |
64605 | 341 |
|
342 |
def handle(json: JSON.T) |
|
343 |
{ |
|
344 |
try { |
|
345 |
json match { |
|
64641 | 346 |
case Protocol.Initialize(id) => init(id) |
347 |
case Protocol.Shutdown(id) => shutdown(id) |
|
64645 | 348 |
case Protocol.Exit(()) => exit() |
64801 | 349 |
case Protocol.DidOpenTextDocument(file, lang, version, text) => |
350 |
update_document(file, text) |
|
351 |
case Protocol.DidChangeTextDocument(file, version, List(Protocol.TextDocumentContent(text))) => |
|
352 |
update_document(file, text) |
|
353 |
case Protocol.DidCloseTextDocument(file) => |
|
354 |
close_document(file) |
|
64877 | 355 |
case Protocol.Completion(id, node_pos) => completion(id, node_pos) |
64651 | 356 |
case Protocol.Hover(id, node_pos) => hover(id, node_pos) |
357 |
case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos) |
|
64767 | 358 |
case Protocol.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos) |
64717 | 359 |
case _ => log("### IGNORED") |
64605 | 360 |
} |
361 |
} |
|
64687 | 362 |
catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) } |
64605 | 363 |
} |
364 |
||
365 |
@tailrec def loop() |
|
366 |
{ |
|
367 |
channel.read() match { |
|
368 |
case Some(json) => |
|
369 |
json match { |
|
370 |
case bulk: List[_] => bulk.foreach(handle(_)) |
|
371 |
case _ => handle(json) |
|
372 |
} |
|
373 |
loop() |
|
64717 | 374 |
case None => log("### TERMINATE") |
64605 | 375 |
} |
376 |
} |
|
377 |
loop() |
|
378 |
} |
|
379 |
} |