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