author | wenzelm |
Mon, 10 Aug 2015 20:22:49 +0200 | |
changeset 60880 | fa958e24ff24 |
parent 60878 | 1f0d2bbcf38b |
child 60883 | 8eb8640d7300 |
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} |
60875 | 13 |
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent, MouseEvent, MouseAdapter, |
14 |
FocusAdapter, FocusEvent} |
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
15 |
import javax.swing.{JTree, JScrollPane, JMenuItem} |
60850 | 16 |
import javax.swing.tree.{DefaultMutableTreeNode, DefaultTreeModel, TreeSelectionModel} |
60848 | 17 |
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener} |
60832 | 18 |
|
60857 | 19 |
import scala.swing.{Button, Label, Component, SplitPane, Orientation, CheckBox} |
60832 | 20 |
import scala.swing.event.ButtonClicked |
60749 | 21 |
|
60875 | 22 |
import org.gjt.sp.jedit.{jEdit, View} |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
23 |
import org.gjt.sp.jedit.menu.EnhancedMenuItem |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
24 |
import org.gjt.sp.jedit.textarea.JEditTextArea |
60749 | 25 |
|
26 |
||
60848 | 27 |
object Debugger_Dockable |
28 |
{ |
|
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
29 |
/* state entries */ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
30 |
|
60851 | 31 |
sealed case class Thread_Entry(thread_name: String, debug_states: List[Debugger.Debug_State]) |
60848 | 32 |
{ |
33 |
override def toString: String = thread_name |
|
34 |
} |
|
60851 | 35 |
|
36 |
sealed case class Stack_Entry(debug_state: Debugger.Debug_State, index: Int) |
|
37 |
{ |
|
38 |
override def toString: String = debug_state.function |
|
39 |
} |
|
60878
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 |
/* breakpoints */ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
43 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
44 |
def toggle_breakpoint(command: Command, breakpoint: Long) |
60878
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 |
GUI_Thread.require {} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
47 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
48 |
Debugger.toggle_breakpoint(command, breakpoint) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
49 |
jEdit.propertiesChanged() |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
50 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
51 |
|
60880
fa958e24ff24
set breakpoint state on ML side, relying on stable situation within the PIDE editing queue;
wenzelm
parents:
60878
diff
changeset
|
52 |
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
|
53 |
{ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
54 |
GUI_Thread.require {} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
55 |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
56 |
PIDE.document_view(text_area) match { |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
57 |
case Some(doc_view) => |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
58 |
val rendering = doc_view.get_rendering() |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
59 |
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
|
60 |
rendering.breakpoint(range) |
60878
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
61 |
case None => None |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
62 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
63 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
64 |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
65 |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
66 |
/* context menu */ |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
67 |
|
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
68 |
def context_menu(text_area: JEditTextArea, offset: Text.Offset): List[JMenuItem] = |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
69 |
if (get_breakpoint(text_area, offset).isDefined) { |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
70 |
val context = jEdit.getActionContext() |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
71 |
val name = "isabelle.toggle-breakpoint" |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
72 |
List(new EnhancedMenuItem(context.getAction(name).getLabel, name, context)) |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
73 |
} |
1f0d2bbcf38b
added action to toggle breakpoints (on editor side);
wenzelm
parents:
60876
diff
changeset
|
74 |
else Nil |
60848 | 75 |
} |
76 |
||
60749 | 77 |
class Debugger_Dockable(view: View, position: String) extends Dockable(view, position) |
78 |
{ |
|
79 |
GUI_Thread.require {} |
|
80 |
||
81 |
||
82 |
/* component state -- owned by GUI thread */ |
|
83 |
||
84 |
private var current_snapshot = Document.Snapshot.init |
|
60848 | 85 |
private var current_threads: Map[String, List[Debugger.Debug_State]] = Map.empty |
60749 | 86 |
private var current_output: List[XML.Tree] = Nil |
87 |
||
88 |
||
60848 | 89 |
/* pretty text area */ |
90 |
||
91 |
val pretty_text_area = new Pretty_Text_Area(view) |
|
92 |
||
93 |
override def detach_operation = pretty_text_area.detach_operation |
|
94 |
||
95 |
private def handle_resize() |
|
96 |
{ |
|
97 |
GUI_Thread.require {} |
|
98 |
||
99 |
pretty_text_area.resize( |
|
100 |
Font_Info.main(PIDE.options.real("jedit_font_scale") * zoom.factor / 100)) |
|
101 |
} |
|
102 |
||
103 |
private def handle_update() |
|
104 |
{ |
|
105 |
GUI_Thread.require {} |
|
106 |
||
107 |
val new_state = Debugger.current_state() |
|
108 |
||
109 |
val new_snapshot = PIDE.editor.current_node_snapshot(view).getOrElse(current_snapshot) |
|
110 |
val new_threads = new_state.threads |
|
60851 | 111 |
val new_output = |
112 |
{ |
|
60859 | 113 |
val current_thread_selection = thread_selection() |
60851 | 114 |
(for { |
115 |
(thread_name, results) <- new_state.output |
|
60859 | 116 |
if current_thread_selection.isEmpty || current_thread_selection.get == thread_name |
60851 | 117 |
(_, tree) <- results.iterator |
118 |
} yield tree).toList |
|
119 |
} |
|
60848 | 120 |
|
121 |
if (new_threads != current_threads) { |
|
60851 | 122 |
val thread_entries = |
60848 | 123 |
(for ((a, b) <- new_threads.iterator) |
60851 | 124 |
yield Debugger_Dockable.Thread_Entry(a, b)).toList.sortBy(_.thread_name) |
125 |
update_tree(thread_entries) |
|
60848 | 126 |
} |
127 |
||
128 |
if (new_output != current_output) |
|
129 |
pretty_text_area.update(new_snapshot, Command.Results.empty, Pretty.separate(new_output)) |
|
130 |
||
131 |
current_snapshot = new_snapshot |
|
132 |
current_threads = new_threads |
|
133 |
current_output = new_output |
|
134 |
} |
|
135 |
||
136 |
||
137 |
/* tree view */ |
|
138 |
||
139 |
private val root = new DefaultMutableTreeNode("Threads") |
|
140 |
||
141 |
val tree = new JTree(root) |
|
142 |
tree.setRowHeight(0) |
|
143 |
tree.getSelectionModel.setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION) |
|
144 |
||
60854 | 145 |
def tree_selection(): Option[(Debugger_Dockable.Thread_Entry, Option[Int])] = |
60851 | 146 |
tree.getSelectionPath match { |
147 |
case null => None |
|
148 |
case path => |
|
149 |
path.getPath.toList.map(n => n.asInstanceOf[DefaultMutableTreeNode].getUserObject) match { |
|
150 |
case List(_, t: Debugger_Dockable.Thread_Entry) => |
|
60854 | 151 |
Some((t, None)) |
60851 | 152 |
case List(_, t: Debugger_Dockable.Thread_Entry, s: Debugger_Dockable.Stack_Entry) => |
60854 | 153 |
Some((t, Some(s.index))) |
60851 | 154 |
case _ => None |
155 |
} |
|
156 |
} |
|
157 |
||
60859 | 158 |
def thread_selection(): Option[String] = tree_selection().map(sel => sel._1.thread_name) |
159 |
||
60875 | 160 |
def focus_selection(): Option[Position.T] = |
161 |
tree_selection() match { |
|
162 |
case Some((t, opt_index)) => |
|
163 |
val i = opt_index getOrElse 0 |
|
164 |
if (i < t.debug_states.length) Some(t.debug_states(i).pos) else None |
|
165 |
case _ => None |
|
166 |
} |
|
167 |
||
60851 | 168 |
private def update_tree(thread_entries: List[Debugger_Dockable.Thread_Entry]) |
60848 | 169 |
{ |
60860 | 170 |
val old_thread_selection = thread_selection() |
171 |
||
60848 | 172 |
tree.clearSelection |
173 |
root.removeAllChildren |
|
174 |
||
60859 | 175 |
for (thread_entry <- thread_entries) { |
176 |
val thread_node = new DefaultMutableTreeNode(thread_entry) |
|
60851 | 177 |
for ((debug_state, i) <- thread_entry.debug_states.zipWithIndex) { |
60859 | 178 |
val stack_node = |
179 |
new DefaultMutableTreeNode(Debugger_Dockable.Stack_Entry(debug_state, i)) |
|
180 |
thread_node.add(stack_node) |
|
60848 | 181 |
} |
60859 | 182 |
root.add(thread_node) |
60848 | 183 |
} |
184 |
||
60859 | 185 |
tree.getModel.asInstanceOf[DefaultTreeModel].reload(root) |
60860 | 186 |
|
187 |
old_thread_selection match { |
|
188 |
case Some(thread_name) if thread_entries.exists(t => t.thread_name == thread_name) => |
|
189 |
val i = |
|
190 |
(for (t <- thread_entries.iterator.takeWhile(t => t.thread_name != thread_name)) |
|
191 |
yield 1 + t.debug_states.length).sum |
|
192 |
tree.addSelectionRow(i + 1) |
|
193 |
case _ => |
|
194 |
} |
|
60859 | 195 |
for (i <- 0 until tree.getRowCount) tree.expandRow(i) |
60860 | 196 |
|
60848 | 197 |
tree.revalidate() |
198 |
} |
|
199 |
||
60875 | 200 |
tree.addTreeSelectionListener( |
201 |
new TreeSelectionListener { |
|
202 |
override def valueChanged(e: TreeSelectionEvent) { update_focus(focus_selection()) } |
|
203 |
}) |
|
60848 | 204 |
|
205 |
tree.addMouseListener(new MouseAdapter { |
|
206 |
override def mouseClicked(e: MouseEvent) |
|
207 |
{ |
|
208 |
val click = tree.getPathForLocation(e.getX, e.getY) |
|
209 |
if (click != null && e.getClickCount == 1) { |
|
210 |
(click.getLastPathComponent, tree.getLastSelectedPathComponent) match { |
|
211 |
case (node: DefaultMutableTreeNode, node1: DefaultMutableTreeNode) if node == node1 => |
|
60875 | 212 |
handle_update() |
60848 | 213 |
case _ => |
214 |
} |
|
215 |
} |
|
216 |
} |
|
217 |
}) |
|
218 |
||
219 |
val tree_view = new JScrollPane(tree) |
|
60849 | 220 |
tree_view.setMinimumSize(new Dimension(200, 50)) |
60848 | 221 |
|
222 |
||
223 |
/* controls */ |
|
60832 | 224 |
|
60862 | 225 |
private val context_label = new Label("Context:") { |
226 |
tooltip = "Isabelle/ML context: type theory, Proof.context, Context.generic" |
|
227 |
} |
|
60832 | 228 |
private val context_field = |
229 |
new Completion_Popup.History_Text_Field("isabelle-debugger-context") |
|
230 |
{ |
|
231 |
setColumns(20) |
|
232 |
setToolTipText(context_label.tooltip) |
|
233 |
setFont(GUI.imitate_font(getFont, Font_Info.main_family(), 1.2)) |
|
234 |
} |
|
235 |
||
60862 | 236 |
private val expression_label = new Label("ML:") { |
237 |
tooltip = "Isabelle/ML or Standard ML expression" |
|
238 |
} |
|
60832 | 239 |
private val expression_field = |
240 |
new Completion_Popup.History_Text_Field("isabelle-debugger-expression") |
|
241 |
{ |
|
242 |
override def processKeyEvent(evt: KeyEvent) |
|
243 |
{ |
|
244 |
if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) |
|
245 |
eval_expression() |
|
246 |
super.processKeyEvent(evt) |
|
247 |
} |
|
248 |
{ val max = getPreferredSize; max.width = Integer.MAX_VALUE; setMaximumSize(max) } |
|
249 |
setColumns(40) |
|
250 |
setToolTipText(expression_label.tooltip) |
|
251 |
setFont(GUI.imitate_font(getFont, Font_Info.main_family(), 1.2)) |
|
252 |
} |
|
253 |
||
60857 | 254 |
private val sml_button = new CheckBox("SML") { |
255 |
tooltip = "Official Standard ML instead of Isabelle/ML" |
|
256 |
selected = false |
|
257 |
} |
|
258 |
||
60832 | 259 |
private val eval_button = new Button("<html><b>Eval</b></html>") { |
60862 | 260 |
tooltip = "Evaluate ML expression within optional context" |
60832 | 261 |
reactions += { case ButtonClicked(_) => eval_expression() } |
262 |
} |
|
263 |
||
264 |
private def eval_expression() |
|
265 |
{ |
|
60861 | 266 |
context_field.addCurrentToHistory() |
267 |
expression_field.addCurrentToHistory() |
|
60856 | 268 |
tree_selection() match { |
60857 | 269 |
case Some((t, opt_index)) if t.debug_states.nonEmpty => |
270 |
Debugger.eval(t.thread_name, opt_index getOrElse 0, |
|
271 |
sml_button.selected, context_field.getText, expression_field.getText) |
|
60856 | 272 |
case _ => |
273 |
} |
|
60832 | 274 |
} |
275 |
||
60869 | 276 |
private val step_button = new Button("Step") { |
277 |
tooltip = "Single-step in depth-first order" |
|
278 |
reactions += { case ButtonClicked(_) => thread_selection().map(Debugger.step(_)) } |
|
279 |
} |
|
280 |
||
281 |
private val step_over_button = new Button("Step over") { |
|
282 |
tooltip = "Single-step within this function" |
|
283 |
reactions += { case ButtonClicked(_) => thread_selection().map(Debugger.step_over(_)) } |
|
284 |
} |
|
285 |
||
286 |
private val step_out_button = new Button("Step out") { |
|
287 |
tooltip = "Single-step outside this function" |
|
288 |
reactions += { case ButtonClicked(_) => thread_selection().map(Debugger.step_out(_)) } |
|
289 |
} |
|
290 |
||
60854 | 291 |
private val continue_button = new Button("Continue") { |
60869 | 292 |
tooltip = "Continue program on current thread, until next breakpoint" |
293 |
reactions += { case ButtonClicked(_) => thread_selection().map(Debugger.continue(_)) } |
|
294 |
} |
|
60854 | 295 |
|
296 |
private val cancel_button = new Button("Cancel") { |
|
60869 | 297 |
tooltip = "Interrupt program on current thread" |
298 |
reactions += { case ButtonClicked(_) => thread_selection().map(Debugger.cancel(_)) } |
|
299 |
} |
|
60854 | 300 |
|
60845 | 301 |
private val debugger_active = |
302 |
new JEdit_Options.Check_Box("ML_debugger_active", "Active", "Enable debugger at run-time") |
|
303 |
||
304 |
private val debugger_stepping = |
|
305 |
new JEdit_Options.Check_Box("ML_debugger_stepping", "Stepping", "Enable single-step mode") |
|
306 |
||
60832 | 307 |
private val zoom = new Font_Info.Zoom_Box { def changed = handle_resize() } |
308 |
||
60848 | 309 |
private val controls = |
310 |
new Wrap_Panel(Wrap_Panel.Alignment.Right)( |
|
60869 | 311 |
step_button, step_over_button, step_out_button, continue_button, cancel_button, |
60848 | 312 |
context_label, Component.wrap(context_field), |
60857 | 313 |
expression_label, Component.wrap(expression_field), |
60869 | 314 |
sml_button, eval_button, |
60848 | 315 |
pretty_text_area.search_label, pretty_text_area.search_field, |
316 |
debugger_stepping, debugger_active, zoom) |
|
317 |
add(controls.peer, BorderLayout.NORTH) |
|
60749 | 318 |
|
319 |
||
60875 | 320 |
/* focus */ |
321 |
||
322 |
override def focusOnDefaultComponent { eval_button.requestFocus } |
|
323 |
||
324 |
addFocusListener(new FocusAdapter { |
|
325 |
override def focusGained(e: FocusEvent) { update_focus(focus_selection()) } |
|
326 |
override def focusLost(e: FocusEvent) { update_focus(None) } |
|
327 |
}) |
|
328 |
||
329 |
private def update_focus(focus: Option[Position.T]) |
|
330 |
{ |
|
331 |
if (Debugger.focus(focus) && focus.isDefined) |
|
332 |
PIDE.editor.hyperlink_position(current_snapshot, focus.get).foreach(_.follow(view)) |
|
333 |
} |
|
334 |
||
335 |
||
60848 | 336 |
/* main panel */ |
60749 | 337 |
|
60848 | 338 |
val main_panel = new SplitPane(Orientation.Vertical) { |
339 |
oneTouchExpandable = true |
|
340 |
leftComponent = Component.wrap(tree_view) |
|
341 |
rightComponent = Component.wrap(pretty_text_area) |
|
60749 | 342 |
} |
60848 | 343 |
set_content(main_panel) |
60749 | 344 |
|
345 |
||
346 |
/* main */ |
|
347 |
||
348 |
private val main = |
|
349 |
Session.Consumer[Any](getClass.getName) { |
|
350 |
case _: Session.Global_Options => |
|
60835 | 351 |
Debugger.init(PIDE.session) |
60845 | 352 |
GUI_Thread.later { |
353 |
debugger_active.load() |
|
354 |
debugger_stepping.load() |
|
355 |
handle_resize() |
|
356 |
} |
|
60749 | 357 |
|
60835 | 358 |
case _: Debugger.Update => |
60749 | 359 |
GUI_Thread.later { handle_update() } |
360 |
} |
|
361 |
||
362 |
override def init() |
|
363 |
{ |
|
364 |
PIDE.session.global_options += main |
|
60835 | 365 |
PIDE.session.debugger_updates += main |
366 |
Debugger.init(PIDE.session) |
|
60876 | 367 |
Debugger.inc_active() |
60749 | 368 |
handle_update() |
60876 | 369 |
jEdit.propertiesChanged() |
60749 | 370 |
} |
371 |
||
372 |
override def exit() |
|
373 |
{ |
|
374 |
PIDE.session.global_options -= main |
|
60835 | 375 |
PIDE.session.debugger_updates -= main |
60749 | 376 |
delay_resize.revoke() |
60875 | 377 |
update_focus(None) |
60876 | 378 |
Debugger.dec_active() |
379 |
jEdit.propertiesChanged() |
|
60749 | 380 |
} |
381 |
||
382 |
||
383 |
/* resize */ |
|
384 |
||
385 |
private val delay_resize = |
|
386 |
GUI_Thread.delay_first(PIDE.options.seconds("editor_update_delay")) { handle_resize() } |
|
387 |
||
388 |
addComponentListener(new ComponentAdapter { |
|
389 |
override def componentResized(e: ComponentEvent) { delay_resize.invoke() } |
|
60750 | 390 |
override def componentShown(e: ComponentEvent) { delay_resize.invoke() } |
60749 | 391 |
}) |
392 |
} |