| author | wenzelm |
| Sun, 02 Nov 2025 23:41:05 +0100 | |
| changeset 83463 | e7c174e33556 |
| parent 83462 | b6916f2ff672 |
| child 83464 | bbb47e048642 |
| permissions | -rw-r--r-- |
| 72761 | 1 |
/* Title: Tools/VSCode/src/language_server.scala |
| 64605 | 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 |
||
| 83434 | 16 |
import java.io.{File => JFile}
|
|
83433
e26f102d2498
minor performance tuning: prefer mutable.ListBuffer;
wenzelm
parents:
83430
diff
changeset
|
17 |
|
|
e26f102d2498
minor performance tuning: prefer mutable.ListBuffer;
wenzelm
parents:
83430
diff
changeset
|
18 |
import scala.collection.mutable |
| 64605 | 19 |
import scala.annotation.tailrec |
20 |
||
21 |
||
| 75393 | 22 |
object Language_Server {
|
| 83437 | 23 |
/* abstract editor operations */ |
24 |
||
25 |
class Editor(server: Language_Server) extends isabelle.Editor[Unit] {
|
|
26 |
/* PIDE session and document model */ |
|
27 |
||
28 |
override def session: VSCode_Session = server.session |
|
29 |
override def flush(): Unit = session.resources.flush_input(session, server.channel) |
|
30 |
||
31 |
override def get_models(): Iterable[Document.Model] = session.resources.get_models() |
|
32 |
||
33 |
||
34 |
/* input from client */ |
|
35 |
||
36 |
private val delay_input: Delay = |
|
37 |
Delay.last(server.options.seconds("vscode_input_delay"), server.channel.Error_Logger) {
|
|
38 |
session.resources.flush_input(session, server.channel) |
|
39 |
} |
|
40 |
||
41 |
override def invoke(): Unit = delay_input.invoke() |
|
42 |
override def revoke(): Unit = delay_input.revoke() |
|
43 |
||
44 |
||
45 |
/* current situation */ |
|
46 |
||
47 |
override def current_node(context: Unit): Option[Document.Node.Name] = |
|
48 |
session.resources.get_caret().map(_.model.node_name) |
|
49 |
override def current_node_snapshot(context: Unit): Option[Document.Snapshot] = |
|
50 |
session.resources.get_caret().map(caret => session.resources.snapshot(caret.model)) |
|
51 |
||
52 |
override def node_snapshot(name: Document.Node.Name): Document.Snapshot = {
|
|
53 |
session.resources.get_snapshot(name) match {
|
|
54 |
case Some(snapshot) => snapshot |
|
55 |
case None => session.snapshot(name) |
|
56 |
} |
|
57 |
} |
|
58 |
||
59 |
def current_command(snapshot: Document.Snapshot): Option[Command] = {
|
|
60 |
session.resources.get_caret() match {
|
|
61 |
case Some(caret) if snapshot.loaded_theory_command(caret.offset).isEmpty => |
|
62 |
snapshot.current_command(caret.node_name, caret.offset) |
|
63 |
case _ => None |
|
64 |
} |
|
65 |
} |
|
66 |
override def current_command(context: Unit, snapshot: Document.Snapshot): Option[Command] = |
|
67 |
current_command(snapshot) |
|
68 |
||
69 |
||
70 |
/* output messages */ |
|
71 |
||
72 |
override def output_state(): Boolean = |
|
73 |
session.resources.options.bool("editor_output_state")
|
|
74 |
||
75 |
||
76 |
/* overlays */ |
|
77 |
||
78 |
override def node_overlays(name: Document.Node.Name): Document.Node.Overlays = |
|
79 |
session.resources.node_overlays(name) |
|
80 |
||
81 |
override def insert_overlay(command: Command, fn: String, args: List[String]): Unit = |
|
82 |
session.resources.insert_overlay(command, fn, args) |
|
83 |
||
84 |
override def remove_overlay(command: Command, fn: String, args: List[String]): Unit = |
|
85 |
session.resources.remove_overlay(command, fn, args) |
|
86 |
||
87 |
||
88 |
/* hyperlinks */ |
|
89 |
||
90 |
override def hyperlink_command( |
|
91 |
focus: Boolean, |
|
92 |
snapshot: Document.Snapshot, |
|
93 |
id: Document_ID.Generic, |
|
94 |
offset: Symbol.Offset = 0 |
|
95 |
): Option[Hyperlink] = {
|
|
96 |
if (snapshot.is_outdated) None |
|
97 |
else |
|
98 |
snapshot.find_command_position(id, offset).map(node_pos => |
|
99 |
new Hyperlink {
|
|
100 |
def follow(unit: Unit): Unit = server.channel.write(LSP.Caret_Update(node_pos, focus)) |
|
101 |
}) |
|
102 |
} |
|
103 |
||
104 |
||
105 |
/* dispatcher thread */ |
|
106 |
||
107 |
override def assert_dispatcher[A](body: => A): A = session.assert_dispatcher(body) |
|
108 |
override def require_dispatcher[A](body: => A): A = session.require_dispatcher(body) |
|
109 |
override def send_dispatcher(body: => Unit): Unit = session.send_dispatcher(body) |
|
110 |
override def send_wait_dispatcher(body: => Unit): Unit = session.send_wait_dispatcher(body) |
|
111 |
} |
|
| 64605 | 112 |
} |
113 |
||
| 72761 | 114 |
class Language_Server( |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
115 |
val channel: Channel, |
|
83401
1d9b1ca7977e
dismantle redundant sledgehammer history in Isabelle/Scala, which does not quite work --- and is already done via vscode.setState / vscode.getState;
wenzelm
parents:
83384
diff
changeset
|
116 |
val options: Options, |
|
83435
0f9bae334ac6
more uniform Isabelle_System.default_logic, with subtle change of semantics due to getenv_strict;
wenzelm
parents:
83434
diff
changeset
|
117 |
session_name: String = Isabelle_System.default_logic(), |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
74307
diff
changeset
|
118 |
include_sessions: List[String] = Nil, |
| 64605 | 119 |
session_dirs: List[Path] = Nil, |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
120 |
session_ancestor: Option[String] = None, |
|
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
121 |
session_requirements: Boolean = false, |
|
75295
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
75262
diff
changeset
|
122 |
session_no_build: Boolean = false, |
| 64717 | 123 |
modes: List[String] = Nil, |
| 79777 | 124 |
log: Logger = new Logger |
| 75393 | 125 |
) {
|
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
126 |
server => |
|
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
127 |
|
| 83437 | 128 |
val editor: Language_Server.Editor = new Language_Server.Editor(server) |
129 |
||
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
130 |
|
| 64725 | 131 |
/* prover session */ |
| 64605 | 132 |
|
|
82744
0ca8b1861fa3
clarified signature: more explicit subtypes of Session, with corresponding subtypes of Resources;
wenzelm
parents:
82720
diff
changeset
|
133 |
private val session_ = Synchronized(None: Option[VSCode_Session]) |
|
0ca8b1861fa3
clarified signature: more explicit subtypes of Session, with corresponding subtypes of Resources;
wenzelm
parents:
82720
diff
changeset
|
134 |
def session: VSCode_Session = session_.value getOrElse error("Server inactive")
|
|
0ca8b1861fa3
clarified signature: more explicit subtypes of Session, with corresponding subtypes of Resources;
wenzelm
parents:
82720
diff
changeset
|
135 |
def resources: VSCode_Resources = session.resources |
| 83380 | 136 |
def ml_settings: ML_Settings = session.store.ml_settings |
137 |
||
| 83440 | 138 |
private val sledgehammer = new VSCode_Sledgehammer(server) |
| 64605 | 139 |
|
| 64651 | 140 |
def rendering_offset(node_pos: Line.Node_Position): Option[(VSCode_Rendering, Text.Offset)] = |
| 64649 | 141 |
for {
|
|
76765
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents:
76729
diff
changeset
|
142 |
rendering <- resources.get_rendering(new JFile(node_pos.name)) |
|
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents:
76729
diff
changeset
|
143 |
offset <- rendering.model.content.doc.offset(node_pos.pos) |
| 64649 | 144 |
} yield (rendering, offset) |
145 |
||
|
66085
100f02099532
added abstract editor operations, notably for Query_Operation;
wenzelm
parents:
66054
diff
changeset
|
146 |
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
|
147 |
|
| 64605 | 148 |
|
| 64725 | 149 |
/* input from client or file-system */ |
150 |
||
| 73538 | 151 |
private val file_watcher: File_Watcher = |
152 |
File_Watcher(sync_documents, options.seconds("vscode_load_delay"))
|
|
153 |
||
| 71704 | 154 |
private val delay_load: Delay = |
155 |
Delay.last(options.seconds("vscode_load_delay"), channel.Error_Logger) {
|
|
| 66100 | 156 |
val (invoke_input, invoke_load) = |
157 |
resources.resolve_dependencies(session, editor, file_watcher) |
|
| 83437 | 158 |
if (invoke_input) editor.invoke() |
| 73367 | 159 |
if (invoke_load) delay_load.invoke() |
| 64800 | 160 |
} |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
161 |
|
| 75393 | 162 |
private def close_document(file: JFile): Unit = {
|
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
163 |
if (resources.close_model(file)) {
|
|
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
164 |
file_watcher.register_parent(file) |
| 65116 | 165 |
sync_documents(Set(file)) |
| 83437 | 166 |
editor.invoke() |
| 65116 | 167 |
delay_output.invoke() |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
168 |
} |
|
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
169 |
} |
|
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
170 |
|
| 75393 | 171 |
private def sync_documents(changed: Set[JFile]): Unit = {
|
| 65116 | 172 |
resources.sync_models(changed) |
| 83437 | 173 |
editor.invoke() |
| 65116 | 174 |
delay_output.invoke() |
175 |
} |
|
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
176 |
|
| 73340 | 177 |
private def change_document( |
| 75393 | 178 |
file: JFile, |
179 |
version: Long, |
|
180 |
changes: List[LSP.TextDocumentChange] |
|
181 |
): Unit = {
|
|
|
81020
0efa8e784384
lsp: removed change_document normalization because it causes desyncs;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
79777
diff
changeset
|
182 |
changes.foreach(change => |
| 66674 | 183 |
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
|
184 |
|
| 83437 | 185 |
editor.invoke() |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
186 |
delay_output.invoke() |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
187 |
} |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
188 |
|
| 64710 | 189 |
|
| 65189 | 190 |
/* caret handling */ |
191 |
||
| 71704 | 192 |
private val delay_caret_update: Delay = |
| 75393 | 193 |
Delay.last(options.seconds("vscode_input_delay"), channel.Error_Logger) {
|
194 |
session.caret_focus.post(Session.Caret_Focus) |
|
195 |
} |
|
| 65189 | 196 |
|
| 75393 | 197 |
private def update_caret(caret: Option[(JFile, Line.Position)]): Unit = {
|
| 65189 | 198 |
resources.update_caret(caret) |
199 |
delay_caret_update.invoke() |
|
| 83437 | 200 |
editor.invoke() |
| 65189 | 201 |
} |
202 |
||
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
203 |
|
| 65983 | 204 |
/* preview */ |
205 |
||
| 66098 | 206 |
private lazy val preview_panel = new Preview_Panel(resources) |
| 65983 | 207 |
|
| 71704 | 208 |
private lazy val delay_preview: Delay = |
| 75393 | 209 |
Delay.last(options.seconds("vscode_output_delay"), channel.Error_Logger) {
|
| 66098 | 210 |
if (preview_panel.flush(channel)) delay_preview.invoke() |
| 65983 | 211 |
} |
212 |
||
|
81031
c9e8461dd5f2
lsp: clarified preview_request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81030
diff
changeset
|
213 |
private def preview_request(file: JFile, column: Int): Unit = {
|
| 66098 | 214 |
preview_panel.request(file, column) |
| 65983 | 215 |
delay_preview.invoke() |
216 |
} |
|
217 |
||
218 |
||
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
219 |
/* output to client */ |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
220 |
|
| 71704 | 221 |
private val delay_output: Delay = |
| 75393 | 222 |
Delay.last(options.seconds("vscode_output_delay"), channel.Error_Logger) {
|
|
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65231
diff
changeset
|
223 |
if (resources.flush_output(channel)) delay_output.invoke() |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
224 |
} |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
225 |
|
| 75393 | 226 |
def update_output(changed_nodes: Iterable[JFile]): Unit = {
|
| 66138 | 227 |
resources.update_output(changed_nodes) |
228 |
delay_output.invoke() |
|
229 |
} |
|
230 |
||
| 75393 | 231 |
def update_output_visible(): Unit = {
|
| 66138 | 232 |
resources.update_output_visible() |
233 |
delay_output.invoke() |
|
234 |
} |
|
235 |
||
| 64725 | 236 |
private val prover_output = |
237 |
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
|
238 |
case changed => |
| 66138 | 239 |
update_output(changed.nodes.toList.map(resources.node_file(_))) |
| 64725 | 240 |
} |
| 64710 | 241 |
|
| 66389 | 242 |
private val syslog_messages = |
243 |
Session.Consumer[Prover.Output](getClass.getName) {
|
|
| 82987 | 244 |
case output => channel.log_writeln(resources.output_text(XML.content(output.message))) |
| 64692 | 245 |
} |
246 |
||
247 |
||
|
81043
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
248 |
/* decoration request */ |
|
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
249 |
|
|
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
250 |
private def decoration_request(file: JFile): Unit = |
|
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
251 |
resources.force_decorations(channel, file) |
|
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
252 |
|
|
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
253 |
|
| 64605 | 254 |
/* init and exit */ |
255 |
||
| 75393 | 256 |
def init(id: LSP.Id): Unit = {
|
257 |
def reply_ok(msg: String): Unit = {
|
|
| 72761 | 258 |
channel.write(LSP.Initialize.reply(id, "")) |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
259 |
channel.writeln(msg) |
|
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
260 |
} |
|
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
261 |
|
| 75393 | 262 |
def reply_error(msg: String): Unit = {
|
| 72761 | 263 |
channel.write(LSP.Initialize.reply(id, msg)) |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
264 |
channel.error_message(msg) |
| 64641 | 265 |
} |
266 |
||
| 64643 | 267 |
val try_session = |
268 |
try {
|
|
| 76656 | 269 |
val session_background = |
270 |
Sessions.background( |
|
|
75262
ec62c5401522
discontinued isabelle_filesystem (superseded by isabelle_encoding), see also da1108a6d249;
wenzelm
parents:
75209
diff
changeset
|
271 |
options, session_name, dirs = session_dirs, |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
74307
diff
changeset
|
272 |
include_sessions = include_sessions, session_ancestor = session_ancestor, |
|
75920
27bf2533f4a4
clarified signature: follow Sessions.Deps.check_errors (despite Process_Result.check);
wenzelm
parents:
75710
diff
changeset
|
273 |
session_requirements = session_requirements).check_errors |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
274 |
|
|
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
275 |
def build(no_build: Boolean = false): Build.Results = |
| 71981 | 276 |
Build.build(options, |
| 76656 | 277 |
selection = Sessions.Selection.session(session_background.session_name), |
278 |
build_heap = true, no_build = no_build, dirs = session_dirs, |
|
279 |
infos = session_background.infos) |
|
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
280 |
|
|
75295
38398766be6b
command-line arguments for "isabelle vscode", similar to "isabelle jedit";
wenzelm
parents:
75262
diff
changeset
|
281 |
if (!session_no_build && !build(no_build = true).ok) {
|
| 76656 | 282 |
val start_msg = "Build started for Isabelle/" + session_background.session_name + " ..." |
| 64733 | 283 |
val fail_msg = "Session build failed -- prover process remains inactive!" |
284 |
||
| 67856 | 285 |
val progress = channel.progress(verbose = true) |
| 64733 | 286 |
progress.echo(start_msg); channel.writeln(start_msg) |
287 |
||
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
288 |
if (!build().ok) { progress.echo(fail_msg); error(fail_msg) }
|
| 64733 | 289 |
} |
290 |
||
| 82778 | 291 |
val session_resources = new VSCode_Resources(options, session_background, log) |
292 |
val session_options = options.bool.update("editor_output_state", true)
|
|
293 |
val session = |
|
294 |
new VSCode_Session(session_options, session_resources) {
|
|
295 |
override def deps_changed(): Unit = delay_load.invoke() |
|
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64725
diff
changeset
|
296 |
} |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
297 |
|
| 76656 | 298 |
Some((session_background, session)) |
| 64605 | 299 |
} |
|
68690
354c04092cd0
more flexible session selection as in "isabelle jedit";
wenzelm
parents:
67870
diff
changeset
|
300 |
catch { case ERROR(msg) => reply_error(msg); None }
|
| 64605 | 301 |
|
| 76656 | 302 |
for ((session_background, session) <- try_session) {
|
| 78178 | 303 |
val store = Store(options) |
|
77414
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
76794
diff
changeset
|
304 |
val session_heaps = |
| 82752 | 305 |
store.session_heaps(session_background, logic = session_background.session_name) |
| 76655 | 306 |
|
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
307 |
session_.change(_ => Some(session)) |
|
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
308 |
|
| 65227 | 309 |
session.commands_changed += prover_output |
| 66389 | 310 |
session.syslog_messages += syslog_messages |
| 65227 | 311 |
|
312 |
dynamic_output.init() |
|
| 83384 | 313 |
sledgehammer.init() |
| 65227 | 314 |
|
| 71607 | 315 |
try {
|
|
77414
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
76794
diff
changeset
|
316 |
Isabelle_Process.start( |
|
0d5994eef9e6
clarified signature: allow to provide session_heaps by different means, e.g. from tmp directory or alternative session structure;
wenzelm
parents:
76794
diff
changeset
|
317 |
options, session, session_background, session_heaps, modes = modes).await_startup() |
| 76656 | 318 |
reply_ok( |
319 |
"Welcome to Isabelle/" + session_background.session_name + |
|
320 |
Isabelle_System.isabelle_heading()) |
|
| 71604 | 321 |
} |
322 |
catch { case ERROR(msg) => reply_error(msg) }
|
|
| 64643 | 323 |
} |
| 64605 | 324 |
} |
325 |
||
| 75393 | 326 |
def shutdown(id: LSP.Id): Unit = {
|
| 72761 | 327 |
def reply(err: String): Unit = channel.write(LSP.Shutdown.reply(id, err)) |
| 64641 | 328 |
|
| 64703 | 329 |
session_.change({
|
330 |
case Some(session) => |
|
| 65228 | 331 |
session.commands_changed -= prover_output |
| 66389 | 332 |
session.syslog_messages -= syslog_messages |
| 65228 | 333 |
|
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
334 |
dynamic_output.exit() |
| 65228 | 335 |
|
336 |
delay_load.revoke() |
|
337 |
file_watcher.shutdown() |
|
| 83437 | 338 |
editor.revoke() |
| 64703 | 339 |
delay_output.revoke() |
| 65228 | 340 |
delay_caret_update.revoke() |
| 65983 | 341 |
delay_preview.revoke() |
| 83384 | 342 |
sledgehammer.exit() |
| 65228 | 343 |
|
| 66677 | 344 |
val result = session.stop() |
| 71747 | 345 |
if (result.ok) reply("")
|
346 |
else reply("Prover shutdown failed: " + result.rc)
|
|
| 64703 | 347 |
None |
348 |
case None => |
|
349 |
reply("Prover inactive")
|
|
350 |
None |
|
351 |
}) |
|
| 64605 | 352 |
} |
353 |
||
| 75393 | 354 |
def exit(): Unit = {
|
| 64717 | 355 |
log("\n")
|
| 74307 | 356 |
sys.exit(if (session_.value.isEmpty) Process_Result.RC.ok else Process_Result.RC.failure) |
| 64646 | 357 |
} |
| 64605 | 358 |
|
359 |
||
| 64877 | 360 |
/* completion */ |
361 |
||
| 75393 | 362 |
def completion(id: LSP.Id, node_pos: Line.Node_Position): Unit = {
|
| 64877 | 363 |
val result = |
364 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
74307
diff
changeset
|
365 |
yield rendering.completion(node_pos, offset)) getOrElse Nil |
| 72761 | 366 |
channel.write(LSP.Completion.reply(id, result)) |
| 64877 | 367 |
} |
368 |
||
369 |
||
| 66138 | 370 |
/* spell-checker dictionary */ |
371 |
||
| 75393 | 372 |
def update_dictionary(include: Boolean, permanent: Boolean): Unit = {
|
| 66138 | 373 |
for {
|
374 |
spell_checker <- resources.spell_checker.get |
|
375 |
caret <- resources.get_caret() |
|
|
76765
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents:
76729
diff
changeset
|
376 |
rendering = resources.rendering(caret.model) |
| 66138 | 377 |
range = rendering.before_caret_range(caret.offset) |
378 |
Text.Info(_, word) <- Spell_Checker.current_word(rendering, range) |
|
379 |
} {
|
|
380 |
spell_checker.update(word, include, permanent) |
|
381 |
update_output_visible() |
|
382 |
} |
|
383 |
} |
|
384 |
||
| 75393 | 385 |
def reset_dictionary(): Unit = {
|
386 |
for (spell_checker <- resources.spell_checker.get) {
|
|
| 66138 | 387 |
spell_checker.reset() |
388 |
update_output_visible() |
|
389 |
} |
|
390 |
} |
|
391 |
||
392 |
||
|
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64618
diff
changeset
|
393 |
/* hover */ |
| 64605 | 394 |
|
| 75393 | 395 |
def hover(id: LSP.Id, node_pos: Line.Node_Position): Unit = {
|
| 64641 | 396 |
val result = |
397 |
for {
|
|
| 64651 | 398 |
(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
|
399 |
info <- rendering.tooltips(VSCode_Rendering.tooltip_elements, Text.Range(offset, offset + 1)) |
| 64641 | 400 |
} yield {
|
| 65197 | 401 |
val range = rendering.model.content.doc.range(info.range) |
| 72761 | 402 |
val contents = info.info.map(t => LSP.MarkedString(resources.output_pretty_tooltip(List(t)))) |
| 64748 | 403 |
(range, contents) |
| 64641 | 404 |
} |
| 72761 | 405 |
channel.write(LSP.Hover.reply(id, result)) |
| 64641 | 406 |
} |
| 64605 | 407 |
|
408 |
||
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
409 |
/* goto definition */ |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
410 |
|
| 75393 | 411 |
def goto_definition(id: LSP.Id, node_pos: Line.Node_Position): Unit = {
|
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
412 |
val result = |
| 64651 | 413 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
| 64649 | 414 |
yield rendering.hyperlinks(Text.Range(offset, offset + 1))) getOrElse Nil |
| 72761 | 415 |
channel.write(LSP.GotoDefinition.reply(id, result)) |
|
64648
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
416 |
} |
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
417 |
|
|
5d7f741aaccb
basic support for hyperlinks / Goto Definition Request;
wenzelm
parents:
64646
diff
changeset
|
418 |
|
| 64767 | 419 |
/* document highlights */ |
420 |
||
| 75393 | 421 |
def document_highlights(id: LSP.Id, node_pos: Line.Node_Position): Unit = {
|
| 64767 | 422 |
val result = |
423 |
(for ((rendering, offset) <- rendering_offset(node_pos)) |
|
424 |
yield {
|
|
| 65197 | 425 |
val model = rendering.model |
426 |
rendering.caret_focus_ranges(Text.Range(offset, offset + 1), model.content.text_range) |
|
| 72761 | 427 |
.map(r => LSP.DocumentHighlight.text(model.content.doc.range(r))) |
| 64767 | 428 |
}) getOrElse Nil |
| 72761 | 429 |
channel.write(LSP.DocumentHighlights.reply(id, result)) |
| 64767 | 430 |
} |
431 |
||
432 |
||
|
81067
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
433 |
/* code actions */ |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
434 |
|
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
435 |
def code_action_request(id: LSP.Id, file: JFile, range: Line.Range): Unit = {
|
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
436 |
for {
|
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
437 |
model <- resources.get_model(file) |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
438 |
version <- model.version |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
439 |
doc = model.content.doc |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
440 |
text_range <- doc.text_range(range) |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
441 |
} {
|
| 83424 | 442 |
val snapshot = resources.snapshot(model) |
| 83430 | 443 |
val results = |
444 |
snapshot.command_results(Text.Range(text_range.start - 1, text_range.stop + 1)) |
|
445 |
.iterator.map(_._2).toList |
|
446 |
val actions = |
|
447 |
List.from( |
|
448 |
for {
|
|
449 |
(snippet, props) <- Protocol.sendback_snippets(results).iterator |
|
450 |
id <- Position.Id.unapply(props) |
|
451 |
(node, command) <- snapshot.find_command(id) |
|
452 |
start <- node.command_start(command) |
|
453 |
range = command.core_range + start |
|
454 |
current_text <- model.get_text(range) |
|
455 |
} yield {
|
|
456 |
val line_range = doc.range(range) |
|
457 |
val edit_text = |
|
458 |
if (props.contains(Markup.PADDING_COMMAND)) {
|
|
459 |
val whole_line = doc.lines(line_range.start.line) |
|
460 |
val indent = whole_line.text.takeWhile(_.isWhitespace) |
|
461 |
current_text + "\n" + Library.prefix_lines(indent, snippet) |
|
462 |
} |
|
463 |
else current_text + snippet |
|
464 |
val edit = LSP.TextEdit(line_range, resources.output_edit(edit_text)) |
|
465 |
LSP.CodeAction(snippet, List(LSP.TextDocumentEdit(file, Some(version), List(edit)))) |
|
466 |
}) |
|
| 83429 | 467 |
channel.write(LSP.CodeActionRequest.reply(id, actions)) |
|
81067
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
468 |
} |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
469 |
} |
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
470 |
|
|
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
471 |
|
|
81030
88879ff1cef5
lsp: added Symbols_Request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81029
diff
changeset
|
472 |
/* symbols */ |
|
88879ff1cef5
lsp: added Symbols_Request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81029
diff
changeset
|
473 |
|
|
81075
f0341e6b1b30
lsp: added symbol conversion request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81069
diff
changeset
|
474 |
def symbols_convert_request(id: LSP.Id, text: String, unicode: Boolean): Unit = {
|
|
f0341e6b1b30
lsp: added symbol conversion request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81069
diff
changeset
|
475 |
val converted = Symbol.output(unicode, text) |
|
f0341e6b1b30
lsp: added symbol conversion request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81069
diff
changeset
|
476 |
channel.write(LSP.Symbols_Convert_Request.reply(id, converted)) |
|
f0341e6b1b30
lsp: added symbol conversion request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81069
diff
changeset
|
477 |
} |
|
81030
88879ff1cef5
lsp: added Symbols_Request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81029
diff
changeset
|
478 |
|
| 83463 | 479 |
def symbols_request(): Unit = {
|
|
83363
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
480 |
val abbrevs = |
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
481 |
resources.get_caret().flatMap { caret =>
|
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
482 |
resources.get_model(caret.file).map(_.syntax().abbrevs) |
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
483 |
}.getOrElse(session.resources.session_base.overall_syntax.abbrevs) |
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
484 |
|
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
485 |
channel.write(LSP.Symbols_Response(Symbol.symbols, abbrevs)) |
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
486 |
} |
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
487 |
|
|
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
488 |
|
| 83406 | 489 |
def documentation_request(): Unit = |
| 83380 | 490 |
channel.write(LSP.Documentation_Response(ml_settings)) |
|
83363
486e094b676c
various improvements of Isabelle/VSCode: original changeset by Diana Korchmar, LMU München;
wenzelm
parents:
82987
diff
changeset
|
491 |
|
|
81030
88879ff1cef5
lsp: added Symbols_Request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81029
diff
changeset
|
492 |
|
| 64605 | 493 |
/* main loop */ |
494 |
||
| 75393 | 495 |
def start(): Unit = {
|
| 64717 | 496 |
log("Server started " + Date.now())
|
| 64605 | 497 |
|
| 75393 | 498 |
def handle(json: JSON.T): Unit = {
|
| 64605 | 499 |
try {
|
500 |
json match {
|
|
| 72761 | 501 |
case LSP.Initialize(id) => init(id) |
| 83422 | 502 |
case LSP.Initialized() => |
| 72761 | 503 |
case LSP.Shutdown(id) => shutdown(id) |
| 83422 | 504 |
case LSP.Exit() => exit() |
| 72761 | 505 |
case LSP.DidOpenTextDocument(file, _, version, text) => |
506 |
change_document(file, version, List(LSP.TextDocumentChange(None, text))) |
|
|
67292
386ddccfccbf
implicit thy_load context for bibtex files (VSCode);
wenzelm
parents:
66976
diff
changeset
|
507 |
delay_load.invoke() |
| 72761 | 508 |
case LSP.DidChangeTextDocument(file, version, changes) => |
| 66674 | 509 |
change_document(file, version, changes) |
| 72761 | 510 |
case LSP.DidCloseTextDocument(file) => close_document(file) |
511 |
case LSP.Completion(id, node_pos) => completion(id, node_pos) |
|
| 83422 | 512 |
case LSP.Include_Word() => update_dictionary(true, false) |
513 |
case LSP.Include_Word_Permanently() => update_dictionary(true, true) |
|
514 |
case LSP.Exclude_Word() => update_dictionary(false, false) |
|
515 |
case LSP.Exclude_Word_Permanently() => update_dictionary(false, true) |
|
516 |
case LSP.Reset_Words() => reset_dictionary() |
|
| 72761 | 517 |
case LSP.Hover(id, node_pos) => hover(id, node_pos) |
518 |
case LSP.GotoDefinition(id, node_pos) => goto_definition(id, node_pos) |
|
519 |
case LSP.DocumentHighlights(id, node_pos) => document_highlights(id, node_pos) |
|
|
81067
1b1d72c45ff4
lsp: added support for code actions to apply active sendback markups;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81062
diff
changeset
|
520 |
case LSP.CodeActionRequest(id, file, range) => code_action_request(id, file, range) |
|
81043
2174ec5f0d0c
lsp: added decoration_request notification;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81031
diff
changeset
|
521 |
case LSP.Decoration_Request(file) => decoration_request(file) |
| 72761 | 522 |
case LSP.Caret_Update(caret) => update_caret(caret) |
|
81029
f4cb1e35c63e
lsp: added Output_Set_Margin and State_Set_Margin Notifications;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81028
diff
changeset
|
523 |
case LSP.Output_Set_Margin(margin) => dynamic_output.set_margin(margin) |
|
81028
84f6f17274d0
lsp: changed State_Init notification into a request and removed State_Init_Response;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81027
diff
changeset
|
524 |
case LSP.State_Init(id) => State_Panel.init(id, server) |
| 81027 | 525 |
case LSP.State_Exit(state_id) => State_Panel.exit(state_id) |
526 |
case LSP.State_Locate(state_id) => State_Panel.locate(state_id) |
|
527 |
case LSP.State_Update(state_id) => State_Panel.update(state_id) |
|
| 81084 | 528 |
case LSP.State_Auto_Update(state_id, enabled) => |
529 |
State_Panel.auto_update(state_id, enabled) |
|
|
81029
f4cb1e35c63e
lsp: added Output_Set_Margin and State_Set_Margin Notifications;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81028
diff
changeset
|
530 |
case LSP.State_Set_Margin(state_id, margin) => State_Panel.set_margin(state_id, margin) |
| 81084 | 531 |
case LSP.Symbols_Convert_Request(id, text, boolean) => |
532 |
symbols_convert_request(id, text, boolean) |
|
|
81031
c9e8461dd5f2
lsp: clarified preview_request;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81030
diff
changeset
|
533 |
case LSP.Preview_Request(file, column) => preview_request(file, column) |
| 83463 | 534 |
case LSP.Symbols_Request() => symbols_request() |
| 83460 | 535 |
case LSP.Documentation_Request() => documentation_request() |
| 83456 | 536 |
case LSP.Sledgehammer_Provers_Request() => sledgehammer.provers() |
| 83446 | 537 |
case LSP.Sledgehammer_Request(args) => sledgehammer.request(args) |
| 83422 | 538 |
case LSP.Sledgehammer_Cancel() => sledgehammer.cancel() |
539 |
case LSP.Sledgehammer_Locate() => sledgehammer.locate() |
|
|
83454
62178604511c
clarified protocol for "PIDE/sledgehammer_sendback" vs. "PIDE/sledgehammer_insert": rely on existing Isabelle/Scala/PIDE operations;
wenzelm
parents:
83446
diff
changeset
|
540 |
case LSP.Sledgehammer_Sendback(text) => sledgehammer.sendback(text) |
| 72761 | 541 |
case _ => if (!LSP.ResponseMessage.is_empty(json)) log("### IGNORED")
|
| 64605 | 542 |
} |
543 |
} |
|
| 64687 | 544 |
catch { case exn: Throwable => channel.log_error_message(Exn.message(exn)) }
|
| 64605 | 545 |
} |
546 |
||
| 75393 | 547 |
@tailrec def loop(): Unit = {
|
| 64605 | 548 |
channel.read() match {
|
549 |
case Some(json) => |
|
550 |
json match {
|
|
| 71601 | 551 |
case bulk: List[_] => bulk.foreach(handle) |
| 64605 | 552 |
case _ => handle(json) |
553 |
} |
|
554 |
loop() |
|
| 64717 | 555 |
case None => log("### TERMINATE")
|
| 64605 | 556 |
} |
557 |
} |
|
558 |
loop() |
|
559 |
} |
|
560 |
} |