author | wenzelm |
Sat, 02 Nov 2024 15:59:58 +0100 | |
changeset 81314 | fad1278d0f5b |
parent 81313 | 5f28bded8d7d |
child 81315 | b796e59ec3ef |
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 |
||
81309 | 72 |
private val tree_text_area: Tree_Text_Area = |
73 |
new Tree_Text_Area(view, root_name = "Threads") { |
|
81313 | 74 |
override def handle_tree_selection(e: TreeSelectionEvent): Unit = { |
75 |
update_focus() |
|
76 |
update_vals() |
|
77 |
} |
|
78 |
||
81309 | 79 |
override def handle_resize(): Unit = |
80 |
GUI_Thread.require { pretty_text_area.zoom(zoom) } |
|
60848 | 81 |
|
81309 | 82 |
override def handle_update(): Unit = { |
83 |
GUI_Thread.require {} |
|
60848 | 84 |
|
81309 | 85 |
val new_snapshot = PIDE.editor.current_node_snapshot(view).getOrElse(current_snapshot) |
86 |
val (new_threads, new_output) = debugger.status(tree_selection()) |
|
60848 | 87 |
|
81309 | 88 |
if (new_threads != current_threads) update_tree(new_threads) |
60848 | 89 |
|
81309 | 90 |
if (new_output != current_output) { |
91 |
pretty_text_area.update(new_snapshot, Command.Results.empty, Pretty.separate(new_output)) |
|
92 |
} |
|
60848 | 93 |
|
81309 | 94 |
current_snapshot = new_snapshot |
95 |
current_threads = new_threads |
|
96 |
current_output = new_output |
|
97 |
} |
|
81310 | 98 |
|
81313 | 99 |
override def handle_focus(): Unit = update_focus() |
75814 | 100 |
} |
60848 | 101 |
|
81309 | 102 |
override def detach_operation: Option[() => Unit] = |
103 |
tree_text_area.pretty_text_area.detach_operation |
|
104 |
||
81314 | 105 |
tree_text_area.init_gui(dockable) |
60848 | 106 |
|
107 |
||
108 |
/* tree view */ |
|
109 |
||
81313 | 110 |
private def tree: JTree = tree_text_area.tree |
60848 | 111 |
|
81313 | 112 |
private def tree_selection(): Option[Debugger.Context] = |
81310 | 113 |
tree_text_area.get_tree_selection({ case c: Debugger.Context => c }) |
60875 | 114 |
|
81313 | 115 |
private def thread_selection(): Option[String] = tree_selection().map(_.thread_name) |
61010 | 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 |
|
81309 | 130 |
tree_text_area.clear() |
60848 | 131 |
|
61018 | 132 |
for (thread <- thread_contexts) { |
61010 | 133 |
val thread_node = new DefaultMutableTreeNode(thread) |
75814 | 134 |
for ((_, i) <- thread.debug_states.zipWithIndex) |
61010 | 135 |
thread_node.add(new DefaultMutableTreeNode(thread.select(i))) |
81309 | 136 |
tree_text_area.root.add(thread_node) |
60848 | 137 |
} |
138 |
||
81309 | 139 |
tree_text_area.reload() |
60860 | 140 |
|
60905 | 141 |
tree.expandRow(0) |
142 |
for (i <- Range.inclusive(tree.getRowCount - 1, 1, -1)) tree.expandRow(i) |
|
143 |
||
61009 | 144 |
new_tree_selection match { |
61010 | 145 |
case Some(c) => |
60860 | 146 |
val i = |
61018 | 147 |
(for (t <- thread_contexts.iterator.takeWhile(t => c.thread_name != t.thread_name)) |
61010 | 148 |
yield t.size).sum |
149 |
tree.addSelectionRow(i + c.index + 1) |
|
60906 | 150 |
case None => |
60860 | 151 |
} |
152 |
||
60848 | 153 |
tree.revalidate() |
154 |
} |
|
155 |
||
75393 | 156 |
def update_vals(): Unit = { |
60901 | 157 |
tree_selection() match { |
61010 | 158 |
case Some(c) if c.stack_state.isDefined => |
65247 | 159 |
debugger.print_vals(c, sml_button.selected, context_field.getText) |
61010 | 160 |
case Some(c) => |
65247 | 161 |
debugger.clear_output(c.thread_name) |
61010 | 162 |
case None => |
60901 | 163 |
} |
164 |
} |
|
165 |
||
61007 | 166 |
tree.addMouseListener( |
167 |
new MouseAdapter { |
|
75393 | 168 |
override def mouseClicked(e: MouseEvent): Unit = { |
61007 | 169 |
val click = tree.getPathForLocation(e.getX, e.getY) |
75814 | 170 |
if (click != null && e.getClickCount == 1) update_focus() |
61007 | 171 |
} |
172 |
}) |
|
60848 | 173 |
|
174 |
||
175 |
/* controls */ |
|
60832 | 176 |
|
75854 | 177 |
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
|
178 |
tooltip = "Break running threads at next possible breakpoint" |
75852 | 179 |
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
|
180 |
} |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
181 |
|
75853 | 182 |
private val continue_button = new GUI.Button("Continue") { |
60907 | 183 |
tooltip = "Continue program on current thread, until next breakpoint" |
81311 | 184 |
override def clicked(): Unit = thread_selection().foreach(debugger.continue) |
60907 | 185 |
} |
186 |
||
75853 | 187 |
private val step_button = new GUI.Button("Step") { |
60907 | 188 |
tooltip = "Single-step in depth-first order" |
81311 | 189 |
override def clicked(): Unit = thread_selection().foreach(debugger.step) |
60907 | 190 |
} |
191 |
||
75853 | 192 |
private val step_over_button = new GUI.Button("Step over") { |
60907 | 193 |
tooltip = "Single-step within this function" |
81311 | 194 |
override def clicked(): Unit = thread_selection().foreach(debugger.step_over) |
60907 | 195 |
} |
196 |
||
75853 | 197 |
private val step_out_button = new GUI.Button("Step out") { |
60907 | 198 |
tooltip = "Single-step outside this function" |
81311 | 199 |
override def clicked(): Unit = thread_selection().foreach(debugger.step_out) |
60907 | 200 |
} |
201 |
||
60862 | 202 |
private val context_label = new Label("Context:") { |
203 |
tooltip = "Isabelle/ML context: type theory, Proof.context, Context.generic" |
|
204 |
} |
|
60832 | 205 |
private val context_field = |
75393 | 206 |
new Completion_Popup.History_Text_Field("isabelle-debugger-context") { |
207 |
override def processKeyEvent(evt: KeyEvent): Unit = { |
|
75814 | 208 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) { |
60936 | 209 |
eval_expression() |
75814 | 210 |
} |
60936 | 211 |
super.processKeyEvent(evt) |
212 |
} |
|
60832 | 213 |
setColumns(20) |
214 |
setToolTipText(context_label.tooltip) |
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
215 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
60832 | 216 |
} |
217 |
||
60862 | 218 |
private val expression_label = new Label("ML:") { |
219 |
tooltip = "Isabelle/ML or Standard ML expression" |
|
220 |
} |
|
60832 | 221 |
private val expression_field = |
75393 | 222 |
new Completion_Popup.History_Text_Field("isabelle-debugger-expression") { |
223 |
override def processKeyEvent(evt: KeyEvent): Unit = { |
|
75814 | 224 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) { |
60832 | 225 |
eval_expression() |
75814 | 226 |
} |
60832 | 227 |
super.processKeyEvent(evt) |
228 |
} |
|
78243 | 229 |
{ val max = getPreferredSize; max.width = Int.MaxValue; setMaximumSize(max) } |
60832 | 230 |
setColumns(40) |
231 |
setToolTipText(expression_label.tooltip) |
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
232 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
60832 | 233 |
} |
234 |
||
75853 | 235 |
private val eval_button = |
236 |
new GUI.Button("<html><b>Eval</b></html>") { |
|
60862 | 237 |
tooltip = "Evaluate ML expression within optional context" |
75853 | 238 |
override def clicked(): Unit = eval_expression() |
60832 | 239 |
} |
240 |
||
75393 | 241 |
private def eval_expression(): Unit = { |
60861 | 242 |
context_field.addCurrentToHistory() |
243 |
expression_field.addCurrentToHistory() |
|
60856 | 244 |
tree_selection() match { |
61014 | 245 |
case Some(c) if c.debug_index.isDefined => |
65247 | 246 |
debugger.eval(c, sml_button.selected, context_field.getText, expression_field.getText) |
60856 | 247 |
case _ => |
248 |
} |
|
60832 | 249 |
} |
250 |
||
75854 | 251 |
private val sml_button = new GUI.Check("SML") { |
60907 | 252 |
tooltip = "Official Standard ML instead of Isabelle/ML" |
60869 | 253 |
} |
254 |
||
81309 | 255 |
private val zoom = |
256 |
new Font_Info.Zoom { override def changed(): Unit = tree_text_area.handle_resize() } |
|
60832 | 257 |
|
60848 | 258 |
private val controls = |
66205 | 259 |
Wrap_Panel( |
260 |
List( |
|
261 |
break_button, continue_button, step_button, step_over_button, step_out_button, |
|
262 |
context_label, Component.wrap(context_field), |
|
263 |
expression_label, Component.wrap(expression_field), eval_button, sml_button, |
|
81309 | 264 |
tree_text_area.pretty_text_area.search_label, |
265 |
tree_text_area.pretty_text_area.search_field, zoom)) |
|
66205 | 266 |
|
60848 | 267 |
add(controls.peer, BorderLayout.NORTH) |
60749 | 268 |
|
269 |
||
60875 | 270 |
/* focus */ |
271 |
||
73367 | 272 |
override def focusOnDefaultComponent(): Unit = eval_button.requestFocus() |
60875 | 273 |
|
75393 | 274 |
private def update_focus(): Unit = { |
61014 | 275 |
for (c <- tree_selection()) { |
65247 | 276 |
debugger.set_focus(c) |
61014 | 277 |
for { |
278 |
pos <- c.debug_position |
|
66082 | 279 |
link <- PIDE.editor.hyperlink_position(false, current_snapshot, pos) |
61015 | 280 |
} link.follow(view) |
61014 | 281 |
} |
61015 | 282 |
JEdit_Lib.jedit_text_areas(view.getBuffer).foreach(_.repaint()) |
60875 | 283 |
} |
284 |
||
285 |
||
60749 | 286 |
/* main */ |
287 |
||
288 |
private val main = |
|
289 |
Session.Consumer[Any](getClass.getName) { |
|
290 |
case _: Session.Global_Options => |
|
81309 | 291 |
GUI_Thread.later { tree_text_area.handle_resize() } |
60749 | 292 |
|
60900 | 293 |
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
|
294 |
GUI_Thread.later { |
65247 | 295 |
break_button.selected = debugger.is_break() |
81309 | 296 |
tree_text_area.handle_update() |
60932
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
297 |
} |
60749 | 298 |
} |
299 |
||
75393 | 300 |
override def init(): Unit = { |
60749 | 301 |
PIDE.session.global_options += main |
60835 | 302 |
PIDE.session.debugger_updates += main |
76605 | 303 |
debugger.init(dockable) |
81309 | 304 |
tree_text_area.handle_update() |
60876 | 305 |
jEdit.propertiesChanged() |
60749 | 306 |
} |
307 |
||
75393 | 308 |
override def exit(): Unit = { |
60749 | 309 |
PIDE.session.global_options -= main |
60835 | 310 |
PIDE.session.debugger_updates -= main |
81312 | 311 |
tree_text_area.delay_resize.revoke() |
76605 | 312 |
debugger.exit(dockable) |
60876 | 313 |
jEdit.propertiesChanged() |
60749 | 314 |
} |
315 |
} |