| author | blanchet |
| Mon, 25 Sep 2023 17:16:32 +0200 | |
| changeset 78697 | 8ca71c0ae31f |
| parent 78243 | 0e221a8128e4 |
| child 81309 | ccdbe1b538fc |
| permissions | -rw-r--r-- |
| 60749 | 1 |
/* Title: Tools/jEdit/src/debugger_dockable.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Dockable window for Isabelle/ML debugger. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.jedit |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
| 60848 | 12 |
import java.awt.{BorderLayout, Dimension}
|
| 61007 | 13 |
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent, FocusAdapter, FocusEvent,
|
14 |
MouseEvent, MouseAdapter} |
|
| 60883 | 15 |
import javax.swing.{JTree, JMenuItem}
|
| 60850 | 16 |
import javax.swing.tree.{DefaultMutableTreeNode, DefaultTreeModel, TreeSelectionModel}
|
| 60848 | 17 |
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener}
|
| 60832 | 18 |
|
| 61016 | 19 |
import scala.collection.immutable.SortedMap |
| 75852 | 20 |
import scala.swing.{Button, Label, Component, ScrollPane, SplitPane, Orientation, BorderPanel}
|
| 60832 | 21 |
import scala.swing.event.ButtonClicked |
| 60749 | 22 |
|
| 60875 | 23 |
import org.gjt.sp.jedit.{jEdit, View}
|
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
24 |
import org.gjt.sp.jedit.menu.EnhancedMenuItem |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
25 |
import org.gjt.sp.jedit.textarea.JEditTextArea |
| 60749 | 26 |
|
27 |
||
| 75393 | 28 |
object Debugger_Dockable {
|
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
29 |
/* breakpoints */ |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
30 |
|
| 75393 | 31 |
def get_breakpoint(text_area: JEditTextArea, offset: Text.Offset): Option[(Command, Long)] = {
|
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
32 |
GUI_Thread.require {}
|
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
33 |
|
| 64882 | 34 |
Document_View.get(text_area) match {
|
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
35 |
case Some(doc_view) => |
|
76765
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents:
76610
diff
changeset
|
36 |
val rendering = Document_View.rendering(doc_view) |
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
37 |
val range = JEdit_Lib.point_range(text_area.getBuffer, offset) |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
38 |
rendering.breakpoint(range) |
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
39 |
case None => None |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
40 |
} |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
41 |
} |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
42 |
|
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
43 |
|
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
44 |
/* context menu */ |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
45 |
|
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
46 |
def context_menu(text_area: JEditTextArea, offset: Text.Offset): List[JMenuItem] = |
| 65247 | 47 |
if (PIDE.session.debugger.is_active() && get_breakpoint(text_area, offset).isDefined) {
|
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
48 |
val context = jEdit.getActionContext() |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
49 |
val name = "isabelle.toggle-breakpoint" |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
50 |
List(new EnhancedMenuItem(context.getAction(name).getLabel, name, context)) |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
51 |
} |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
52 |
else Nil |
| 60848 | 53 |
} |
54 |
||
| 75393 | 55 |
class Debugger_Dockable(view: View, position: String) extends Dockable(view, position) {
|
| 76605 | 56 |
dockable => |
57 |
||
| 60749 | 58 |
GUI_Thread.require {}
|
59 |
||
| 65247 | 60 |
private val debugger = PIDE.session.debugger |
61 |
||
| 60749 | 62 |
|
63 |
/* component state -- owned by GUI thread */ |
|
64 |
||
65 |
private var current_snapshot = Document.Snapshot.init |
|
| 61016 | 66 |
private var current_threads: Debugger.Threads = SortedMap.empty |
| 60749 | 67 |
private var current_output: List[XML.Tree] = Nil |
68 |
||
69 |
||
| 60848 | 70 |
/* pretty text area */ |
71 |
||
72 |
val pretty_text_area = new Pretty_Text_Area(view) |
|
73 |
||
| 71601 | 74 |
override def detach_operation: Option[() => Unit] = pretty_text_area.detach_operation |
| 60848 | 75 |
|
| 75812 | 76 |
private def handle_resize(): Unit = |
| 75839 | 77 |
GUI_Thread.require { pretty_text_area.zoom(zoom) }
|
| 60848 | 78 |
|
| 75393 | 79 |
private def handle_update(): Unit = {
|
| 60848 | 80 |
GUI_Thread.require {}
|
81 |
||
| 66082 | 82 |
val new_snapshot = PIDE.editor.current_node_snapshot(view).getOrElse(current_snapshot) |
| 65247 | 83 |
val (new_threads, new_output) = debugger.status(tree_selection()) |
| 60848 | 84 |
|
| 75814 | 85 |
if (new_threads != current_threads) update_tree(new_threads) |
| 60848 | 86 |
|
| 75814 | 87 |
if (new_output != current_output) {
|
| 60848 | 88 |
pretty_text_area.update(new_snapshot, Command.Results.empty, Pretty.separate(new_output)) |
| 75814 | 89 |
} |
| 60848 | 90 |
|
91 |
current_snapshot = new_snapshot |
|
92 |
current_threads = new_threads |
|
93 |
current_output = new_output |
|
94 |
} |
|
95 |
||
96 |
||
97 |
/* tree view */ |
|
98 |
||
99 |
private val root = new DefaultMutableTreeNode("Threads")
|
|
100 |
||
101 |
val tree = new JTree(root) |
|
102 |
tree.setRowHeight(0) |
|
103 |
tree.getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION) |
|
104 |
||
| 61010 | 105 |
def tree_selection(): Option[Debugger.Context] = |
106 |
tree.getLastSelectedPathComponent match {
|
|
107 |
case node: DefaultMutableTreeNode => |
|
108 |
node.getUserObject match {
|
|
109 |
case c: Debugger.Context => Some(c) |
|
| 60851 | 110 |
case _ => None |
111 |
} |
|
| 60875 | 112 |
case _ => None |
113 |
} |
|
114 |
||
| 61010 | 115 |
def thread_selection(): Option[String] = tree_selection().map(_.thread_name) |
116 |
||
| 75393 | 117 |
private def update_tree(threads: Debugger.Threads): Unit = {
|
| 61018 | 118 |
val thread_contexts = |
119 |
(for ((a, b) <- threads.iterator) |
|
120 |
yield Debugger.Context(a, b)).toList |
|
| 61010 | 121 |
|
122 |
val new_tree_selection = |
|
| 61009 | 123 |
tree_selection() match {
|
| 61018 | 124 |
case Some(c) if thread_contexts.contains(c) => Some(c) |
125 |
case Some(c) if thread_contexts.exists(t => c.thread_name == t.thread_name) => |
|
126 |
Some(c.reset) |
|
127 |
case _ => thread_contexts.headOption |
|
| 60906 | 128 |
} |
| 60860 | 129 |
|
| 75814 | 130 |
tree.clearSelection() |
131 |
root.removeAllChildren() |
|
| 60848 | 132 |
|
| 61018 | 133 |
for (thread <- thread_contexts) {
|
| 61010 | 134 |
val thread_node = new DefaultMutableTreeNode(thread) |
| 75814 | 135 |
for ((_, i) <- thread.debug_states.zipWithIndex) |
| 61010 | 136 |
thread_node.add(new DefaultMutableTreeNode(thread.select(i))) |
| 60859 | 137 |
root.add(thread_node) |
| 60848 | 138 |
} |
139 |
||
| 60859 | 140 |
tree.getModel.asInstanceOf[DefaultTreeModel].reload(root) |
| 60860 | 141 |
|
| 60905 | 142 |
tree.expandRow(0) |
143 |
for (i <- Range.inclusive(tree.getRowCount - 1, 1, -1)) tree.expandRow(i) |
|
144 |
||
| 61009 | 145 |
new_tree_selection match {
|
| 61010 | 146 |
case Some(c) => |
| 60860 | 147 |
val i = |
| 61018 | 148 |
(for (t <- thread_contexts.iterator.takeWhile(t => c.thread_name != t.thread_name)) |
| 61010 | 149 |
yield t.size).sum |
150 |
tree.addSelectionRow(i + c.index + 1) |
|
| 60906 | 151 |
case None => |
| 60860 | 152 |
} |
153 |
||
| 60848 | 154 |
tree.revalidate() |
155 |
} |
|
156 |
||
| 75393 | 157 |
def update_vals(): Unit = {
|
| 60901 | 158 |
tree_selection() match {
|
| 61010 | 159 |
case Some(c) if c.stack_state.isDefined => |
| 65247 | 160 |
debugger.print_vals(c, sml_button.selected, context_field.getText) |
| 61010 | 161 |
case Some(c) => |
| 65247 | 162 |
debugger.clear_output(c.thread_name) |
| 61010 | 163 |
case None => |
| 60901 | 164 |
} |
165 |
} |
|
166 |
||
| 75814 | 167 |
tree.addTreeSelectionListener({ (_: TreeSelectionEvent) =>
|
168 |
update_focus() |
|
169 |
update_vals() |
|
170 |
}) |
|
| 61007 | 171 |
tree.addMouseListener( |
172 |
new MouseAdapter {
|
|
| 75393 | 173 |
override def mouseClicked(e: MouseEvent): Unit = {
|
| 61007 | 174 |
val click = tree.getPathForLocation(e.getX, e.getY) |
| 75814 | 175 |
if (click != null && e.getClickCount == 1) update_focus() |
| 61007 | 176 |
} |
177 |
}) |
|
| 60848 | 178 |
|
| 60883 | 179 |
private val tree_pane = new ScrollPane(Component.wrap(tree)) |
180 |
tree_pane.horizontalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
181 |
tree_pane.verticalScrollBarPolicy = ScrollPane.BarPolicy.Always |
|
182 |
tree_pane.minimumSize = new Dimension(200, 100) |
|
| 60848 | 183 |
|
184 |
||
185 |
/* controls */ |
|
| 60832 | 186 |
|
| 75854 | 187 |
private val break_button = new GUI.Check("Break", init = debugger.is_break()) {
|
|
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
188 |
tooltip = "Break running threads at next possible breakpoint" |
| 75852 | 189 |
override def clicked(state: Boolean): Unit = debugger.set_break(state) |
|
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
190 |
} |
|
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
191 |
|
| 75853 | 192 |
private val continue_button = new GUI.Button("Continue") {
|
| 60907 | 193 |
tooltip = "Continue program on current thread, until next breakpoint" |
| 75853 | 194 |
override def clicked(): Unit = thread_selection().map(debugger.continue) |
| 60907 | 195 |
} |
196 |
||
| 75853 | 197 |
private val step_button = new GUI.Button("Step") {
|
| 60907 | 198 |
tooltip = "Single-step in depth-first order" |
| 75853 | 199 |
override def clicked(): Unit = thread_selection().map(debugger.step) |
| 60907 | 200 |
} |
201 |
||
| 75853 | 202 |
private val step_over_button = new GUI.Button("Step over") {
|
| 60907 | 203 |
tooltip = "Single-step within this function" |
| 75853 | 204 |
override def clicked(): Unit = thread_selection().map(debugger.step_over) |
| 60907 | 205 |
} |
206 |
||
| 75853 | 207 |
private val step_out_button = new GUI.Button("Step out") {
|
| 60907 | 208 |
tooltip = "Single-step outside this function" |
| 75853 | 209 |
override def clicked(): Unit = thread_selection().map(debugger.step_out) |
| 60907 | 210 |
} |
211 |
||
| 60862 | 212 |
private val context_label = new Label("Context:") {
|
213 |
tooltip = "Isabelle/ML context: type theory, Proof.context, Context.generic" |
|
214 |
} |
|
| 60832 | 215 |
private val context_field = |
| 75393 | 216 |
new Completion_Popup.History_Text_Field("isabelle-debugger-context") {
|
217 |
override def processKeyEvent(evt: KeyEvent): Unit = {
|
|
| 75814 | 218 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) {
|
| 60936 | 219 |
eval_expression() |
| 75814 | 220 |
} |
| 60936 | 221 |
super.processKeyEvent(evt) |
222 |
} |
|
| 60832 | 223 |
setColumns(20) |
224 |
setToolTipText(context_label.tooltip) |
|
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
225 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
| 60832 | 226 |
} |
227 |
||
| 60862 | 228 |
private val expression_label = new Label("ML:") {
|
229 |
tooltip = "Isabelle/ML or Standard ML expression" |
|
230 |
} |
|
| 60832 | 231 |
private val expression_field = |
| 75393 | 232 |
new Completion_Popup.History_Text_Field("isabelle-debugger-expression") {
|
233 |
override def processKeyEvent(evt: KeyEvent): Unit = {
|
|
| 75814 | 234 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) {
|
| 60832 | 235 |
eval_expression() |
| 75814 | 236 |
} |
| 60832 | 237 |
super.processKeyEvent(evt) |
238 |
} |
|
| 78243 | 239 |
{ val max = getPreferredSize; max.width = Int.MaxValue; setMaximumSize(max) }
|
| 60832 | 240 |
setColumns(40) |
241 |
setToolTipText(expression_label.tooltip) |
|
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
242 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
| 60832 | 243 |
} |
244 |
||
| 75853 | 245 |
private val eval_button = |
246 |
new GUI.Button("<html><b>Eval</b></html>") {
|
|
| 60862 | 247 |
tooltip = "Evaluate ML expression within optional context" |
| 75853 | 248 |
override def clicked(): Unit = eval_expression() |
| 60832 | 249 |
} |
250 |
||
| 75393 | 251 |
private def eval_expression(): Unit = {
|
| 60861 | 252 |
context_field.addCurrentToHistory() |
253 |
expression_field.addCurrentToHistory() |
|
| 60856 | 254 |
tree_selection() match {
|
| 61014 | 255 |
case Some(c) if c.debug_index.isDefined => |
| 65247 | 256 |
debugger.eval(c, sml_button.selected, context_field.getText, expression_field.getText) |
| 60856 | 257 |
case _ => |
258 |
} |
|
| 60832 | 259 |
} |
260 |
||
| 75854 | 261 |
private val sml_button = new GUI.Check("SML") {
|
| 60907 | 262 |
tooltip = "Official Standard ML instead of Isabelle/ML" |
| 60869 | 263 |
} |
264 |
||
| 75839 | 265 |
private val zoom = new Font_Info.Zoom { override def changed(): Unit = handle_resize() }
|
| 60832 | 266 |
|
| 60848 | 267 |
private val controls = |
| 66205 | 268 |
Wrap_Panel( |
269 |
List( |
|
270 |
break_button, continue_button, step_button, step_over_button, step_out_button, |
|
271 |
context_label, Component.wrap(context_field), |
|
272 |
expression_label, Component.wrap(expression_field), eval_button, sml_button, |
|
| 66206 | 273 |
pretty_text_area.search_label, pretty_text_area.search_field, zoom)) |
| 66205 | 274 |
|
| 60848 | 275 |
add(controls.peer, BorderLayout.NORTH) |
| 60749 | 276 |
|
277 |
||
| 60875 | 278 |
/* focus */ |
279 |
||
| 73367 | 280 |
override def focusOnDefaultComponent(): Unit = eval_button.requestFocus() |
| 60875 | 281 |
|
282 |
addFocusListener(new FocusAdapter {
|
|
| 73340 | 283 |
override def focusGained(e: FocusEvent): Unit = update_focus() |
| 60875 | 284 |
}) |
285 |
||
| 75393 | 286 |
private def update_focus(): Unit = {
|
| 61014 | 287 |
for (c <- tree_selection()) {
|
| 65247 | 288 |
debugger.set_focus(c) |
| 61014 | 289 |
for {
|
290 |
pos <- c.debug_position |
|
| 66082 | 291 |
link <- PIDE.editor.hyperlink_position(false, current_snapshot, pos) |
| 61015 | 292 |
} link.follow(view) |
| 61014 | 293 |
} |
| 61015 | 294 |
JEdit_Lib.jedit_text_areas(view.getBuffer).foreach(_.repaint()) |
| 60875 | 295 |
} |
296 |
||
297 |
||
| 60848 | 298 |
/* main panel */ |
| 60749 | 299 |
|
| 71601 | 300 |
val main_panel: SplitPane = new SplitPane(Orientation.Vertical) {
|
| 60848 | 301 |
oneTouchExpandable = true |
| 60883 | 302 |
leftComponent = tree_pane |
| 60848 | 303 |
rightComponent = Component.wrap(pretty_text_area) |
| 60749 | 304 |
} |
| 60848 | 305 |
set_content(main_panel) |
| 60749 | 306 |
|
307 |
||
308 |
/* main */ |
|
309 |
||
310 |
private val main = |
|
311 |
Session.Consumer[Any](getClass.getName) {
|
|
312 |
case _: Session.Global_Options => |
|
| 60889 | 313 |
GUI_Thread.later { handle_resize() }
|
| 60749 | 314 |
|
| 60900 | 315 |
case Debugger.Update => |
|
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
316 |
GUI_Thread.later {
|
| 65247 | 317 |
break_button.selected = debugger.is_break() |
|
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
318 |
handle_update() |
|
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
319 |
} |
| 60749 | 320 |
} |
321 |
||
| 75393 | 322 |
override def init(): Unit = {
|
| 60749 | 323 |
PIDE.session.global_options += main |
| 60835 | 324 |
PIDE.session.debugger_updates += main |
| 76605 | 325 |
debugger.init(dockable) |
| 60749 | 326 |
handle_update() |
| 60876 | 327 |
jEdit.propertiesChanged() |
| 60749 | 328 |
} |
329 |
||
| 75393 | 330 |
override def exit(): Unit = {
|
| 60749 | 331 |
PIDE.session.global_options -= main |
| 60835 | 332 |
PIDE.session.debugger_updates -= main |
| 60749 | 333 |
delay_resize.revoke() |
| 76605 | 334 |
debugger.exit(dockable) |
| 60876 | 335 |
jEdit.propertiesChanged() |
| 60749 | 336 |
} |
337 |
||
338 |
||
339 |
/* resize */ |
|
340 |
||
341 |
private val delay_resize = |
|
| 76610 | 342 |
Delay.first(PIDE.session.update_delay, gui = true) { handle_resize() }
|
| 60749 | 343 |
|
344 |
addComponentListener(new ComponentAdapter {
|
|
| 73340 | 345 |
override def componentResized(e: ComponentEvent): Unit = delay_resize.invoke() |
346 |
override def componentShown(e: ComponentEvent): Unit = delay_resize.invoke() |
|
| 60749 | 347 |
}) |
348 |
} |