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