src/Tools/jEdit/src/debugger_dockable.scala
author wenzelm
Sat, 02 Nov 2024 15:59:58 +0100
changeset 81314 fad1278d0f5b
parent 81313 5f28bded8d7d
child 81315 b796e59ec3ef
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/jEdit/src/debugger_dockable.scala
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     3
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     4
Dockable window for Isabelle/ML debugger.
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     5
*/
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     6
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     8
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
     9
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    10
import isabelle._
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    11
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    12
import java.awt.{BorderLayout, Dimension}
61007
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
    13
import java.awt.event.{ComponentEvent, ComponentAdapter, KeyEvent, FocusAdapter, FocusEvent,
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
    14
  MouseEvent, MouseAdapter}
60883
8eb8640d7300 more uniform ScrollPane, like graphview;
wenzelm
parents: 60880
diff changeset
    15
import javax.swing.{JTree, JMenuItem}
60850
d5d776c8a7e2 proper dynamic update;
wenzelm
parents: 60849
diff changeset
    16
import javax.swing.tree.{DefaultMutableTreeNode, DefaultTreeModel, TreeSelectionModel}
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    17
import javax.swing.event.{TreeSelectionEvent, TreeSelectionListener}
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
    18
61016
wenzelm
parents: 61015
diff changeset
    19
import scala.collection.immutable.SortedMap
75852
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
    20
import scala.swing.{Button, Label, Component, ScrollPane, SplitPane, Orientation, BorderPanel}
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
    21
import scala.swing.event.ButtonClicked
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    22
60875
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
    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
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    26
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    27
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    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
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    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
c3b42ac0cf81 tuned signature;
wenzelm
parents: 61018
diff changeset
    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
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
    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
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    53
}
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    54
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
    55
