author | wenzelm |
Fri, 09 Jun 2017 17:13:50 +0200 | |
changeset 66052 | 39eb61b1fa51 |
parent 65983 | d8c5603c1732 |
child 66054 | fb0eea226a4d |
permissions | -rw-r--r-- |
64605 | 1 |
/* Title: Tools/VSCode/src/server.scala |
2 |
Author: Makarius |
|
3 |
||
65160 | 4 |
Server for VS Code Language Server Protocol 2.0/3.0, see also |
64605 | 5 |
https://github.com/Microsoft/language-server-protocol |
6 |
https://github.com/Microsoft/language-server-protocol/blob/master/protocol.md |
|
65160 | 7 |
|
8 |
PIDE protocol extensions depend on system option "vscode_pide_extensions". |
|
64605 | 9 |
*/ |
10 |
||
11 |
package isabelle.vscode |
|
12 |
||
13 |
||
14 |
import isabelle._ |
|
15 |
||
64710 | 16 |
import java.io.{PrintStream, OutputStream, File => JFile} |
64605 | 17 |
|
18 |
import scala.annotation.tailrec |
|
65234
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
19 |
import scala.collection.mutable |
64605 | 20 |
|
21 |
||
22 |
object Server |
|
23 |
{ |
|
24 |
/* Isabelle tool wrapper */ |
|
25 |
||
26 |
private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
|
27 |
||
28 |
val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args => |
|
29 |
{ |
|
64644 | 30 |
try { |
31 |
var log_file: Option[Path] = None |
|
32 |
var dirs: List[Path] = Nil |
|
33 |
var logic = default_logic |
|
34 |
var modes: List[String] = Nil |
|
35 |
var options = Options.init() |
|
64733 | 36 |
var system_mode = false |
65922 | 37 |
var verbose = false |
64605 | 38 |
|
64644 | 39 |
val getopts = Getopts(""" |
64605 | 40 |
Usage: isabelle vscode_server [OPTIONS] |
41 |
||
42 |
Options are: |
|
43 |
-L FILE enable logging on FILE |
|
44 |
-d DIR include session directory |
|
64737 | 45 |
-l NAME logic session name (default ISABELLE_LOGIC=""" + quote(default_logic) + """) |
64605 | 46 |
-m MODE add print mode for output |
47 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
64733 | 48 |
-s system build mode for session image |
65922 | 49 |
-v verbose logging |
64605 | 50 |
|
51 |
Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout. |
|
52 |
""", |
|
64755 | 53 |
"L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))), |
54 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(File.standard_path(arg)))), |
|
64644 | 55 |
"l:" -> (arg => logic = arg), |
56 |
"m:" -> (arg => modes = arg :: modes), |
|
64733 | 57 |
"o:" -> (arg => options = options + arg), |
65922 | 58 |
"s" -> (_ => system_mode = true), |
59 |
"v" -> (_ => verbose = true)) |
|
64605 | 60 |
|
64644 | 61 |
val more_args = getopts(args) |
62 |
if (more_args.nonEmpty) getopts.usage() |
|
63 |
||
64717 | 64 |
val log = Logger.make(log_file) |
65922 | 65 |
val channel = new Channel(System.in, System.out, log, verbose) |
65196
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
wenzelm
parents:
65191
diff
changeset
|
66 |
val server = new Server(channel, options, logic, dirs, modes, system_mode, log) |
64605 | 67 |
|
64644 | 68 |
// prevent spurious garbage on the main protocol channel |
69 |
val orig_out = System.out |
|
70 |
try { |
|
71 |
System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} })) |
|
72 |
server.start() |
|
73 |
} |
|
74 |
finally { System.setOut(orig_out) } |
|
64605 | 75 |
} |
64644 | 76 |
catch { |
77 |
case exn: Throwable => |
|
64717 | 78 |
val channel = new Channel(System.in, System.out, No_Logger) |
64644 | 79 |
channel.error_message(Exn.message(exn)) |
80 |
throw(exn) |
|
81 |
} |
|
64605 | 82 |
}) |
83 |
} |
|
84 |
||
85 |
class Server( |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
86 |
val channel: Channel, |
64605 | 87 |
options: Options, |
88 |
session_name: String = Server.default_logic, |
|
89 |
session_dirs: List[Path] = Nil, |
|
64717 | 90 |
modes: List[String] = Nil, |
64733 | 91 |
system_mode: Boolean = false, |
64717 | 92 |
log: Logger = No_Logger) |
64605 | 93 |
{ |
64725 | 94 |
/* prover session */ |
64605 | 95 |
|
64703 | 96 |
private val session_ = Synchronized(None: Option[Session]) |
97 |
def session: Session = session_.value getOrElse error("Server inactive") |
|
64623 | 98 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
64605 | 99 |
|
64651 | 100 |
def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] = |
64649 | 101 |
for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
102 |
model <- resources.get_model(new JFile(node_pos.name)) |
64704 | 103 |
rendering = model.rendering() |
64830 | 104 |
offset <- model.content.doc.offset(node_pos.pos) |
64649 | 105 |
} yield (rendering, offset) |
106 |
||
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
107 |
private val dynamic_output = Dynamic_Output(this) |
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
108 |
|
64605 | 109 |
|
64725 | 110 |
/* input from client or file-system */ |
111 |
||
64800 | 112 |
private val delay_input: Standard_Thread.Delay = |
64810 | 113 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger) |
64725 | 114 |
{ resources.flush_input(session) } |
115 |
||
64800 | 116 |
private val delay_load: Standard_Thread.Delay = |
64810 | 117 |
Standard_Thread.delay_last(options.seconds("vscode_load_delay"), channel.Error_Logger) { |
64857 | 118 |
val (invoke_input, invoke_load) = resources.resolve_dependencies(session, file_watcher) |
64800 | 119 |
if (invoke_input) delay_input.invoke() |
120 |
if (invoke_load) delay_load.invoke |
|
121 |
} |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
122 |
|
64857 | 123 |
private val file_watcher = |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
124 |
File_Watcher(sync_documents(_), options.seconds("vscode_load_delay")) |
64725 | 125 |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
126 |
private def close_document(file: JFile) |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
127 |
{ |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
128 |
if (resources.close_model(file)) { |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
129 |
file_watcher.register_parent(file) |
65116 | 130 |
sync_documents(Set(file)) |
131 |
delay_input.invoke() |
|
132 |
delay_output.invoke() |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
133 |
} |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
134 |
} |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
135 |
|
65116 | 136 |
private def sync_documents(changed: Set[JFile]) |
137 |
{ |
|
138 |
resources.sync_models(changed) |
|
139 |
delay_input.invoke() |
|
140 |
delay_output.invoke() |
|
141 |
} |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
142 |
|
65160 | 143 |
private def change_document(file: JFile, changes: List[Protocol.TextDocumentChange]) |
144 |
{ |
|
65234
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
145 |
val norm_changes = new mutable.ListBuffer[Protocol.TextDocumentChange] |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
146 |
@tailrec def norm(chs: List[Protocol.TextDocumentChange]) |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
147 |
{ |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
148 |
if (chs.nonEmpty) { |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
149 |
val (full_texts, rest1) = chs.span(_.range.isEmpty) |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
150 |
val (edits, rest2) = rest1.span(_.range.nonEmpty) |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
151 |
norm_changes ++= full_texts |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
152 |
norm_changes ++= edits.sortBy(_.range.get.start)(Line.Position.Ordering).reverse |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
153 |
norm(rest2) |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
154 |
} |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
155 |
} |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
156 |
norm(changes) |
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
157 |
norm_changes.foreach(change => |
65184
0f555ce33970
apply multiple edits bottom-to-top as specified in the protocol definition (assuming canonical order);
wenzelm
parents:
65162
diff
changeset
|
158 |
resources.change_model(session, file, change.text, change.range)) |
65234
1d6e9048cb62
normalize changes strictly as specified in the protocol definition (assuming non-overlapping ranges, amending 0f555ce33970), e.g. relevant for automatic quotes/parentheses around selection;
wenzelm
parents:
65232
diff
changeset
|
159 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
160 |
delay_input.invoke() |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
161 |
delay_output.invoke() |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
162 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
163 |
|
64710 | 164 |
|
65189 | 165 |
/* caret handling */ |
166 |
||
167 |
private val delay_caret_update: Standard_Thread.Delay = |
|
168 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger) |
|
169 |
{ session.caret_focus.post(Session.Caret_Focus) } |
|
170 |
||
171 |
private def update_caret(caret: Option[(JFile, Line.Position)]) |
|
172 |
{ |
|
173 |
resources.update_caret(caret) |
|
174 |
delay_caret_update.invoke() |
|
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65922
diff
changeset
|
175 |
delay_input.invoke() |
65189 | 176 |
} |
177 |
||
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
178 |
|
65983 | 179 |
/* preview */ |
180 |
||
181 |
private lazy val preview = new Preview(resources) |
|
182 |
||
183 |
private lazy val delay_preview: Standard_Thread.Delay = |
|
184 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger) |
|
185 |
{ |
|
186 |
if (preview.flush(channel)) delay_preview.invoke() |
|
187 |
} |
|
188 |
||
189 |
private def request_preview(file: JFile, column: Int) |
|
190 |
{ |
|
191 |
preview.request(file, column) |
|
192 |
delay_preview.invoke() |
|
193 |
} |
|
194 |
||
195 |
||
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
196 |
/* output to client */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
197 |
|
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
198 |
private val delay_output: Standard_Thread.Delay = |
64810 | 199 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger) |
64703 | 200 |
{ |
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65231
diff
changeset
|
201 |
if (resources.flush_output(channel)) delay_output.invoke() |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
202 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
203 |
|
64725 | 204 |
private val prover_output = |
205 |
Session.Consumer[Session.Commands_Changed](getClass.getName) { |
|
65119
a7bedf94e71c
always invoke output: pending_output may be present due to other reasons;
wenzelm
parents:
65116
diff
changeset
|
206 |
case changed => |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
207 |
resources.update_output(changed.nodes.toList.map(resources.node_file(_))) |
64725 | 208 |
delay_output.invoke() |
209 |
} |
|
64710 | 210 |
|
64725 | 211 |
private val syslog = |
64692 | 212 |
Session.Consumer[Prover.Message](getClass.getName) { |
213 |
case output: Prover.Output if output.is_syslog => |
|
64870 | 214 |
channel.log_writeln(resources.output_xml(output.message)) |
64692 | 215 |
case _ => |
216 |
} |
|
217 |
||
218 |
||
64605 | 219 |
/* init and exit */ |
220 |
||
64641 | 221 |
def init(id: Protocol.Id) |
64605 | 222 |
{ |
64641 | 223 |
def reply(err: String) |
224 |
{ |
|
225 |
channel.write(Protocol.Initialize.reply(id, err)) |
|
64642
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
226 |
if (err == "") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
227 |
channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
228 |
else channel.error_message(err) |
64641 | 229 |
} |
230 |
||
64643 | 231 |
val try_session = |
232 |
try { |
|
64733 | 233 |
if (!Build.build(options = options, build_heap = true, no_build = true, |
234 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
235 |
{ |
|
236 |
val start_msg = "Build started for Isabelle/" + session_name + " ..." |
|
237 |
val fail_msg = "Session build failed -- prover process remains inactive!" |
|
238 |
||
239 |
val progress = channel.make_progress(verbose = true) |
|
240 |
progress.echo(start_msg); channel.writeln(start_msg) |
|
241 |
||
242 |
if (!Build.build(options = options, progress = progress, build_heap = true, |
|
243 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
244 |
{ |
|
245 |
progress.echo(fail_msg); error(fail_msg) |
|
246 |
} |
|
247 |
} |
|
248 |
||
65512 | 249 |
val session_base = |
250 |
Sessions.session_base(options, session_name, dirs = session_dirs, all_known = true) |
|
65361 | 251 |
val resources = new VSCode_Resources(options, session_base, log) |
64854 | 252 |
{ |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
253 |
override def commit(change: Session.Change): Unit = |
64800 | 254 |
if (change.deps_changed || undefined_blobs(change.version.nodes).nonEmpty) |
255 |
delay_load.invoke() |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
256 |
} |
64605 | 257 |
|
65272
7611c55c39d0
always output proof state: there is only one output buffer in Isabelle/VSCode;
wenzelm
parents:
65264
diff
changeset
|
258 |
val session_options = options.bool("editor_output_state") = true |
7611c55c39d0
always output proof state: there is only one output buffer in Isabelle/VSCode;
wenzelm
parents:
65264
diff
changeset
|
259 |
Some(new Session(session_options, resources)) |
64605 | 260 |
} |
64643 | 261 |
catch { case ERROR(msg) => reply(msg); None } |
64605 | 262 |
|
64643 | 263 |
for (session <- try_session) { |
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
264 |
session_.change(_ => Some(session)) |
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
265 |
|
65227 | 266 |
session.commands_changed += prover_output |
267 |
session.all_messages += syslog |
|
268 |
||
269 |
dynamic_output.init() |
|
270 |
||
64643 | 271 |
var session_phase: Session.Consumer[Session.Phase] = null |
272 |
session_phase = |
|
273 |
Session.Consumer(getClass.getName) { |
|
274 |
case Session.Ready => |
|
275 |
session.phase_changed -= session_phase |
|
276 |
reply("") |
|
65317 | 277 |
case Session.Terminated(result) if !result.ok => |
64643 | 278 |
session.phase_changed -= session_phase |
65317 | 279 |
reply("Prover startup failed: return code " + result.rc) |
64643 | 280 |
case _ => |
281 |
} |
|
282 |
session.phase_changed += session_phase |
|
64605 | 283 |
|
65216 | 284 |
Isabelle_Process.start(session, options, |
285 |
logic = session_name, dirs = session_dirs, modes = modes) |
|
64643 | 286 |
} |
64605 | 287 |
} |
288 |
||
64641 | 289 |
def shutdown(id: Protocol.Id) |
64605 | 290 |
{ |
64641 | 291 |
def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err)) |
292 |
||
64703 | 293 |
session_.change({ |
294 |
case Some(session) => |
|
65228 | 295 |
session.commands_changed -= prover_output |
296 |
session.all_messages -= syslog |
|
297 |
||
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
298 |
dynamic_output.exit() |
65228 | 299 |
|
300 |
delay_load.revoke() |
|
301 |
file_watcher.shutdown() |
|
64703 | 302 |
delay_input.revoke() |
303 |
delay_output.revoke() |
|
65228 | 304 |
delay_caret_update.revoke() |
65983 | 305 |
delay_preview.revoke() |
65228 | 306 |
|
307 |
val rc = session.stop() |
|
65231 | 308 |
if (rc == 0) reply("") else reply("Prover shutdown failed: return code " + rc) |
64703 | 309 |
None |
310 |
case None => |
|
311 |
reply("Prover inactive") |
|
312 |
None |
|
313 |
}) |
|
64605 | 314 |
} |
315 |
||
64646 | 316 |
def exit() { |
64717 | 317 |
log("\n") |
64703 | 318 |
sys.exit(if (session_.value.isDefined) 1 else 0) |
64646 | 319 |
} |
64605 | 320 |
|
321 |
||
64877 | 322 |
/* completion */ |
323 |
||
324 |
def completion(id: Protocol.Id, node_pos: Line.Node_Position) |
|
325 |
{ |
|
326 |
val result = |
|
327 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
328 |
yield rendering.completion(Text.Range(offset - 1, offset))) getOrElse Nil |
|
329 |
channel.write(Protocol.Completion.reply(id, result)) |
|
330 |
} |
|
331 |
||
332 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
333 |
/* hover */ |
64605 | 334 |
|
64651 | 335 |
def hover(id: Protocol.Id, node_pos: Line.Node_Position) |
64641 | 336 |
{ |
337 |
val result = |
|
338 |
for { |
|
64651 | 339 |
(rendering, offset) <- rendering_offset(node_pos) |
65134
511bf19348d3
clarified messages (with improved scalability): legacy/error as diagnostics, writeln/information/warning/bad as tooltips (dynamic hover);
wenzelm
parents:
65129
diff
changeset
|
340 |
info <- rendering.tooltips(VSCode_Rendering.tooltip_elements, Text.Range(offset, offset + 1)) |
64641 | 341 |
} yield { |
65197 | 342 |
val range = rendering.model.content.doc.range(info.range) |
64748 | 343 |
val contents = |
65107 | 344 |
info.info.map(t => Protocol.MarkedString(resources.output_pretty_tooltip(List(t)))) |
64748 | 345 |
(range, contents) |
64641 | 346 |
} |
347 |
channel.write(Protocol.Hover.reply(id, result)) |
|
348 |
} |
|
64605 | 349 |
|
350 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
351 |
/* goto definition */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
352 |
|
64651 | 353 |
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
|
354 |
{ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
355 |
val result = |
64651 | 356 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
64649 | 357 |
yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
358 |
channel.write(Protocol.GotoDefinition.reply(id, result)) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
359 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
360 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
361 |
|
64767 | 362 |
/* document highlights */ |
363 |
||
364 |
def document_highlights(id: Protocol.Id, node_pos: Line.Node_Position) |
|
365 |
{ |
|
366 |
val result = |
|
367 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
368 |
yield { |
|
65197 | 369 |
val model = rendering.model |
370 |
rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range) |
|
371 |
.map(r => Protocol.DocumentHighlight.text(model.content.doc.range(r))) |
|
64767 | 372 |
}) getOrElse Nil |
373 |
channel.write(Protocol.DocumentHighlights.reply(id, result)) |
|
374 |
} |
|
375 |
||
376 |
||
64605 | 377 |
/* main loop */ |
378 |
||
379 |
def start() |
|
380 |
{ |
|
64717 | 381 |
log("Server started " + Date.now()) |
64605 | 382 |
|
383 |
def handle(json: JSON.T) |
|
384 |
{ |
|
385 |
try { |
|
386 |
json match { |
|
64641 | 387 |
case Protocol.Initialize(id) => init(id) |
65229 | 388 |
case Protocol.Initialized(()) => |
64641 | 389 |
case Protocol.Shutdown(id) => shutdown(id) |
64645 | 390 |
case Protocol.Exit(()) => exit() |
65162 | 391 |
case Protocol.DidOpenTextDocument(file, _, _, text) => |
392 |
change_document(file, List(Protocol.TextDocumentChange(None, text))) |
|
65160 | 393 |
case Protocol.DidChangeTextDocument(file, _, changes) => change_document(file, changes) |
394 |
case Protocol.DidCloseTextDocument(file) => close_document(file) |
|
64877 | 395 |
case Protocol.Completion(id, node_pos) => completion(id, node_pos) |
64651 | 396 |
case Protocol.Hover(id, node_pos) => hover(id, node_pos) |
397 |
case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos) |
|
64767 | 398 |
case Protocol.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos) |
65189 | 399 |
case Protocol.Caret_Update(caret) => update_caret(caret) |
65983 | 400 |
case Protocol.Preview_Request(file, column) => request_preview(file, column) |
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
401 |
case Protocol.Symbols_Request(()) => channel.write(Protocol.Symbols()) |
64717 | 402 |
case _ => log("### IGNORED") |
64605 | 403 |
} |
404 |
} |
|
64687 | 405 |
catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) } |
64605 | 406 |
} |
407 |
||
408 |
@tailrec def loop() |
|
409 |
{ |
|
410 |
channel.read() match { |
|
411 |
case Some(json) => |
|
412 |
json match { |
|
413 |
case bulk: List[_] => bulk.foreach(handle(_)) |
|
414 |
case _ => handle(json) |
|
415 |
} |
|
416 |
loop() |
|
64717 | 417 |
case None => log("### TERMINATE") |
64605 | 418 |
} |
419 |
} |
|
420 |
loop() |
|
421 |
} |
|
422 |
} |