author | wenzelm |
Sun, 17 Nov 2024 20:14:57 +0100 | |
changeset 81477 | c9256ac99214 |
parent 81460 | 6ea0055fa42d |
child 81483 | 7d4df25af572 |
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 |
||
81318 | 12 |
import java.awt.BorderLayout |
13 |
import java.awt.event.KeyEvent |
|
81323 | 14 |
import javax.swing.JMenuItem |
81318 | 15 |
import javax.swing.event.TreeSelectionEvent |
60832 | 16 |
|
61016 | 17 |
import scala.collection.immutable.SortedMap |
81318 | 18 |
import scala.swing.{Button, Label, Component} |
60749 | 19 |
|
60875 | 20 |
import org.gjt.sp.jedit.{jEdit, View} |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
21 |
import org.gjt.sp.jedit.menu.EnhancedMenuItem |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
22 |
import org.gjt.sp.jedit.textarea.JEditTextArea |
60749 | 23 |
|
24 |
||
75393 | 25 |
object Debugger_Dockable { |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
26 |
/* breakpoints */ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
27 |
|
75393 | 28 |
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
|
29 |
GUI_Thread.require {} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
30 |
|
64882 | 31 |
Document_View.get(text_area) match { |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
32 |
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
|
33 |
val rendering = Document_View.rendering(doc_view) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
34 |
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
|
35 |
rendering.breakpoint(range) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
36 |
case None => None |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
37 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
38 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
39 |
|
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 |
/* context menu */ |
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 |
def context_menu(text_area: JEditTextArea, offset: Text.Offset): List[JMenuItem] = |
65247 | 44 |
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
|
45 |
val context = jEdit.getActionContext() |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
46 |
val name = "isabelle.toggle-breakpoint" |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
47 |
List(new EnhancedMenuItem(context.getAction(name).getLabel, name, context)) |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
48 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
49 |
else Nil |
60848 | 50 |
} |
51 |
||
75393 | 52 |
class Debugger_Dockable(view: View, position: String) extends Dockable(view, position) { |
76605 | 53 |
dockable => |
54 |
||
60749 | 55 |
GUI_Thread.require {} |
56 |
||
65247 | 57 |
private val debugger = PIDE.session.debugger |
58 |
||
60749 | 59 |
|
60 |
/* component state -- owned by GUI thread */ |
|
61 |
||
62 |
private var current_snapshot = Document.Snapshot.init |
|
61016 | 63 |
private var current_threads: Debugger.Threads = SortedMap.empty |
60749 | 64 |
private var current_output: List[XML.Tree] = Nil |
65 |
||
66 |
||
60848 | 67 |
/* pretty text area */ |
68 |
||
81336 | 69 |
private val output: Output_Area = |
81460 | 70 |
new Output_Area(view, root_name = "Threads") { |
81309 | 71 |
override def handle_update(): Unit = { |
72 |
val new_snapshot = PIDE.editor.current_node_snapshot(view).getOrElse(current_snapshot) |
|
73 |
val (new_threads, new_output) = debugger.status(tree_selection()) |
|
60848 | 74 |
|
81309 | 75 |
if (new_threads != current_threads) update_tree(new_threads) |
60848 | 76 |
|
81309 | 77 |
if (new_output != current_output) { |
81398
f92ea68473f2
clarified signature with subtle change of semantics: output consists of individual messages that are formatted (and separated) internally;
wenzelm
parents:
81387
diff
changeset
|
78 |
pretty_text_area.update(new_snapshot, Command.Results.empty, new_output) |
81309 | 79 |
} |
60848 | 80 |
|
81309 | 81 |
current_snapshot = new_snapshot |
82 |
current_threads = new_threads |
|
83 |
current_output = new_output |
|
84 |
} |
|
81310 | 85 |
|
81313 | 86 |
override def handle_focus(): Unit = update_focus() |
75814 | 87 |
} |
60848 | 88 |
|
81309 | 89 |
override def detach_operation: Option[() => Unit] = |
81327 | 90 |
output.pretty_text_area.detach_operation |
81309 | 91 |
|
81460 | 92 |
output.init_gui(dockable, split = true) |
60848 | 93 |
|
94 |
||
95 |
/* tree view */ |
|
96 |
||
81313 | 97 |
private def tree_selection(): Option[Debugger.Context] = |
81327 | 98 |
output.tree.get_selection({ case c: Debugger.Context => c }) |
60875 | 99 |
|
81313 | 100 |
private def thread_selection(): Option[String] = tree_selection().map(_.thread_name) |
61010 | 101 |
|
75393 | 102 |
private def update_tree(threads: Debugger.Threads): Unit = { |
61018 | 103 |
val thread_contexts = |
104 |
(for ((a, b) <- threads.iterator) |
|
105 |
yield Debugger.Context(a, b)).toList |
|
61010 | 106 |
|
107 |
val new_tree_selection = |
|
61009 | 108 |
tree_selection() match { |
61018 | 109 |
case Some(c) if thread_contexts.contains(c) => Some(c) |
110 |
case Some(c) if thread_contexts.exists(t => c.thread_name == t.thread_name) => |
|
111 |
Some(c.reset) |
|
112 |
case _ => thread_contexts.headOption |
|
60906 | 113 |
} |
60860 | 114 |
|
81327 | 115 |
output.tree.clear() |
60848 | 116 |
|
61018 | 117 |
for (thread <- thread_contexts) { |
81329 | 118 |
val thread_node = Tree_View.Node(thread) |
75814 | 119 |
for ((_, i) <- thread.debug_states.zipWithIndex) |
81329 | 120 |
thread_node.add(Tree_View.Node(thread.select(i))) |
81327 | 121 |
output.tree.root.add(thread_node) |
60848 | 122 |
} |
123 |
||
81327 | 124 |
output.tree.reload_model() |
60860 | 125 |
|
81327 | 126 |
output.tree.expandRow(0) |
127 |
for (i <- Range.inclusive(output.tree.getRowCount - 1, 1, -1)) output.tree.expandRow(i) |
|
60905 | 128 |
|
61009 | 129 |
new_tree_selection match { |
61010 | 130 |
case Some(c) => |
60860 | 131 |
val i = |
61018 | 132 |
(for (t <- thread_contexts.iterator.takeWhile(t => c.thread_name != t.thread_name)) |
61010 | 133 |
yield t.size).sum |
81327 | 134 |
output.tree.addSelectionRow(i + c.index + 1) |
60906 | 135 |
case None => |
60860 | 136 |
} |
137 |
||
81327 | 138 |
output.tree.revalidate() |
60848 | 139 |
} |
140 |
||
75393 | 141 |
def update_vals(): Unit = { |
60901 | 142 |
tree_selection() match { |
61010 | 143 |
case Some(c) if c.stack_state.isDefined => |
65247 | 144 |
debugger.print_vals(c, sml_button.selected, context_field.getText) |
61010 | 145 |
case Some(c) => |
65247 | 146 |
debugger.clear_output(c.thread_name) |
61010 | 147 |
case None => |
60901 | 148 |
} |
149 |
} |
|
150 |
||
60848 | 151 |
|
152 |
/* controls */ |
|
60832 | 153 |
|
75854 | 154 |
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
|
155 |
tooltip = "Break running threads at next possible breakpoint" |
75852 | 156 |
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
|
157 |
} |
13ee73f57c85
allow to break running threads at next possible breakpoint (simplified version of former option, see f3039309702e);
wenzelm
parents:
60910
diff
changeset
|
158 |
|
75853 | 159 |
private val continue_button = new GUI.Button("Continue") { |
60907 | 160 |
tooltip = "Continue program on current thread, until next breakpoint" |
81311 | 161 |
override def clicked(): Unit = thread_selection().foreach(debugger.continue) |
60907 | 162 |
} |
163 |
||
75853 | 164 |
private val step_button = new GUI.Button("Step") { |
60907 | 165 |
tooltip = "Single-step in depth-first order" |
81311 | 166 |
override def clicked(): Unit = thread_selection().foreach(debugger.step) |
60907 | 167 |
} |
168 |
||
75853 | 169 |
private val step_over_button = new GUI.Button("Step over") { |
60907 | 170 |
tooltip = "Single-step within this function" |
81311 | 171 |
override def clicked(): Unit = thread_selection().foreach(debugger.step_over) |
60907 | 172 |
} |
173 |
||
75853 | 174 |
private val step_out_button = new GUI.Button("Step out") { |
60907 | 175 |
tooltip = "Single-step outside this function" |
81311 | 176 |
override def clicked(): Unit = thread_selection().foreach(debugger.step_out) |
60907 | 177 |
} |
178 |
||
60862 | 179 |
private val context_label = new Label("Context:") { |
180 |
tooltip = "Isabelle/ML context: type theory, Proof.context, Context.generic" |
|
181 |
} |
|
60832 | 182 |
private val context_field = |
75393 | 183 |
new Completion_Popup.History_Text_Field("isabelle-debugger-context") { |
184 |
override def processKeyEvent(evt: KeyEvent): Unit = { |
|
75814 | 185 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) { |
60936 | 186 |
eval_expression() |
75814 | 187 |
} |
60936 | 188 |
super.processKeyEvent(evt) |
189 |
} |
|
60832 | 190 |
setColumns(20) |
191 |
setToolTipText(context_label.tooltip) |
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
192 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
60832 | 193 |
} |
194 |
||
60862 | 195 |
private val expression_label = new Label("ML:") { |
196 |
tooltip = "Isabelle/ML or Standard ML expression" |
|
197 |
} |
|
60832 | 198 |
private val expression_field = |
75393 | 199 |
new Completion_Popup.History_Text_Field("isabelle-debugger-expression") { |
200 |
override def processKeyEvent(evt: KeyEvent): Unit = { |
|
75814 | 201 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) { |
60832 | 202 |
eval_expression() |
75814 | 203 |
} |
60832 | 204 |
super.processKeyEvent(evt) |
205 |
} |
|
78243 | 206 |
{ val max = getPreferredSize; max.width = Int.MaxValue; setMaximumSize(max) } |
60832 | 207 |
setColumns(40) |
208 |
setToolTipText(expression_label.tooltip) |
|
69358
71ef6e6da3dc
prefer Isabelle_Fonts.sans (not mono) as derived GUI font;
wenzelm
parents:
66591
diff
changeset
|
209 |
setFont(GUI.imitate_font(getFont, scale = 1.2)) |
60832 | 210 |
} |
211 |
||
75853 | 212 |
private val eval_button = |
213 |
new GUI.Button("<html><b>Eval</b></html>") { |
|
60862 | 214 |
tooltip = "Evaluate ML expression within optional context" |
75853 | 215 |
override def clicked(): Unit = eval_expression() |
60832 | 216 |
} |
217 |
||
75393 | 218 |
private def eval_expression(): Unit = { |
60861 | 219 |
context_field.addCurrentToHistory() |
220 |
expression_field.addCurrentToHistory() |
|
60856 | 221 |
tree_selection() match { |
61014 | 222 |
case Some(c) if c.debug_index.isDefined => |
65247 | 223 |
debugger.eval(c, sml_button.selected, context_field.getText, expression_field.getText) |
60856 | 224 |
case _ => |
225 |
} |
|
60832 | 226 |
} |
227 |
||
75854 | 228 |
private val sml_button = new GUI.Check("SML") { |
60907 | 229 |
tooltip = "Official Standard ML instead of Isabelle/ML" |
60869 | 230 |
} |
231 |
||
60848 | 232 |
private val controls = |
66205 | 233 |
Wrap_Panel( |
234 |
List( |
|
235 |
break_button, continue_button, step_button, step_over_button, step_out_button, |
|
236 |
context_label, Component.wrap(context_field), |
|
81379 | 237 |
expression_label, Component.wrap(expression_field), eval_button, sml_button) ::: |
81387
c677755779f5
more uniform pretty_text_area.zoom via its zoom_component;
wenzelm
parents:
81379
diff
changeset
|
238 |
output.pretty_text_area.search_zoom_components) |
66205 | 239 |
|
60848 | 240 |
add(controls.peer, BorderLayout.NORTH) |
60749 | 241 |
|
242 |
||
60875 | 243 |
/* focus */ |
244 |
||
73367 | 245 |
override def focusOnDefaultComponent(): Unit = eval_button.requestFocus() |
60875 | 246 |
|
75393 | 247 |
private def update_focus(): Unit = { |
61014 | 248 |
for (c <- tree_selection()) { |
65247 | 249 |
debugger.set_focus(c) |
61014 | 250 |
for { |
251 |
pos <- c.debug_position |
|
66082 | 252 |
link <- PIDE.editor.hyperlink_position(false, current_snapshot, pos) |
61015 | 253 |
} link.follow(view) |
61014 | 254 |
} |
61015 | 255 |
JEdit_Lib.jedit_text_areas(view.getBuffer).foreach(_.repaint()) |
60875 | 256 |
} |
257 |
||
81376 | 258 |
output.tree.addTreeSelectionListener({ (e: TreeSelectionEvent) => |
259 |
update_focus() |
|
260 |
update_vals() |
|
261 |
}) |
|
262 |
||
60875 | 263 |
|
60749 | 264 |
/* main */ |
265 |
||
266 |
private val main = |
|
267 |
Session.Consumer[Any](getClass.getName) { |
|
268 |
case _: Session.Global_Options => |
|
81327 | 269 |
GUI_Thread.later { output.handle_resize() } |
60749 | 270 |
|
60900 | 271 |
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
|
272 |
GUI_Thread.later { |
65247 | 273 |
break_button.selected = debugger.is_break() |
81327 | 274 |
output.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
|
275 |
} |
60749 | 276 |
} |
277 |
||
75393 | 278 |
override def init(): Unit = { |
60749 | 279 |
PIDE.session.global_options += main |
60835 | 280 |
PIDE.session.debugger_updates += main |
76605 | 281 |
debugger.init(dockable) |
81477 | 282 |
output.init() |
60876 | 283 |
jEdit.propertiesChanged() |
60749 | 284 |
} |
285 |
||
75393 | 286 |
override def exit(): Unit = { |
60749 | 287 |
PIDE.session.global_options -= main |
60835 | 288 |
PIDE.session.debugger_updates -= main |
81477 | 289 |
output.exit() |
76605 | 290 |
debugger.exit(dockable) |
60876 | 291 |
jEdit.propertiesChanged() |
60749 | 292 |
} |
293 |
} |