class Debugger_Dockable(view: View, position: String) extends Dockable(view, position) {
76605
77805bdabc8e clarified signature: more robust;
wenzelm
parents: 75854
diff changeset
    56
  dockable =>
77805bdabc8e clarified signature: more robust;
wenzelm
parents: 75854
diff changeset
    57
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    58
  GUI_Thread.require {}
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    59
65247
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
    60
  private val debugger = PIDE.session.debugger
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
    61
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    62
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    63
  /* component state -- owned by GUI thread */
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    64
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    65
  private var current_snapshot = Document.Snapshot.init
61016
wenzelm
parents: 61015
diff changeset
    66
  private var current_threads: Debugger.Threads = SortedMap.empty
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    67
  private var current_output: List[XML.Tree] = Nil
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    68
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
    69
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    70
  /* pretty text area */
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    71
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    72
  private val tree_text_area: Tree_Text_Area =
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    73
    new Tree_Text_Area(view, root_name = "Threads") {
81313
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    74
      override def handle_tree_selection(e: TreeSelectionEvent): Unit = {
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    75
        update_focus()
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    76
        update_vals()
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    77
      }
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    78
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    79
      override def handle_resize(): Unit =
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    80
        GUI_Thread.require { pretty_text_area.zoom(zoom) }
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    81
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    82
      override def handle_update(): Unit = {
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    83
        GUI_Thread.require {}
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    84
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    85
        val new_snapshot = PIDE.editor.current_node_snapshot(view).getOrElse(current_snapshot)
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    86
        val (new_threads, new_output) = debugger.status(tree_selection())
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    87
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    88
        if (new_threads != current_threads) update_tree(new_threads)
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    89
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    90
        if (new_output != current_output) {
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    91
          pretty_text_area.update(new_snapshot, Command.Results.empty, Pretty.separate(new_output))
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    92
        }
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
    93
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    94
        current_snapshot = new_snapshot
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    95
        current_threads = new_threads
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    96
        current_output = new_output
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
    97
      }
81310
e3b0c7aec1ed clarified signature;
wenzelm
parents: 81309
diff changeset
    98
81313
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
    99
      override def handle_focus(): Unit = update_focus()
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   100
    }
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   101
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   102
  override def detach_operation: Option[() => Unit] =
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   103
    tree_text_area.pretty_text_area.detach_operation
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   104
81314
fad1278d0f5b clarified signature;
wenzelm
parents: 81313
diff changeset
   105
  tree_text_area.init_gui(dockable)
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   106
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   107
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   108
  /* tree view */
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   109
81313
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
   110
  private def tree: JTree = tree_text_area.tree
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   111
81313
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
   112
  private def tree_selection(): Option[Debugger.Context] =
81310
e3b0c7aec1ed clarified signature;
wenzelm
parents: 81309
diff changeset
   113
    tree_text_area.get_tree_selection({ case c: Debugger.Context => c })
60875
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   114
81313
5f28bded8d7d clarified signature;
wenzelm
parents: 81312
diff changeset
   115
  private def thread_selection(): Option[String] = tree_selection().map(_.thread_name)
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   116
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   117
  private def update_tree(threads: Debugger.Threads): Unit = {
61018
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   118
    val thread_contexts =
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   119
      (for ((a, b) <- threads.iterator)
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   120
        yield Debugger.Context(a, b)).toList
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   121
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   122
    val new_tree_selection =
61009
a9574cdd5eaf more precise tree re-selection;
wenzelm
parents: 61007
diff changeset
   123
      tree_selection() match {
61018
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   124
        case Some(c) if thread_contexts.contains(c) => Some(c)
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   125
        case Some(c) if thread_contexts.exists(t => c.thread_name == t.thread_name) =>
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   126
          Some(c.reset)
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   127
        case _ => thread_contexts.headOption
60906
6032429da70d clarified thread re-selection;
wenzelm
parents: 60905
diff changeset
   128
      }
60860
e78bdf06d33c approximate old selection after update;
wenzelm
parents: 60859
diff changeset
   129
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   130
    tree_text_area.clear()
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   131
61018
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   132
    for (thread <- thread_contexts) {
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   133
      val thread_node = new DefaultMutableTreeNode(thread)
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   134
      for ((_, i) <- thread.debug_states.zipWithIndex)
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   135
        thread_node.add(new DefaultMutableTreeNode(thread.select(i)))
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   136
      tree_text_area.root.add(thread_node)
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   137
    }
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   138
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   139
    tree_text_area.reload()
60860
e78bdf06d33c approximate old selection after update;
wenzelm
parents: 60859
diff changeset
   140
60905
eba3acb72b55 clarified tree row handling;
wenzelm
parents: 60901
diff changeset
   141
    tree.expandRow(0)
eba3acb72b55 clarified tree row handling;
wenzelm
parents: 60901
diff changeset
   142
    for (i <- Range.inclusive(tree.getRowCount - 1, 1, -1)) tree.expandRow(i)
eba3acb72b55 clarified tree row handling;
wenzelm
parents: 60901
diff changeset
   143
61009
a9574cdd5eaf more precise tree re-selection;
wenzelm
parents: 61007
diff changeset
   144
    new_tree_selection match {
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   145
      case Some(c) =>
60860
e78bdf06d33c approximate old selection after update;
wenzelm
parents: 60859
diff changeset
   146
        val i =
61018
32cc7d219c38 atomic Debugger.status;
wenzelm
parents: 61016
diff changeset
   147
          (for (t <- thread_contexts.iterator.takeWhile(t => c.thread_name != t.thread_name))
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   148
            yield t.size).sum
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   149
        tree.addSelectionRow(i + c.index + 1)
60906
6032429da70d clarified thread re-selection;
wenzelm
parents: 60905
diff changeset
   150
      case None =>
60860
e78bdf06d33c approximate old selection after update;
wenzelm
parents: 60859
diff changeset
   151
    }
e78bdf06d33c approximate old selection after update;
wenzelm
parents: 60859
diff changeset
   152
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   153
    tree.revalidate()
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   154
  }
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   155
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   156
  def update_vals(): Unit = {
60901
ce8abd005c5d clarified GUI event handling;
wenzelm
parents: 60900
diff changeset
   157
    tree_selection() match {
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   158
      case Some(c) if c.stack_state.isDefined =>
65247
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
   159
        debugger.print_vals(c, sml_button.selected, context_field.getText)
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   160
      case Some(c) =>
65247
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
   161
        debugger.clear_output(c.thread_name)
61010
cccfd7f6317d more explicit type Debugger.Context;
wenzelm
parents: 61009
diff changeset
   162
      case None =>
60901
ce8abd005c5d clarified GUI event handling;
wenzelm
parents: 60900
diff changeset
   163
    }
ce8abd005c5d clarified GUI event handling;
wenzelm
parents: 60900
diff changeset
   164
  }
ce8abd005c5d clarified GUI event handling;
wenzelm
parents: 60900
diff changeset
   165
61007
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
   166
  tree.addMouseListener(
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
   167
    new MouseAdapter {
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   168
      override def mouseClicked(e: MouseEvent): Unit = {
61007
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
   169
        val click = tree.getPathForLocation(e.getX, e.getY)
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   170
        if (click != null && e.getClickCount == 1) update_focus()
61007
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
   171
      }
eaceb601a8a2 update focus more thoroughly;
wenzelm
parents: 60936
diff changeset
   172
    })
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   173
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   174
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   175
  /* controls */
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   176
75854
2163772eeaf2 tuned signature;
wenzelm
parents: 75853
diff changeset
   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
fcc25bb49def clarified signature;
wenzelm
parents: 75839
diff changeset
   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
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   182
  private val continue_button = new GUI.Button("Continue") {
60907
wenzelm
parents: 60906
diff changeset
   183
    tooltip = "Continue program on current thread, until next breakpoint"
81311
ea3cae90f76b tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81310
diff changeset
   184
    override def clicked(): Unit = thread_selection().foreach(debugger.continue)
60907
wenzelm
parents: 60906
diff changeset
   185
  }
wenzelm
parents: 60906
diff changeset
   186
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   187
  private val step_button = new GUI.Button("Step") {
60907
wenzelm
parents: 60906
diff changeset
   188
    tooltip = "Single-step in depth-first order"
81311
ea3cae90f76b tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81310
diff changeset
   189
    override def clicked(): Unit = thread_selection().foreach(debugger.step)
60907
wenzelm
parents: 60906
diff changeset
   190
  }
wenzelm
parents: 60906
diff changeset
   191
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   192
  private val step_over_button = new GUI.Button("Step over") {
60907
wenzelm
parents: 60906
diff changeset
   193
    tooltip = "Single-step within this function"
81311
ea3cae90f76b tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81310
diff changeset
   194
    override def clicked(): Unit = thread_selection().foreach(debugger.step_over)
60907
wenzelm
parents: 60906
diff changeset
   195
  }
wenzelm
parents: 60906
diff changeset
   196
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   197
  private val step_out_button = new GUI.Button("Step out") {
60907
wenzelm
parents: 60906
diff changeset
   198
    tooltip = "Single-step outside this function"
81311
ea3cae90f76b tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 81310
diff changeset
   199
    override def clicked(): Unit = thread_selection().foreach(debugger.step_out)
60907
wenzelm
parents: 60906
diff changeset
   200
  }
wenzelm
parents: 60906
diff changeset
   201
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   202
  private val context_label = new Label("Context:") {
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   203
    tooltip = "Isabelle/ML context: type theory, Proof.context, Context.generic"
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   204
  }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   205
  private val context_field =
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   206
    new Completion_Popup.History_Text_Field("isabelle-debugger-context") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   207
      override def processKeyEvent(evt: KeyEvent): Unit = {
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   208
        if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) {
60936
2751f7f31be2 tuned GUI;
wenzelm
parents: 60932
diff changeset
   209
          eval_expression()
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   210
        }
60936
2751f7f31be2 tuned GUI;
wenzelm
parents: 60932
diff changeset
   211
        super.processKeyEvent(evt)
2751f7f31be2 tuned GUI;
wenzelm
parents: 60932
diff changeset
   212
      }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   213
      setColumns(20)
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   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
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   216
    }
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   217
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   218
  private val expression_label = new Label("ML:") {
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   219
    tooltip = "Isabelle/ML or Standard ML expression"
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   220
  }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   221
  private val expression_field =
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   222
    new Completion_Popup.History_Text_Field("isabelle-debugger-expression") {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   223
      override def processKeyEvent(evt: KeyEvent): Unit = {
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   224
        if (evt.getID == KeyEvent.KEY_PRESSED && evt.getKeyCode == KeyEvent.VK_ENTER) {
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   225
          eval_expression()
75814
15951587f171 tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75812
diff changeset
   226
        }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   227
        super.processKeyEvent(evt)
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   228
      }
78243
0e221a8128e4 tuned: prefer Scala over Java;
wenzelm
parents: 76765
diff changeset
   229
      { val max = getPreferredSize; max.width = Int.MaxValue; setMaximumSize(max) }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   230
      setColumns(40)
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   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
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   233
    }
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   234
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   235
  private val eval_button =
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   236
    new GUI.Button("<html><b>Eval</b></html>") {
60862
097afdd8a2fd eval ML context;
wenzelm
parents: 60861
diff changeset
   237
      tooltip = "Evaluate ML expression within optional context"
75853
f981111768ec clarified signature;
wenzelm
parents: 75852
diff changeset
   238
      override def clicked(): Unit = eval_expression()
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   239
    }
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   240
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   241
  private def eval_expression(): Unit = {
60861
fa77faa87d5f maintain history more often;
wenzelm
parents: 60860
diff changeset
   242
    context_field.addCurrentToHistory()
fa77faa87d5f maintain history more often;
wenzelm
parents: 60860
diff changeset
   243
    expression_field.addCurrentToHistory()
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60854
diff changeset
   244
    tree_selection() match {
61014
39f67bb4e609 maintain per-thread focus context;
wenzelm
parents: 61011
diff changeset
   245
      case Some(c) if c.debug_index.isDefined =>
65247
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
   246
        debugger.eval(c, sml_button.selected, context_field.getText, expression_field.getText)
60856
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60854
diff changeset
   247
      case _ =>
eb21ae05ec43 clarified thread state;
wenzelm
parents: 60854
diff changeset
   248
    }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   249
  }
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   250
75854
2163772eeaf2 tuned signature;
wenzelm
parents: 75853
diff changeset
   251
  private val sml_button = new GUI.Check("SML") {
60907
wenzelm
parents: 60906
diff changeset
   252
    tooltip = "Official Standard ML instead of Isabelle/ML"
60869
878e32cf3131 more single stepping;
wenzelm
parents: 60862
diff changeset
   253
  }
