author | wenzelm |
Thu, 29 Jun 2017 15:12:40 +0200 | |
changeset 66216 | d4949bae0bad |
parent 66215 | 9a8b6b86350c |
child 66241 | 8f39d60b943d |
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 |
{ |
|
66100 | 24 |
type Editor = isabelle.Editor[Unit] |
25 |
||
26 |
||
64605 | 27 |
/* Isabelle tool wrapper */ |
28 |
||
29 |
private lazy val default_logic = Isabelle_System.getenv("ISABELLE_LOGIC") |
|
30 |
||
31 |
val isabelle_tool = Isabelle_Tool("vscode_server", "VSCode Language Server for PIDE", args => |
|
32 |
{ |
|
64644 | 33 |
try { |
34 |
var log_file: Option[Path] = None |
|
35 |
var dirs: List[Path] = Nil |
|
36 |
var logic = default_logic |
|
37 |
var modes: List[String] = Nil |
|
38 |
var options = Options.init() |
|
64733 | 39 |
var system_mode = false |
65922 | 40 |
var verbose = false |
64605 | 41 |
|
64644 | 42 |
val getopts = Getopts(""" |
64605 | 43 |
Usage: isabelle vscode_server [OPTIONS] |
44 |
||
45 |
Options are: |
|
46 |
-L FILE enable logging on FILE |
|
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 |
65922 | 52 |
-v verbose logging |
64605 | 53 |
|
54 |
Run the VSCode Language Server protocol (JSON RPC) over stdin/stdout. |
|
55 |
""", |
|
64755 | 56 |
"L:" -> (arg => log_file = Some(Path.explode(File.standard_path(arg)))), |
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), |
65922 | 61 |
"s" -> (_ => system_mode = true), |
62 |
"v" -> (_ => verbose = true)) |
|
64605 | 63 |
|
64644 | 64 |
val more_args = getopts(args) |
65 |
if (more_args.nonEmpty) getopts.usage() |
|
66 |
||
64717 | 67 |
val log = Logger.make(log_file) |
65922 | 68 |
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
|
69 |
val server = new Server(channel, options, logic, dirs, modes, system_mode, log) |
64605 | 70 |
|
64644 | 71 |
// prevent spurious garbage on the main protocol channel |
72 |
val orig_out = System.out |
|
73 |
try { |
|
74 |
System.setOut(new PrintStream(new OutputStream { def write(n: Int) {} })) |
|
75 |
server.start() |
|
76 |
} |
|
77 |
finally { System.setOut(orig_out) } |
|
64605 | 78 |
} |
64644 | 79 |
catch { |
80 |
case exn: Throwable => |
|
64717 | 81 |
val channel = new Channel(System.in, System.out, No_Logger) |
64644 | 82 |
channel.error_message(Exn.message(exn)) |
83 |
throw(exn) |
|
84 |
} |
|
64605 | 85 |
}) |
86 |
} |
|
87 |
||
88 |
class Server( |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
89 |
val channel: Channel, |
64605 | 90 |
options: Options, |
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 |
{ |
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
97 |
server => |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
98 |
|
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
99 |
|
64725 | 100 |
/* prover session */ |
64605 | 101 |
|
64703 | 102 |
private val session_ = Synchronized(None: Option[Session]) |
103 |
def session: Session = session_.value getOrElse error("Server inactive") |
|
64623 | 104 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
64605 | 105 |
|
64651 | 106 |
def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] = |
64649 | 107 |
for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64767
diff
changeset
|
108 |
model <- resources.get_model(new JFile(node_pos.name)) |
64704 | 109 |
rendering = model.rendering() |
64830 | 110 |
offset <- model.content.doc.offset(node_pos.pos) |
64649 | 111 |
} yield (rendering, offset) |
112 |
||
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
113 |
private val dynamic_output = Dynamic_Output(server) |
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
114 |
|
64605 | 115 |
|
64725 | 116 |
/* input from client or file-system */ |
117 |
||
64800 | 118 |
private val delay_input: Standard_Thread.Delay = |
64810 | 119 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger) |
64725 | 120 |
{ resources.flush_input(session) } |
121 |
||
64800 | 122 |
private val delay_load: Standard_Thread.Delay = |
64810 | 123 |
Standard_Thread.delay_last(options.seconds("vscode_load_delay"), channel.Error_Logger) { |
66100 | 124 |
val (invoke_input, invoke_load) = |
125 |
resources.resolve_dependencies(session, editor, file_watcher) |
|
64800 | 126 |
if (invoke_input) delay_input.invoke() |
127 |
if (invoke_load) delay_load.invoke |
|
128 |
} |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
129 |
|
64857 | 130 |
private val file_watcher = |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
131 |
File_Watcher(sync_documents(_), options.seconds("vscode_load_delay")) |
64725 | 132 |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
133 |
private def close_document(file: JFile) |
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 |
if (resources.close_model(file)) { |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
136 |
file_watcher.register_parent(file) |
65116 | 137 |
sync_documents(Set(file)) |
138 |
delay_input.invoke() |
|
139 |
delay_output.invoke() |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
140 |
} |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
141 |
} |
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
142 |
|
65116 | 143 |
private def sync_documents(changed: Set[JFile]) |
144 |
{ |
|
145 |
resources.sync_models(changed) |
|
146 |
delay_input.invoke() |
|
147 |
delay_output.invoke() |
|
148 |
} |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
149 |
|
65160 | 150 |
private def change_document(file: JFile, changes: List[Protocol.TextDocumentChange]) |
151 |
{ |
|
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
|
152 |
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
|
153 |
@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
|
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 |
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
|
156 |
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
|
157 |
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
|
158 |
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
|
159 |
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
|
160 |
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
|
161 |
} |
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
|
162 |
} |
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
|
163 |
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
|
164 |
norm_changes.foreach(change => |
66100 | 165 |
resources.change_model(session, editor, 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
|
166 |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
167 |
delay_input.invoke() |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
168 |
delay_output.invoke() |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
169 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
170 |
|
64710 | 171 |
|
65189 | 172 |
/* caret handling */ |
173 |
||
174 |
private val delay_caret_update: Standard_Thread.Delay = |
|
175 |
Standard_Thread.delay_last(options.seconds("vscode_input_delay"), channel.Error_Logger) |
|
176 |
{ session.caret_focus.post(Session.Caret_Focus) } |
|
177 |
||
178 |
private def update_caret(caret: Option[(JFile, Line.Position)]) |
|
179 |
{ |
|
180 |
resources.update_caret(caret) |
|
181 |
delay_caret_update.invoke() |
|
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65922
diff
changeset
|
182 |
delay_input.invoke() |
65189 | 183 |
} |
184 |
||
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
185 |
|
65983 | 186 |
/* preview */ |
187 |
||
66098 | 188 |
private lazy val preview_panel = new Preview_Panel(resources) |
65983 | 189 |
|
190 |
private lazy val delay_preview: Standard_Thread.Delay = |
|
191 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger) |
|
192 |
{ |
|
66098 | 193 |
if (preview_panel.flush(channel)) delay_preview.invoke() |
65983 | 194 |
} |
195 |
||
196 |
private def request_preview(file: JFile, column: Int) |
|
197 |
{ |
|
66098 | 198 |
preview_panel.request(file, column) |
65983 | 199 |
delay_preview.invoke() |
200 |
} |
|
201 |
||
202 |
||
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
203 |
/* output to client */ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
204 |
|
64691
db2b21a52f20
prefer stable state -- reduce repeated diagnostics;
wenzelm
parents:
64690
diff
changeset
|
205 |
private val delay_output: Standard_Thread.Delay = |
64810 | 206 |
Standard_Thread.delay_last(options.seconds("vscode_output_delay"), channel.Error_Logger) |
64703 | 207 |
{ |
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65231
diff
changeset
|
208 |
if (resources.flush_output(channel)) delay_output.invoke() |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
209 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
210 |
|
66138 | 211 |
def update_output(changed_nodes: Traversable[JFile]) |
212 |
{ |
|
213 |
resources.update_output(changed_nodes) |
|
214 |
delay_output.invoke() |
|
215 |
} |
|
216 |
||
217 |
def update_output_visible() |
|
218 |
{ |
|
219 |
resources.update_output_visible() |
|
220 |
delay_output.invoke() |
|
221 |
} |
|
222 |
||
64725 | 223 |
private val prover_output = |
224 |
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
|
225 |
case changed => |
66138 | 226 |
update_output(changed.nodes.toList.map(resources.node_file(_))) |
64725 | 227 |
} |
64710 | 228 |
|
64725 | 229 |
private val syslog = |
64692 | 230 |
Session.Consumer[Prover.Message](getClass.getName) { |
231 |
case output: Prover.Output if output.is_syslog => |
|
64870 | 232 |
channel.log_writeln(resources.output_xml(output.message)) |
64692 | 233 |
case _ => |
234 |
} |
|
235 |
||
236 |
||
64605 | 237 |
/* init and exit */ |
238 |
||
64641 | 239 |
def init(id: Protocol.Id) |
64605 | 240 |
{ |
64641 | 241 |
def reply(err: String) |
242 |
{ |
|
243 |
channel.write(Protocol.Initialize.reply(id, err)) |
|
64642
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
244 |
if (err == "") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
245 |
channel.writeln("Welcome to Isabelle/" + session_name + " (" + Distribution.version + ")") |
c231206a84c8
display messages, according to regular Isabelle Output;
wenzelm
parents:
64641
diff
changeset
|
246 |
else channel.error_message(err) |
64641 | 247 |
} |
248 |
||
64643 | 249 |
val try_session = |
250 |
try { |
|
64733 | 251 |
if (!Build.build(options = options, build_heap = true, no_build = true, |
252 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
253 |
{ |
|
254 |
val start_msg = "Build started for Isabelle/" + session_name + " ..." |
|
255 |
val fail_msg = "Session build failed -- prover process remains inactive!" |
|
256 |
||
257 |
val progress = channel.make_progress(verbose = true) |
|
258 |
progress.echo(start_msg); channel.writeln(start_msg) |
|
259 |
||
260 |
if (!Build.build(options = options, progress = progress, build_heap = true, |
|
261 |
system_mode = system_mode, dirs = session_dirs, sessions = List(session_name)).ok) |
|
262 |
{ |
|
263 |
progress.echo(fail_msg); error(fail_msg) |
|
264 |
} |
|
265 |
} |
|
266 |
||
65512 | 267 |
val session_base = |
268 |
Sessions.session_base(options, session_name, dirs = session_dirs, all_known = true) |
|
65361 | 269 |
val resources = new VSCode_Resources(options, session_base, log) |
64854 | 270 |
{ |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
271 |
override def commit(change: Session.Change): Unit = |
64800 | 272 |
if (change.deps_changed || undefined_blobs(change.version.nodes).nonEmpty) |
273 |
delay_load.invoke() |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
274 |
} |
64605 | 275 |
|
65272
7611c55c39d0
always output proof state: there is only one output buffer in Isabelle/VSCode;
wenzelm
parents:
65264
diff
changeset
|
276 |
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
|
277 |
Some(new Session(session_options, resources)) |
64605 | 278 |
} |
64643 | 279 |
catch { case ERROR(msg) => reply(msg); None } |
64605 | 280 |
|
64643 | 281 |
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
|
282 |
session_.change(_ => Some(session)) |
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
283 |
|
65227 | 284 |
session.commands_changed += prover_output |
285 |
session.all_messages += syslog |
|
286 |
||
287 |
dynamic_output.init() |
|
288 |
||
64643 | 289 |
var session_phase: Session.Consumer[Session.Phase] = null |
290 |
session_phase = |
|
291 |
Session.Consumer(getClass.getName) { |
|
292 |
case Session.Ready => |
|
293 |
session.phase_changed -= session_phase |
|
294 |
reply("") |
|
65317 | 295 |
case Session.Terminated(result) if !result.ok => |
64643 | 296 |
session.phase_changed -= session_phase |
65317 | 297 |
reply("Prover startup failed: return code " + result.rc) |
64643 | 298 |
case _ => |
299 |
} |
|
300 |
session.phase_changed += session_phase |
|
64605 | 301 |
|
65216 | 302 |
Isabelle_Process.start(session, options, |
303 |
logic = session_name, dirs = session_dirs, modes = modes) |
|
64643 | 304 |
} |
64605 | 305 |
} |
306 |
||
64641 | 307 |
def shutdown(id: Protocol.Id) |
64605 | 308 |
{ |
64641 | 309 |
def reply(err: String): Unit = channel.write(Protocol.Shutdown.reply(id, err)) |
310 |
||
64703 | 311 |
session_.change({ |
312 |
case Some(session) => |
|
65228 | 313 |
session.commands_changed -= prover_output |
314 |
session.all_messages -= syslog |
|
315 |
||
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
316 |
dynamic_output.exit() |
65228 | 317 |
|
318 |
delay_load.revoke() |
|
319 |
file_watcher.shutdown() |
|
64703 | 320 |
delay_input.revoke() |
321 |
delay_output.revoke() |
|
65228 | 322 |
delay_caret_update.revoke() |
65983 | 323 |
delay_preview.revoke() |
65228 | 324 |
|
325 |
val rc = session.stop() |
|
65231 | 326 |
if (rc == 0) reply("") else reply("Prover shutdown failed: return code " + rc) |
64703 | 327 |
None |
328 |
case None => |
|
329 |
reply("Prover inactive") |
|
330 |
None |
|
331 |
}) |
|
64605 | 332 |
} |
333 |
||
64646 | 334 |
def exit() { |
64717 | 335 |
log("\n") |
64703 | 336 |
sys.exit(if (session_.value.isDefined) 1 else 0) |
64646 | 337 |
} |
64605 | 338 |
|
339 |
||
64877 | 340 |
/* completion */ |
341 |
||
342 |
def completion(id: Protocol.Id, node_pos: Line.Node_Position) |
|
343 |
{ |
|
344 |
val result = |
|
345 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
66052
diff
changeset
|
346 |
yield rendering.completion(node_pos.pos, offset)) getOrElse Nil |
64877 | 347 |
channel.write(Protocol.Completion.reply(id, result)) |
348 |
} |
|
349 |
||
350 |
||
66138 | 351 |
/* spell-checker dictionary */ |
352 |
||
353 |
def update_dictionary(include: Boolean, permanent: Boolean) |
|
354 |
{ |
|
355 |
for { |
|
356 |
spell_checker <- resources.spell_checker.get |
|
357 |
caret <- resources.get_caret() |
|
358 |
rendering = caret.model.rendering() |
|
359 |
range = rendering.before_caret_range(caret.offset) |
|
360 |
Text.Info(_, word) <- Spell_Checker.current_word(rendering, range) |
|
361 |
} { |
|
362 |
spell_checker.update(word, include, permanent) |
|
363 |
update_output_visible() |
|
364 |
} |
|
365 |
} |
|
366 |
||
367 |
def reset_dictionary() |
|
368 |
{ |
|
369 |
for (spell_checker <- resources.spell_checker.get) |
|
370 |
{ |
|
371 |
spell_checker.reset() |
|
372 |
update_output_visible() |
|
373 |
} |
|
374 |
} |
|
375 |
||
376 |
||
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
377 |
/* hover */ |
64605 | 378 |
|
64651 | 379 |
def hover(id: Protocol.Id, node_pos: Line.Node_Position) |
64641 | 380 |
{ |
381 |
val result = |
|
382 |
for { |
|
64651 | 383 |
(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
|
384 |
info <- rendering.tooltips(VSCode_Rendering.tooltip_elements, Text.Range(offset, offset + 1)) |
64641 | 385 |
} yield { |
65197 | 386 |
val range = rendering.model.content.doc.range(info.range) |
64748 | 387 |
val contents = |
65107 | 388 |
info.info.map(t => Protocol.MarkedString(resources.output_pretty_tooltip(List(t)))) |
64748 | 389 |
(range, contents) |
64641 | 390 |
} |
391 |
channel.write(Protocol.Hover.reply(id, result)) |
|
392 |
} |
|
64605 | 393 |
|
394 |
||
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
395 |
/* goto definition */ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
396 |
|
64651 | 397 |
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
|
398 |
{ |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
399 |
val result = |
64651 | 400 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
64649 | 401 |
yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil |
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
402 |
channel.write(Protocol.GotoDefinition.reply(id, result)) |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
403 |
} |
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
404 |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
405 |
|
64767 | 406 |
/* document highlights */ |
407 |
||
408 |
def document_highlights(id: Protocol.Id, node_pos: Line.Node_Position) |
|
409 |
{ |
|
410 |
val result = |
|
411 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
412 |
yield { |
|
65197 | 413 |
val model = rendering.model |
414 |
rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range) |
|
415 |
.map(r => Protocol.DocumentHighlight.text(model.content.doc.range(r))) |
|
64767 | 416 |
}) getOrElse Nil |
417 |
channel.write(Protocol.DocumentHighlights.reply(id, result)) |
|
418 |
} |
|
419 |
||
420 |
||
64605 | 421 |
/* main loop */ |
422 |
||
423 |
def start() |
|
424 |
{ |
|
64717 | 425 |
log("Server started " + Date.now()) |
64605 | 426 |
|
427 |
def handle(json: JSON.T) |
|
428 |
{ |
|
429 |
try { |
|
430 |
json match { |
|
64641 | 431 |
case Protocol.Initialize(id) => init(id) |
65229 | 432 |
case Protocol.Initialized(()) => |
64641 | 433 |
case Protocol.Shutdown(id) => shutdown(id) |
64645 | 434 |
case Protocol.Exit(()) => exit() |
65162 | 435 |
case Protocol.DidOpenTextDocument(file, _, _, text) => |
436 |
change_document(file, List(Protocol.TextDocumentChange(None, text))) |
|
65160 | 437 |
case Protocol.DidChangeTextDocument(file, _, changes) => change_document(file, changes) |
438 |
case Protocol.DidCloseTextDocument(file) => close_document(file) |
|
64877 | 439 |
case Protocol.Completion(id, node_pos) => completion(id, node_pos) |
66138 | 440 |
case Protocol.Include_Word(()) => update_dictionary(true, false) |
441 |
case Protocol.Include_Word_Permanently(()) => update_dictionary(true, true) |
|
442 |
case Protocol.Exclude_Word(()) => update_dictionary(false, false) |
|
443 |
case Protocol.Exclude_Word_Permanently(()) => update_dictionary(false, true) |
|
444 |
case Protocol.Reset_Words(()) => reset_dictionary() |
|
64651 | 445 |
case Protocol.Hover(id, node_pos) => hover(id, node_pos) |
446 |
case Protocol.GotoDefinition(id, node_pos) => goto_definition(id, node_pos) |
|
64767 | 447 |
case Protocol.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos) |
65189 | 448 |
case Protocol.Caret_Update(caret) => update_caret(caret) |
66098 | 449 |
case Protocol.State_Init(()) => State_Panel.init(server) |
450 |
case Protocol.State_Exit(id) => State_Panel.exit(id) |
|
451 |
case Protocol.State_Locate(id) => State_Panel.locate(id) |
|
452 |
case Protocol.State_Update(id) => State_Panel.update(id) |
|
66211 | 453 |
case Protocol.State_Auto_Update(id, enabled) => State_Panel.auto_update(id, enabled) |
65983 | 454 |
case Protocol.Preview_Request(file, column) => request_preview(file, column) |
66052
39eb61b1fa51
provide information about Isabelle symbols within VSCode;
wenzelm
parents:
65983
diff
changeset
|
455 |
case Protocol.Symbols_Request(()) => channel.write(Protocol.Symbols()) |
64717 | 456 |
case _ => log("### IGNORED") |
64605 | 457 |
} |
458 |
} |
|
64687 | 459 |
catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) } |
64605 | 460 |
} |
461 |
||
462 |
@tailrec def loop() |
|
463 |
{ |
|
464 |
channel.read() match { |
|
465 |
case Some(json) => |
|
466 |
json match { |
|
467 |
case bulk: List[_] => bulk.foreach(handle(_)) |
|
468 |
case _ => handle(json) |
|
469 |
} |
|
470 |
loop() |
|
64717 | 471 |
case None => log("### TERMINATE") |
64605 | 472 |
} |
473 |
} |
|
474 |
loop() |
|
475 |
} |
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
476 |
|
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
477 |
|
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
478 |
/* abstract editor operations */ |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
479 |
|
66100 | 480 |
object editor extends Server.Editor |
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
481 |
{ |
66101
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
482 |
/* session */ |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
483 |
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
484 |
override def session: Session = server.session |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
485 |
override def flush(): Unit = resources.flush_input(session) |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
486 |
override def invoke(): Unit = delay_input.invoke() |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
487 |
|
66101
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
488 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
489 |
/* current situation */ |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
490 |
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
491 |
override def current_node(context: Unit): Option[Document.Node.Name] = |
66086 | 492 |
resources.get_caret().map(_.model.node_name) |
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
493 |
override def current_node_snapshot(context: Unit): Option[Document.Snapshot] = |
66086 | 494 |
resources.get_caret().map(_.model.snapshot()) |
495 |
||
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
496 |
override def node_snapshot(name: Document.Node.Name): Document.Snapshot = |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
497 |
{ |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
498 |
resources.get_model(name) match { |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
499 |
case Some(model) => model.snapshot() |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
500 |
case None => session.snapshot(name) |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
501 |
} |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
502 |
} |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
503 |
|
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
504 |
def current_command(snapshot: Document.Snapshot): Option[Command] = |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
505 |
{ |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
506 |
resources.get_caret() match { |
66086 | 507 |
case Some(caret) => snapshot.current_command(caret.node_name, caret.offset) |
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
508 |
case None => None |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
509 |
} |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
510 |
} |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
511 |
override def current_command(context: Unit, snapshot: Document.Snapshot): Option[Command] = |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
512 |
current_command(snapshot) |
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
513 |
|
66101
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
514 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
515 |
/* overlays */ |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
516 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
517 |
override def node_overlays(name: Document.Node.Name): Document.Node.Overlays = |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
518 |
resources.node_overlays(name) |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
519 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
520 |
override def insert_overlay(command: Command, fn: String, args: List[String]): Unit = |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
521 |
resources.insert_overlay(command, fn, args) |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
522 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
523 |
override def remove_overlay(command: Command, fn: String, args: List[String]): Unit = |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
524 |
resources.remove_overlay(command, fn, args) |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
525 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
526 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
527 |
/* hyperlinks */ |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
528 |
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
529 |
override def hyperlink_command( |
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
530 |
focus: Boolean, snapshot: Document.Snapshot, id: Document_ID.Generic, |
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
531 |
offset: Symbol.Offset = 0): Option[Hyperlink] = |
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
532 |
{ |
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
533 |
if (snapshot.is_outdated) None |
66216 | 534 |
else |
535 |
snapshot.find_command_position(id, offset).map(node_pos => |
|
536 |
new Hyperlink { |
|
537 |
def follow(unit: Unit) { channel.write(Protocol.Caret_Update(node_pos, focus)) } |
|
538 |
}) |
|
66215
9a8b6b86350c
proper hyperlink_command, notably for locate_query;
wenzelm
parents:
66211
diff
changeset
|
539 |
} |
66094 | 540 |
|
66101
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
541 |
|
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
542 |
/* dispatcher thread */ |
0f0f294e314f
maintain overlays within main state of document models;
wenzelm
parents:
66100
diff
changeset
|
543 |
|
66094 | 544 |
override def assert_dispatcher[A](body: => A): A = session.assert_dispatcher(body) |
545 |
override def require_dispatcher[A](body: => A): A = session.require_dispatcher(body) |
|
546 |
override def send_dispatcher(body: => Unit): Unit = session.send_dispatcher(body) |
|
547 |
override def send_wait_dispatcher(body: => Unit): Unit = session.send_wait_dispatcher(body) |
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
548 |
} |
64605 | 549 |
} |