author | Thomas Lindae <thomas.lindae@in.tum.de> |
Mon, 01 Jul 2024 18:48:26 +0200 | |
changeset 81028 | 84f6f17274d0 |
parent 81025 | d4eb94b46e83 |
child 81029 | f4cb1e35c63e |
permissions | -rw-r--r-- |
66098 | 1 |
/* Title: Tools/VSCode/src/state_panel.scala |
66096 | 2 |
Author: Makarius |
3 |
||
4 |
Show proof state. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.vscode |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
12 |
||
75393 | 13 |
object State_Panel { |
66096 | 14 |
private val make_id = Counter.make() |
66098 | 15 |
private val instances = Synchronized(Map.empty[Counter.ID, State_Panel]) |
66096 | 16 |
|
81028
84f6f17274d0
lsp: changed State_Init notification into a request and removed State_Init_Response;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81025
diff
changeset
|
17 |
def init(id: LSP.Id, server: Language_Server): Unit = { |
66098 | 18 |
val instance = new State_Panel(server) |
66096 | 19 |
instances.change(_ + (instance.id -> instance)) |
20 |
instance.init() |
|
81028
84f6f17274d0
lsp: changed State_Init notification into a request and removed State_Init_Response;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81025
diff
changeset
|
21 |
instance.init_response(id) |
66096 | 22 |
} |
23 |
||
75393 | 24 |
def exit(id: Counter.ID): Unit = { |
66096 | 25 |
instances.change(map => |
26 |
map.get(id) match { |
|
27 |
case None => map |
|
28 |
case Some(instance) => instance.exit(); map - id |
|
29 |
}) |
|
30 |
} |
|
31 |
||
32 |
def locate(id: Counter.ID): Unit = |
|
33 |
instances.value.get(id).foreach(state => |
|
34 |
state.server.editor.send_dispatcher(state.locate())) |
|
35 |
||
36 |
def update(id: Counter.ID): Unit = |
|
37 |
instances.value.get(id).foreach(state => |
|
38 |
state.server.editor.send_dispatcher(state.update())) |
|
66211 | 39 |
|
40 |
def auto_update(id: Counter.ID, enabled: Boolean): Unit = |
|
41 |
instances.value.get(id).foreach(state => |
|
42 |
state.server.editor.send_dispatcher(state.auto_update(Some(enabled)))) |
|
66096 | 43 |
} |
44 |
||
45 |
||
75393 | 46 |
class State_Panel private(val server: Language_Server) { |
66096 | 47 |
/* output */ |
48 |
||
66098 | 49 |
val id: Counter.ID = State_Panel.make_id() |
81025
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
50 |
private val margin: Double = 80 |
66096 | 51 |
|
52 |
private def output(content: String): Unit = |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73340
diff
changeset
|
53 |
server.channel.write(LSP.State_Output(id, content, auto_update_enabled.value)) |
66096 | 54 |
|
81028
84f6f17274d0
lsp: changed State_Init notification into a request and removed State_Init_Response;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81025
diff
changeset
|
55 |
private def init_response(id: LSP.Id): Unit = |
84f6f17274d0
lsp: changed State_Init notification into a request and removed State_Init_Response;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81025
diff
changeset
|
56 |
server.channel.write(LSP.State_Init.reply(id, this.id)) |
81021
89bfada3a16d
lsp: added State_Init_Response message;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
75979
diff
changeset
|
57 |
|
66096 | 58 |
|
59 |
/* query operation */ |
|
60 |
||
66397 | 61 |
private val output_active = Synchronized(true) |
62 |
||
66096 | 63 |
private val print_state = |
64 |
new Query_Operation(server.editor, (), "print_state", _ => (), |
|
75126
da1108a6d249
various improvements to Isabelle/VSCode (by Denis Paluca and Fabian Huch);
wenzelm
parents:
73340
diff
changeset
|
65 |
(_, _, body) => |
81023
8602af6f4bd0
tuned formatting;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81021
diff
changeset
|
66 |
if (output_active.value && body.nonEmpty) { |
81025
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
67 |
if (server.resources.html_output) { |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
68 |
val node_context = |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
69 |
new Browser_Info.Node_Context { |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
70 |
override def make_ref(props: Properties.T, body: XML.Body): Option[XML.Elem] = |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
71 |
for { |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
72 |
thy_file <- Position.Def_File.unapply(props) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
73 |
def_line <- Position.Def_Line.unapply(props) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
74 |
source <- server.resources.source_file(thy_file) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
75 |
uri = File.uri(Path.explode(source).absolute_file) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
76 |
} yield HTML.link(uri.toString + "#" + def_line, body) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
77 |
} |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
78 |
val elements = Browser_Info.extra_elements.copy(entity = Markup.Elements.full) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
79 |
val separate = Pretty.separate(body) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
80 |
val formatted = Pretty.formatted(separate, margin = margin) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
81 |
val html = node_context.make_html(elements, formatted) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
82 |
output(HTML.source(html).toString) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
83 |
} else { |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
84 |
output(server.resources.output_pretty(body, margin)) |
d4eb94b46e83
lsp: added State and Dynamic Output html_output and margin handling;
Thomas Lindae <thomas.lindae@in.tum.de>
parents:
81023
diff
changeset
|
85 |
} |
66096 | 86 |
}) |
87 |
||
73340 | 88 |
def locate(): Unit = print_state.locate_query() |
66096 | 89 |
|
75393 | 90 |
def update(): Unit = { |
66096 | 91 |
server.editor.current_node_snapshot(()) match { |
92 |
case Some(snapshot) => |
|
93 |
(server.editor.current_command((), snapshot), print_state.get_location) match { |
|
94 |
case (Some(command1), Some(command2)) if command1.id == command2.id => |
|
95 |
case _ => print_state.apply_query(Nil) |
|
96 |
} |
|
97 |
case None => |
|
98 |
} |
|
99 |
} |
|
100 |
||
101 |
||
102 |
/* auto update */ |
|
103 |
||
104 |
private val auto_update_enabled = Synchronized(true) |
|
105 |
||
75393 | 106 |
def auto_update(set: Option[Boolean] = None): Unit = { |
66211 | 107 |
val enabled = |
108 |
auto_update_enabled.guarded_access(a => |
|
109 |
set match { |
|
110 |
case None => Some((a, a)) |
|
111 |
case Some(b) => Some((b, b)) |
|
112 |
}) |
|
113 |
if (enabled) update() |
|
114 |
} |
|
66096 | 115 |
|
116 |
||
66201
d8f2c745f572
GUI controls similar to Tools/jEdit/src/state_dockable.scala;
wenzelm
parents:
66099
diff
changeset
|
117 |
|
66096 | 118 |
/* main */ |
119 |
||
120 |
private val main = |
|
121 |
Session.Consumer[Any](getClass.getName) { |
|
66099 | 122 |
case changed: Session.Commands_Changed => |
123 |
if (changed.assignment) auto_update() |
|
124 |
||
125 |
case Session.Caret_Focus => |
|
126 |
auto_update() |
|
66096 | 127 |
} |
128 |
||
75393 | 129 |
def init(): Unit = { |
66096 | 130 |
server.session.commands_changed += main |
131 |
server.session.caret_focus += main |
|
132 |
server.editor.send_wait_dispatcher { print_state.activate() } |
|
133 |
server.editor.send_dispatcher { auto_update() } |
|
134 |
} |
|
135 |
||
75393 | 136 |
def exit(): Unit = { |
66397 | 137 |
output_active.change(_ => false) |
66096 | 138 |
server.session.commands_changed -= main |
139 |
server.session.caret_focus -= main |
|
66397 | 140 |
server.editor.send_wait_dispatcher { print_state.deactivate() } |
66096 | 141 |
} |
142 |
} |