878e32cf3131 more single stepping;
wenzelm
parents: 60862
diff changeset
   254
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   255
  private val zoom =
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   256
    new Font_Info.Zoom { override def changed(): Unit = tree_text_area.handle_resize() }
60832
1cdc63224ed3 more GUI components;
wenzelm
parents: 60750
diff changeset
   257
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   258
  private val controls =
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   259
    Wrap_Panel(
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   260
      List(
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   261
        break_button, continue_button, step_button, step_over_button, step_out_button,
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   262
        context_label, Component.wrap(context_field),
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   263
        expression_label, Component.wrap(expression_field), eval_button, sml_button,
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   264
        tree_text_area.pretty_text_area.search_label,
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   265
        tree_text_area.pretty_text_area.search_field, zoom))
66205
e9fa94f43a15 tuned signature;
wenzelm
parents: 66082
diff changeset
   266
60848
7ec20b1c8dc9 more GUI components;
wenzelm
parents: 60845
diff changeset
   267
  add(controls.peer, BorderLayout.NORTH)
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   268
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   269
60875
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   270
  /* focus */
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   271
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   272
  override def focusOnDefaultComponent(): Unit = eval_button.requestFocus()
60875
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   273
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   274
  private def update_focus(): Unit = {
61014
39f67bb4e609 maintain per-thread focus context;
wenzelm
parents: 61011
diff changeset
   275
    for (c <- tree_selection()) {
65247
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
   276
      debugger.set_focus(c)
61014
39f67bb4e609 maintain per-thread focus context;
wenzelm
parents: 61011
diff changeset
   277
      for {
39f67bb4e609 maintain per-thread focus context;
wenzelm
parents: 61011
diff changeset
   278
        pos <- c.debug_position
66082
2d12a730a380 clarified modules;
wenzelm
parents: 65247
diff changeset
   279
        link <- PIDE.editor.hyperlink_position(false, current_snapshot, pos)
61015
2c34ab15e3eb more thorough GUI update;
wenzelm
parents: 61014
diff changeset
   280
      } link.follow(view)
61014
39f67bb4e609 maintain per-thread focus context;
wenzelm
parents: 61011
diff changeset
   281
    }
61015
2c34ab15e3eb more thorough GUI update;
wenzelm
parents: 61014
diff changeset
   282
    JEdit_Lib.jedit_text_areas(view.getBuffer).foreach(_.repaint())
60875
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   283
  }
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   284
ee23c1d21ac3 follow debugger focus;
wenzelm
parents: 60869
diff changeset
   285
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   286
  /* main */
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   287
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   288
  private val main =
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   289
    Session.Consumer[Any](getClass.getName) {
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   290
      case _: Session.Global_Options =>
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   291
        GUI_Thread.later { tree_text_area.handle_resize() }
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   292
60900
11a0f333de6f tuned signature;
wenzelm
parents: 60899
diff changeset
   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
63d91d5de121 tuned signature;
wenzelm
parents: 65246
diff changeset
   295
          break_button.selected = debugger.is_break()
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   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
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   298
    }
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   299
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   300
  override def init(): Unit = {
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   301
    PIDE.session.global_options += main
60835
6512bb0b1ff4 clarified management of (single) session;
wenzelm
parents: 60834
diff changeset
   302
    PIDE.session.debugger_updates += main
76605
77805bdabc8e clarified signature: more robust;
wenzelm
parents: 75854
diff changeset
   303
    debugger.init(dockable)
81309
ccdbe1b538fc clarified modules: more re-usable;
wenzelm
parents: 78243
diff changeset
   304
    tree_text_area.handle_update()
60876
52edced9cce5 rendering for debugger/breakpoint active state;
wenzelm
parents: 60875
diff changeset
   305
    jEdit.propertiesChanged()
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   306
  }
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   307
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73987
diff changeset
   308
  override def exit(): Unit = {
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   309
    PIDE.session.global_options -= main
60835
6512bb0b1ff4 clarified management of (single) session;
wenzelm
parents: 60834
diff changeset
   310
    PIDE.session.debugger_updates -= main
81312
a00d4d50b851 clarified signature;
wenzelm
parents: 81311
diff changeset
   311
    tree_text_area.delay_resize.revoke()
76605
77805bdabc8e clarified signature: more robust;
wenzelm
parents: 75854
diff changeset
   312
    debugger.exit(dockable)
60876
52edced9cce5 rendering for debugger/breakpoint active state;
wenzelm
parents: 60875
diff changeset
   313
    jEdit.propertiesChanged()
60749
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   314
  }
f727b99faaf7 skeleton for interactive debugger;
wenzelm
parents:
diff changeset
   315
}