author | wenzelm |
Thu, 29 Dec 2016 22:10:29 +0100 | |
changeset 64704 | 08c2d80428ff |
parent 64703 | a115391494ed |
child 64706 | 3ebf9f8299df |
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 |
||
14 |
import java.io.{PrintStream, OutputStream} |
|
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() |
|
64605 | 35 |
|
64644 | 36 |
def text_length_choice: String = |
64682 | 37 |
commas(Text.Length.encodings.map( |
64644 | 38 |
{ case (a, _) => if (a == default_text_length) a + " (default)" else a })) |
64618 | 39 |
|
64644 | 40 |
val getopts = Getopts(""" |
64605 | 41 |
Usage: isabelle vscode_server [OPTIONS] |
42 |
||
43 |
Options are: |
|
44 |
-L FILE enable logging on FILE |
|
64618 | 45 |
-T LENGTH text length encoding: """ + text_length_choice + """ |
64605 | 46 |
-d DIR include session directory |
47 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(logic) + """) |
|
48 |
-m MODE add print mode for output |
|
49 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
50 |
||
51 |
Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout. |
|
52 |
""", |
|
64644 | 53 |
"L:" -> (arg => log_file = Some(Path.explode(arg))), |
64682 | 54 |
"T:" -> (arg => Text.Length.encoding(arg)), |
64644 | 55 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
56 |
"l:" -> (arg => logic = arg), |
|
57 |
"m:" -> (arg => modes = arg :: modes), |
|
58 |
"o:" -> (arg => options = options + arg)) |
|
64605 | 59 |
|
64644 | 60 |
val more_args = getopts(args) |
61 |
if (more_args.nonEmpty) getopts.usage() |
|
62 |
||
63 |
if (!Build.build(options = options, build_heap = true, no_build = true, |
|
64 |
dirs = dirs, sessions = List(logic)).ok) |
|
65 |
error("Missing logic image " + quote(logic)) |
|
64605 | 66 |
|
64644 | 67 |
val channel = new Channel(System.in, System.out, log_file) |
68 |
val server = new Server(channel, options, text_length, logic, dirs, modes) |
|
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 => |
|
80 |
val channel = new Channel(System.in, System.out, None) |
|
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, |
|
93 |
modes: List[String] = Nil) |
|
94 |
{ |
|
64703 | 95 |
/* server session */ |
64605 | 96 |
|
64703 | 97 |
private val session_ = Synchronized(None: Option[Session]) |
98 |
def session: Session = session_.value getOrElse error("Server inactive") |
|
64623 | 99 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
64605 | 100 |
|
64651 | 101 |
def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] = |
64649 | 102 |
for { |
64703 | 103 |
model <- resources.get_model(node_pos.name) |
64704 | 104 |
rendering = model.rendering() |
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
105 |
offset <- model.doc.offset(node_pos.pos) |
64649 | 106 |
} yield (rendering, offset) |
107 |
||
64605 | 108 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
109 |
/* input from client */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
110 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
111 |
private val delay_input = |
64703 | 112 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay")) |
113 |
{ resources.flush_input(session) } |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
114 |
|
64703 | 115 |
private def update_document(uri: String, text: String) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
116 |
{ |
64703 | 117 |
val model = Document_Model(session, resources.node_name(uri), Line.Document(text, text_length)) |
118 |
resources.update_model(model) |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
119 |
delay_input.invoke() |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
120 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
121 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
122 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
123 |
/* output to client */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
124 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
125 |
private val commands_changed = |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
126 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
64703 | 127 |
case changed if changed.nodes.nonEmpty => |
128 |
resources.update_output(changed.nodes) |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
129 |
delay_output.invoke() |
64703 | 130 |
case _ => |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
131 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
132 |
|
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
133 |
private val delay_output: Standard_Thread.Delay = |
64703 | 134 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay")) |
135 |
{ |
|
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
136 |
if (session.current_state().stable_tip_version.isEmpty) delay_output.invoke() |
64703 | 137 |
else resources.flush_output(channel) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
138 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
139 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
140 |
|
64692 | 141 |
/* syslog */ |
142 |
||
143 |
private val all_messages = |
|
144 |
Session.Consumer[Prover.Message](getClass.getName) { |
|
145 |
case output: Prover.Output if output.is_syslog => |
|
146 |
channel.log_writeln(XML.content(output.message)) |
|
147 |
case _ => |
|
148 |
} |
|
149 |
||
150 |
||
64605 | 151 |
/* init and exit */ |
152 |
||
64641 | 153 |
def init(id: Protocol.Id) |
64605 | 154 |
{ |
64641 | 155 |
def reply(err: String) |
156 |
{ |
|
157 |
channel.write(Protocol.Initialize.reply(id, err)) |
|
64642
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
158 |
if (err == "") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
159 |
channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
160 |
else channel.error_message(err) |
64641 | 161 |
} |
162 |
||
64643 | 163 |
val try_session = |
164 |
try { |
|
165 |
val content = Build.session_content(options, false, session_dirs, session_name) |
|
166 |
val resources = |
|
64703 | 167 |
new VSCode_Resources( |
168 |
options, content.loaded_theories, content.known_theories, content.syntax) |
|
64605 | 169 |
|
64643 | 170 |
Some(new Session(resources) { |
171 |
override def output_delay = options.seconds("editor_output_delay") |
|
172 |
override def prune_delay = options.seconds("editor_prune_delay") |
|
173 |
override def syslog_limit = options.int("editor_syslog_limit") |
|
174 |
override def reparse_limit = options.int("editor_reparse_limit") |
|
175 |
}) |
|
64605 | 176 |
} |
64643 | 177 |
catch { case ERROR(msg) => reply(msg); None } |
64605 | 178 |
|
64643 | 179 |
for (session <- try_session) { |
180 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
181 |
session_phase = |
|
182 |
Session.Consumer(getClass.getName) { |
|
183 |
case Session.Ready => |
|
184 |
session.phase_changed -= session_phase |
|
185 |
session.update_options(options) |
|
186 |
reply("") |
|
187 |
case Session.Failed => |
|
188 |
session.phase_changed -= session_phase |
|
189 |
reply("Prover startup failed") |
|
190 |
case _ => |
|
191 |
} |
|
192 |
session.phase_changed += session_phase |
|
64605 | 193 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
194 |
session.commands_changed += commands_changed |
64692 | 195 |
session.all_messages += all_messages |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
196 |
|
64643 | 197 |
session.start(receiver => |
198 |
Isabelle_Process(options = options, logic = session_name, dirs = session_dirs, |
|
199 |
modes = modes, receiver = receiver)) |
|
64605 | 200 |
|
64703 | 201 |
session_.change(_ => Some(session)) |
64643 | 202 |
} |
64605 | 203 |
} |
204 |
||
64641 | 205 |
def shutdown(id: Protocol.Id) |
64605 | 206 |
{ |
64641 | 207 |
def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err)) |
208 |
||
64703 | 209 |
session_.change({ |
210 |
case Some(session) => |
|
211 |
var session_phase: Session.Consumer[Session.Phase] = null |
|
212 |
session_phase = |
|
213 |
Session.Consumer(getClass.getName) { |
|
214 |
case Session.Inactive => |
|
215 |
session.phase_changed -= session_phase |
|
216 |
session.commands_changed -= commands_changed |
|
217 |
session.all_messages -= all_messages |
|
218 |
reply("") |
|
219 |
case _ => |
|
220 |
} |
|
221 |
session.phase_changed += session_phase |
|
222 |
session.stop() |
|
223 |
delay_input.revoke() |
|
224 |
delay_output.revoke() |
|
225 |
None |
|
226 |
case None => |
|
227 |
reply("Prover inactive") |
|
228 |
None |
|
229 |
}) |
|
64605 | 230 |
} |
231 |
||
64646 | 232 |
def exit() { |
233 |
channel.log("\n") |
|
64703 | 234 |
sys.exit(if (session_.value.isDefined) 1 else 0) |
64646 | 235 |
} |
64605 | 236 |
|
237 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
238 |
/* hover */ |
64605 | 239 |
|
64651 | 240 |
def hover(id: Protocol.Id, node_pos: Line.Node_Position) |
64641 | 241 |
{ |
242 |
val result = |
|
243 |
for { |
|
64651 | 244 |
(rendering, offset) <- rendering_offset(node_pos) |
64641 | 245 |
info <- rendering.tooltip(Text.Range(offset, offset + 1)) |
246 |
} yield { |
|
64649 | 247 |
val doc = rendering.model.doc |
64683
c0c09b6dfbe0
clarified signature: maintan Text.Length within Line.Document;
wenzelm
parents:
64682
diff
changeset
|
248 |
val range = doc.range(info.range) |
64641 | 249 |
val s = Pretty.string_of(info.info, margin = rendering.tooltip_margin) |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
250 |
(range, List("```\n" + s + "\n```")) // FIXME proper content format |
64641 | 251 |
} |
252 |
channel.write(Protocol.Hover.reply(id, result)) |
|
253 |
} |
|
64605 | 254 |
|
255 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
256 |
/* goto definition */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
257 |
|
64651 | 258 |
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
|
259 |
{ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
260 |
val result = |
64651 | 261 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
64649 | 262 |
yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
263 |
channel.write(Protocol.GotoDefinition.reply(id, result)) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
264 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
265 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
266 |
|
64605 | 267 |
/* main loop */ |
268 |
||
269 |
def start() |
|
270 |
{ |
|
64646 | 271 |
channel.log("Server started " + Date.now()) |
64605 | 272 |
|
273 |
def handle(json: JSON.T) |
|
274 |
{ |
|
275 |
try { |
|
276 |
json match { |
|
64641 | 277 |
case Protocol.Initialize(id) => init(id) |
278 |
case Protocol.Shutdown(id) => shutdown(id) |
|
64645 | 279 |
case Protocol.Exit(()) => exit() |
64605 | 280 |
case Protocol.DidOpenTextDocument(uri, lang, version, text) => |
281 |
update_document(uri, text) |
|
282 |
case Protocol.DidChangeTextDocument(uri, version, List(Protocol.TextDocumentContent(text))) => |
|
283 |
update_document(uri, text) |
|
284 |
case Protocol.DidCloseTextDocument(uri) => channel.log("CLOSE " + uri) |
|
285 |
case Protocol.DidSaveTextDocument(uri) => channel.log("SAVE " + uri) |
|
64651 | 286 |
case Protocol.Hover(id, node_pos) => hover(id, node_pos) |
287 |
case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos) |
|
64605 | 288 |
case _ => channel.log("### IGNORED") |
289 |
} |
|
290 |
} |
|
64687 | 291 |
catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) } |
64605 | 292 |
} |
293 |
||
294 |
@tailrec def loop() |
|
295 |
{ |
|
296 |
channel.read() match { |
|
297 |
case Some(json) => |
|
298 |
json match { |
|
299 |
case bulk: List[_] => bulk.foreach(handle(_)) |
|
300 |
case _ => handle(json) |
|
301 |
} |
|
302 |
loop() |
|
303 |
case None => channel.log("### TERMINATE") |
|
304 |
} |
|
305 |
} |
|
306 |
loop() |
|
307 |
} |
|
308 |
} |