src/Tools/jEdit/src/main_plugin.scala
author wenzelm
Fri, 04 Apr 2025 20:34:13 +0200
changeset 82436 e48b3ddc4810
parent 82413 a6046b6d23b4
permissions -rw-r--r--
clarified navigator events: avoid excessive 0 positions;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73987
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73947
diff changeset
     1
/*  Title:      Tools/jEdit/src/main_plugin.scala
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     2
    Author:     Makarius
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     3
50362
wenzelm
parents: 50344
diff changeset
     4
Main plumbing for PIDE infrastructure as jEdit plugin.
36760
b82a698ef6c9 tuned headers;
wenzelm
parents: 36015
diff changeset
     5
*/
34407
aad6834ba380 added some headers and comments;
wenzelm
parents: 34406
diff changeset
     6
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     7
package isabelle.jedit
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
     8
34429
wenzelm
parents: 34422
diff changeset
     9
36015
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    10
import isabelle._
6111de7c916a adapted to Scala 2.8.0 Beta 1;
wenzelm
parents: 34871
diff changeset
    11
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
    12
import javax.swing.JOptionPane
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    13
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
    14
import java.io.{File => JFile}
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
    15
66593
d389714a8aaa clarified startup sequence;
wenzelm
parents: 66590
diff changeset
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, PerspectiveManager}
65248
wenzelm
parents: 65247
diff changeset
    17
import org.gjt.sp.jedit.textarea.JEditTextArea
50362
wenzelm
parents: 50344
diff changeset
    18
import org.gjt.sp.jedit.syntax.ModeProvider
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, BufferChanging, PositionChanging,
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
    20
  EditPaneUpdate, PropertiesChanged, ViewUpdate}
62228
6dfe5b12c5b2 report error on internal channel as well: startup_failure dialog may be too late;
wenzelm
parents: 62227
diff changeset
    21
import org.gjt.sp.util.Log
39241
e9a442606db3 Isabelle.load_icon with some sanity checks;
wenzelm
parents: 39167
diff changeset
    22
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    23
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
    24
object PIDE {
65248
wenzelm
parents: 65247
diff changeset
    25
  /* semantic document content */
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    26
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
    27
  def maybe_snapshot(view: View = null): Option[Document.Snapshot] = GUI_Thread.now {
69636
dd1e0e1570d2 clarified signature;
wenzelm
parents: 69458
diff changeset
    28
    val buffer = JEdit_Lib.jedit_view(view).getBuffer
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76716
diff changeset
    29
    Document_Model.get_snapshot(buffer)
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    30
  }
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    31
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
    32
  def maybe_rendering(view: View = null): Option[JEdit_Rendering] = GUI_Thread.now {
69636
dd1e0e1570d2 clarified signature;
wenzelm
parents: 69458
diff changeset
    33
    val text_area = JEdit_Lib.jedit_view(view).getTextArea
76765
c654103e9c9d more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
wenzelm
parents: 76716
diff changeset
    34
    Document_View.get_rendering(text_area)
55621
8d69c15b6fb9 added PIDE.snapshot, PIDE.rendering for convenience;
wenzelm
parents: 55618
diff changeset
    35
  }
65277
wenzelm
parents: 65275
diff changeset
    36
69759
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    37
  def snapshot(view: View = null): Document.Snapshot =
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    38
    maybe_snapshot(view) getOrElse error("No document model for current buffer")
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    39
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    40
  def rendering(view: View = null): JEdit_Rendering =
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    41
    maybe_rendering(view) getOrElse error("No document view for current text area")
092c6a4bcc26 tuned signature;
wenzelm
parents: 69636
diff changeset
    42
65277
wenzelm
parents: 65275
diff changeset
    43
wenzelm
parents: 65275
diff changeset
    44
  /* plugin instance */
wenzelm
parents: 65275
diff changeset
    45
73987
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73947
diff changeset
    46
  @volatile var _plugin: Main_Plugin = null
65277
wenzelm
parents: 65275
diff changeset
    47
73987
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73947
diff changeset
    48
  def plugin: Main_Plugin =
65277
wenzelm
parents: 65275
diff changeset
    49
    if (_plugin == null) error("Uninitialized Isabelle/jEdit plugin")
wenzelm
parents: 65275
diff changeset
    50
    else _plugin
wenzelm
parents: 65275
diff changeset
    51
wenzelm
parents: 65275
diff changeset
    52
  def options: JEdit_Options = plugin.options
wenzelm
parents: 65275
diff changeset
    53
  def resources: JEdit_Resources = plugin.resources
wenzelm
parents: 65275
diff changeset
    54
  def session: Session = plugin.session
81433
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    55
  def cache: Rich_Text.Cache = session.cache.asInstanceOf[Rich_Text.Cache]
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
    56
66083
wenzelm
parents: 66082
diff changeset
    57
  object editor extends JEdit_Editor
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    58
}
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
    59
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
    60
class Main_Plugin extends EBPlugin {
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    61
  /* options */
65238
02037d14cb42 more robust early initialization;
wenzelm
parents: 65236
diff changeset
    62
82321
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    63
  private lazy val initial_options: Options = Options.init()
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    64
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    65
  private lazy val more_options: List[Options.Spec] =
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    66
    Library.space_explode('\u000b', Isabelle_System.getenv("JEDIT_ISABELLE_OPTIONS"))
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    67
      .map(Options.Spec.make)
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    68
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
    69
  private var _options: JEdit_Options = null
82321
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    70
  private def init_options(): Unit = {
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    71
    _options = new JEdit_Options(initial_options ++ more_options)
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
    72
  }
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
    73
  def options: JEdit_Options = _options
65239
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    74
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    75
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    76
  /* resources */
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    77
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    78
  private var _resources: JEdit_Resources = null
66572
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
    79
  private def init_resources(): Unit = _resources = JEdit_Resources(options.value)
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    80
  def resources: JEdit_Resources = _resources
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    81
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    82
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    83
  /* session */
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    84
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    85
  private var _session: Session = null
81433
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    86
  private def init_session(): Unit = {
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    87
    _session =
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    88
      new Session(options.value, resources) {
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    89
        override val cache: Term.Cache = Rich_Text.Cache.make()
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    90
      }
c3793899b880 performance tuning: cache for Rich_Text.format, notably for incremental tracing;
wenzelm
parents: 79717
diff changeset
    91
  }
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    92
  def session: Session = _session
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    93
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
    94
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    95
  /* misc support */
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
    96
65239
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    97
  val completion_history = new Completion.History_Variable
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
    98
  val spell_checker = new Spell_Checker_Variable
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
    99
  val navigator = new Isabelle_Navigator
65239
509a9b0ad02e avoid global variables with implicit initialization;
wenzelm
parents: 65238
diff changeset
   100
65238
02037d14cb42 more robust early initialization;
wenzelm
parents: 65236
diff changeset
   101
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents: 44574
diff changeset
   102
  /* theory files */
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   103
76704
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   104
  private lazy val delay_init: Delay =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76601
diff changeset
   105
    Delay.last(PIDE.session.load_delay, gui = true) { init_models() }
50344
608265769ce0 emit bulk edits on initialization of multiple buffers, which greatly improves performance when starting big sessions (e.g. JinjaThreads);
wenzelm
parents: 50209
diff changeset
   106
76704
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   107
  private lazy val delay_load: Delay =
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   108
    Delay.last(session.load_delay, gui = true) {
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   109
      if (JEdit_Options.continuous_checking()) {
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   110
        if (!PerspectiveManager.isPerspectiveEnabled ||
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   111
            JEdit_Lib.jedit_buffers().exists(_.isLoading)) delay_load.invoke()
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   112
        else if (delay_load_activated()) delay_load_body()
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   113
        else delay_load.invoke()
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   114
      }
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   115
    }
26f939b23fdd clarified module initialization;
wenzelm
parents: 76666
diff changeset
   116
76706
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76704
diff changeset
   117
  def deps_changed(): Unit = delay_load.invoke()
3d4358e01646 clarified state change: presumably more robust;
wenzelm
parents: 76704
diff changeset
   118
57582
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   119
  private val delay_load_active = Synchronized(false)
76584
017384868fcb tuned signature;
wenzelm
parents: 76488
diff changeset
   120
  private def delay_load_finished(): Unit = delay_load_active.change(_ => false)
57582
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   121
  private def delay_load_activated(): Boolean =
b79b75f92604 avoid delay_load overrun;
wenzelm
parents: 56834
diff changeset
   122
    delay_load_active.guarded_access(a => Some((!a, true)))
76584
017384868fcb tuned signature;
wenzelm
parents: 76488
diff changeset
   123
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   124
  private def delay_load_body(): Unit = {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   125
    val required_files = {
76777
7cf938666641 tuned signature;
wenzelm
parents: 76768
diff changeset
   126
      val models = Document_Model.get_models_map()
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   127
76716
a7602257a825 clarified state document nodes for Theories_Status / Document_Dockable;
wenzelm
parents: 76706
diff changeset
   128
      val thy_files =
76767
540cd80c5af2 tuned signature;
wenzelm
parents: 76766
diff changeset
   129
        resources.resolve_dependencies(models.values, PIDE.editor.document_required())
67290
98b6cd12f963 implicit thy_load context for bibtex files;
wenzelm
parents: 67059
diff changeset
   130
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   131
      val aux_files =
76601
wenzelm
parents: 76590
diff changeset
   132
        if (resources.auto_resolve) {
76766
235de80d4b25 tuned signature;
wenzelm
parents: 76765
diff changeset
   133
          session.stable_tip_version(models.values) match {
76590
3fc3c7c285cd clarified signature;
wenzelm
parents: 76587
diff changeset
   134
            case Some(version) => resources.undefined_blobs(version)
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   135
            case None => delay_load.invoke(); Nil
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   136
          }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   137
        }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   138
        else Nil
46761
b0a797158e34 avoid buffer loading overrun;
wenzelm
parents: 46740
diff changeset
   139
76777
7cf938666641 tuned signature;
wenzelm
parents: 76768
diff changeset
   140
      (thy_files ::: aux_files).filterNot(models.keySet)
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   141
    }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   142
    if (required_files.nonEmpty) {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   143
      try {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   144
        Isabelle_Thread.fork(name = "resolve_dependencies") {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   145
          val loaded_files =
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   146
            for {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   147
              name <- required_files
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   148
              text <- resources.read_file_content(name)
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   149
            } yield (name, text)
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   150
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   151
          GUI_Thread.later {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   152
            try {
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   153
              Document_Model.provide_files(session, loaded_files)
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   154
              delay_init.invoke()
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   155
            }
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   156
            finally { delay_load_finished() }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   157
          }
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64818
diff changeset
   158
        }
76585
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   159
      }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   160
      catch { case _: Throwable => delay_load_finished() }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   161
    }
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   162
    else delay_load_finished()
1b7bb4f8c0f4 potentially more robust delay_load action: avoid loosing events due to guards;
wenzelm
parents: 76584
diff changeset
   163
  }
62068
500f54190a3c more robust event propagation;
wenzelm
parents: 62062
diff changeset
   164
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   165
  private def file_watcher_action(changed: Set[JFile]): Unit =
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   166
    if (Document_Model.sync_files(changed)) PIDE.editor.invoke_generated()
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   167
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   168
  lazy val file_watcher: File_Watcher =
76610
6e2383488a55 clarified signature: proper scopes and types;
wenzelm
parents: 76601
diff changeset
   169
    File_Watcher(file_watcher_action, session.load_delay)
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
   170
44574
24444588fddd actual auto loading of required files;
wenzelm
parents: 44573
diff changeset
   171
56715
52125652e82a clarified Session.Consumer, with Session.Outlet managed by dispatcher thread;
wenzelm
parents: 56662
diff changeset
   172
  /* session phase */
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
   173
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   174
  val session_phase_changed: Session.Consumer[Session.Phase] = Session.Consumer("Isabelle/jEdit") {
65317
b9f5cd845616 more informative session result;
wenzelm
parents: 65277
diff changeset
   175
    case Session.Terminated(result) if !result.ok =>
65226
wenzelm
parents: 65222
diff changeset
   176
      GUI_Thread.later {
65267
7e427dff15dd clarified message;
wenzelm
parents: 65266
diff changeset
   177
        GUI.error_dialog(jEdit.getActiveView, "Prover process terminated with error",
76488
1eed7e1300ed clarified signature: more public operations;
wenzelm
parents: 75849
diff changeset
   178
          "Isabelle Syslog", GUI.scrollable_text(session.syslog.content()))
65226
wenzelm
parents: 65222
diff changeset
   179
      }
39735
969ede84aac0 more uniform init/exit model/view in session_manager, trading race wrt. session.phase for race wrt. global editor state;
wenzelm
parents: 39702
diff changeset
   180
71471
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   181
    case Session.Ready if !shutting_down.value =>
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   182
      init_models()
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   183
75849
dfedac6525d4 clarified modules;
wenzelm
parents: 75848
diff changeset
   184
      if (!JEdit_Options.continuous_checking()) {
65226
wenzelm
parents: 65222
diff changeset
   185
        GUI_Thread.later {
wenzelm
parents: 65222
diff changeset
   186
          val answer =
wenzelm
parents: 65222
diff changeset
   187
            GUI.confirm_dialog(jEdit.getActiveView,
wenzelm
parents: 65222
diff changeset
   188
              "Continuous checking of PIDE document",
wenzelm
parents: 65222
diff changeset
   189
              JOptionPane.YES_NO_OPTION,
wenzelm
parents: 65222
diff changeset
   190
              "Continuous checking is presently disabled:",
wenzelm
parents: 65222
diff changeset
   191
              "editor buffers will remain inactive!",
wenzelm
parents: 65222
diff changeset
   192
              "Enable continuous checking now?")
75849
dfedac6525d4 clarified modules;
wenzelm
parents: 75848
diff changeset
   193
          if (answer == 0) JEdit_Options.continuous_checking.set()
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   194
        }
65226
wenzelm
parents: 65222
diff changeset
   195
      }
57640
0a28cf866d5d reconfirm continuous checking on startup, to address common trap of disabling it accidentally;
wenzelm
parents: 57612
diff changeset
   196
65226
wenzelm
parents: 65222
diff changeset
   197
      delay_load.invoke()
44573
51f8895b9ad9 some dialog for auto loading of required files (still inactive);
wenzelm
parents: 44434
diff changeset
   198
65226
wenzelm
parents: 65222
diff changeset
   199
    case Session.Shutdown =>
wenzelm
parents: 65222
diff changeset
   200
      GUI_Thread.later {
wenzelm
parents: 65222
diff changeset
   201
        delay_load.revoke()
wenzelm
parents: 65222
diff changeset
   202
        delay_init.revoke()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66351
diff changeset
   203
        PIDE.editor.shutdown()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   204
        exit_models(JEdit_Lib.jedit_buffers().toList)
65226
wenzelm
parents: 65222
diff changeset
   205
      }
46740
852baa599351 explicitly revoke delay, to avoid spurious timer events after deactivation of related components;
wenzelm
parents: 46204
diff changeset
   206
65226
wenzelm
parents: 65222
diff changeset
   207
    case _ =>
wenzelm
parents: 65222
diff changeset
   208
  }
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   209
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   210
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   211
  /* document model and view */
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   212
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   213
  def exit_models(buffers: List[Buffer]): Unit = {
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   214
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   215
      buffers.foreach(buffer =>
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   216
        JEdit_Lib.buffer_lock(buffer) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   217
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   218
          Document_Model.exit(buffer)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   219
        })
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   220
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   221
  }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   222
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   223
  def init_models(): Unit = {
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   224
    GUI_Thread.now {
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   225
      PIDE.editor.flush()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   226
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   227
      for {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   228
        buffer <- JEdit_Lib.jedit_buffers()
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   229
        if buffer != null && !buffer.getBooleanProperty(Buffer.GZIPPED)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   230
      } {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   231
        if (buffer.isLoaded) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   232
          JEdit_Lib.buffer_lock(buffer) {
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   233
            val node_name = resources.node_name(buffer)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   234
            val model = Document_Model.init(session, node_name, buffer)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   235
            for {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   236
              text_area <- JEdit_Lib.jedit_text_areas(buffer)
75751
204b97d05abe tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75393
diff changeset
   237
              if !Document_View.get(text_area).map(_.model).contains(model)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   238
            } Document_View.init(model, text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   239
          }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   240
        }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   241
        else delay_init.invoke()
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   242
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   243
66082
2d12a730a380 clarified modules;
wenzelm
parents: 66019
diff changeset
   244
      PIDE.editor.invoke_generated()
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   245
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   246
  }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   247
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   248
  def init_view(buffer: Buffer, text_area: JEditTextArea): Unit =
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   249
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   250
      JEdit_Lib.buffer_lock(buffer) {
76768
40c8275f0131 tuned signature: follow terminology of VSCode_Resources;
wenzelm
parents: 76767
diff changeset
   251
        Document_Model.get_model(buffer) match {
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   252
          case Some(model) => Document_View.init(model, text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   253
          case None =>
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   254
        }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   255
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   256
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   257
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   258
  def exit_view(buffer: Buffer, text_area: JEditTextArea): Unit =
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   259
    GUI_Thread.now {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   260
      JEdit_Lib.buffer_lock(buffer) {
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   261
        Document_View.exit(text_area)
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   262
      }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   263
    }
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   264
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   265
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   266
  /* main plugin plumbing */
34433
3da749b53842 renamed Plugin.plugin to Plugin.self;
wenzelm
parents: 34429
diff changeset
   267
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   268
  @volatile private var startup_failure: Option[Throwable] = None
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   269
  @volatile private var startup_notified = false
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   270
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   271
  private def init_editor(view: View): Unit = {
71725
c255ed582095 proper asynchronous GUI interaction for somewhat heavy JEdit_Sessions.session_build check;
wenzelm
parents: 71724
diff changeset
   272
    Keymap_Merge.check_dialog(view)
66459
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   273
    Session_Build.check_dialog(view)
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   274
  }
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   275
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   276
  private def init_title(view: View): Unit = {
68080
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   277
    val title =
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   278
      proper_string(Isabelle_System.getenv("ISABELLE_IDENTIFIER")).getOrElse("Isabelle") +
75752
a0253e471aa4 clarified signature;
wenzelm
parents: 75751
diff changeset
   279
        "/" + PIDE.resources.session_base.session_name
68080
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   280
    val marker = "\u200B"
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   281
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   282
    val old_title = view.getViewConfig.title
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   283
    if (old_title == null || old_title.startsWith(marker)) {
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   284
      view.setUserTitle(marker + title)
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   285
    }
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   286
  }
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   287
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   288
  override def handleMessage(message: EBMessage): Unit = {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 57611
diff changeset
   289
    GUI_Thread.assert {}
39630
44181423183a explicit Session.Phase indication with associated event bus;
wenzelm
parents: 39628
diff changeset
   290
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   291
    if (startup_failure.isDefined && !startup_notified) {
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   292
      message match {
75751
204b97d05abe tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75393
diff changeset
   293
        case _: EditorStarted =>
51616
949e2cf02a3d tuned signature -- concentrate GUI tools;
wenzelm
parents: 51614
diff changeset
   294
          GUI.error_dialog(null, "Isabelle plugin startup failure",
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   295
            GUI.scrollable_text(Exn.message(startup_failure.get)),
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   296
            "Prover IDE inactive!")
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   297
          startup_notified = true
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   298
        case _ =>
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   299
      }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   300
    }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   301
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   302
    if (startup_failure.isEmpty) {
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   303
      message match {
75751
204b97d05abe tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75393
diff changeset
   304
        case _: EditorStarted =>
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   305
          val view = jEdit.getActiveView
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   306
76666
981801179bc5 clarified signature;
wenzelm
parents: 76610
diff changeset
   307
          try { resources.session_background.check_errors }
981801179bc5 clarified signature;
wenzelm
parents: 76610
diff changeset
   308
          catch {
981801179bc5 clarified signature;
wenzelm
parents: 76610
diff changeset
   309
            case ERROR(msg) =>
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   310
              GUI.warning_dialog(view,
76666
981801179bc5 clarified signature;
wenzelm
parents: 76610
diff changeset
   311
                "Bad session structure: may cause problems with theory imports",
981801179bc5 clarified signature;
wenzelm
parents: 76610
diff changeset
   312
                GUI.scrollable_text(msg))
66572
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   313
          }
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66459
diff changeset
   314
66602
180b2e72601f more thorough change of syntax style extender: jEdit.propertiesChanged invalidates buffer chunk cache;
wenzelm
parents: 66593
diff changeset
   315
          jEdit.propertiesChanged()
180b2e72601f more thorough change of syntax style extender: jEdit.propertiesChanged invalidates buffer chunk cache;
wenzelm
parents: 66593
diff changeset
   316
82404
47c4ea946fc8 more robust;
wenzelm
parents: 82403
diff changeset
   317
          if (view != null) {
47c4ea946fc8 more robust;
wenzelm
parents: 82403
diff changeset
   318
            init_editor(view)
64602
8edca3465758 added isabelle jedit -R;
wenzelm
parents: 64524
diff changeset
   319
82404
47c4ea946fc8 more robust;
wenzelm
parents: 82403
diff changeset
   320
            PIDE.editor.hyperlink_position(true, Document.Snapshot.init,
47c4ea946fc8 more robust;
wenzelm
parents: 82403
diff changeset
   321
              JEdit_Sessions.logic_root(options.value)).foreach(_.follow(view))
47c4ea946fc8 more robust;
wenzelm
parents: 82403
diff changeset
   322
          }
61277
c9152a195899 build session within running jEdit;
wenzelm
parents: 61024
diff changeset
   323
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   324
        case msg: ViewUpdate =>
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   325
          val what = msg.getWhat
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   326
          val view = msg.getView
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   327
          what match {
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   328
            case ViewUpdate.CREATED if view != null => init_title(view)
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   329
            case _ =>
64838
ae6c51dcba9c avoid immediate editor.flush on buffer events;
wenzelm
parents: 64835
diff changeset
   330
          }
57611
b6256ea3b7c5 proper change of perspective for removed nodes (stemming from closed buffers);
wenzelm
parents: 57582
diff changeset
   331
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   332
        case msg: BufferUpdate =>
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   333
          val what = msg.getWhat
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   334
          val buffer = msg.getBuffer
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   335
          val view = msg.getView
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   336
          val view_edit_pane = if (view == null) null else view.getEditPane
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   337
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   338
          what match {
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   339
            case BufferUpdate.LOAD_STARTED | BufferUpdate.CLOSING if buffer != null =>
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   340
              exit_models(List(buffer))
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   341
              PIDE.editor.invoke_generated()
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   342
            case BufferUpdate.PROPERTIES_CHANGED | BufferUpdate.LOADED if session.is_ready =>
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   343
              delay_init.invoke()
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   344
              delay_load.invoke()
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   345
            case _ =>
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   346
          }
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   347
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   348
          if (buffer != null && !buffer.isUntitled) {
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   349
            what match {
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   350
              case BufferUpdate.CREATED => navigator.init(Set(buffer))
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   351
              case BufferUpdate.CLOSED => navigator.exit(Set(buffer))
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   352
              case _ =>
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   353
            }
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   354
          }
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   355
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   356
        case msg: EditPaneUpdate =>
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   357
          val what = msg.getWhat
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   358
          val edit_pane = msg.getEditPane
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   359
          val buffer = if (edit_pane == null) null else edit_pane.getBuffer
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   360
          val text_area = if (edit_pane == null) null else edit_pane.getTextArea
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   361
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   362
          if (buffer != null && text_area != null) {
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   363
            if (what == EditPaneUpdate.BUFFER_CHANGED || what == EditPaneUpdate.CREATED) {
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   364
              if (session.is_ready) init_view(buffer, text_area)
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   365
            }
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   366
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   367
            if (what == EditPaneUpdate.BUFFER_CHANGING || what == EditPaneUpdate.DESTROYED) {
65240
fe5a96240749 clarified modules;
wenzelm
parents: 65239
diff changeset
   368
              Isabelle.dismissed_popups(text_area.getView)
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   369
              exit_view(buffer, text_area)
52867
8d8cb75ab20a more central Pretty_Tooltip.dismissed_all -- avoid spurious crash of Rich_Text_Area.robust_body in asynchronous mouse_motion_listener;
wenzelm
parents: 52816
diff changeset
   370
            }
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   371
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   372
            if (what == EditPaneUpdate.CREATED) Completion_Popup.Text_Area.init(text_area)
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   373
82409
8f83f798d467 misc tuning: more modular message dispatch;
wenzelm
parents: 82404
diff changeset
   374
            if (what == EditPaneUpdate.DESTROYED) Completion_Popup.Text_Area.exit(text_area)
43510
17d431c92575 init/exit model/view synchronously within the swing thread -- EditBus.send in jedit-4.4.1 always runs there;
wenzelm
parents: 43487
diff changeset
   375
          }
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   376
82413
a6046b6d23b4 more accurate navigator position;
wenzelm
parents: 82410
diff changeset
   377
          if (msg.isInstanceOf[PositionChanging]) navigator.record(edit_pane)
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   378
75751
204b97d05abe tuned, following hints by IntelliJ IDEA;
wenzelm
parents: 75393
diff changeset
   379
        case _: PropertiesChanged =>
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   380
          for {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   381
            view <- JEdit_Lib.jedit_views()
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   382
            edit_pane <- JEdit_Lib.jedit_edit_panes(view)
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   383
          } {
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   384
            val buffer = edit_pane.getBuffer
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   385
            val text_area = edit_pane.getTextArea
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   386
            if (buffer != null && text_area != null) init_view(buffer, text_area)
62264
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   387
          }
340f98836fd9 re-init document views for the sake of Text_Overview size;
wenzelm
parents: 62228
diff changeset
   388
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   389
          spell_checker.update(options.value)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   390
          session.update_options(options.value)
34784
02959dcea756 split Theory_View into Document_Model (connected to Buffer) and Document_View (connected to JEditTextArea);
wenzelm
parents: 34782
diff changeset
   391
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   392
        case _ =>
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   393
      }
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   394
    }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   395
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   396
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   397
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   398
  /* mode provider */
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   399
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   400
  private var orig_mode_provider: ModeProvider = null
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   401
  private var pide_mode_provider: ModeProvider = null
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   402
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   403
  def init_mode_provider(): Unit = {
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   404
    orig_mode_provider = ModeProvider.instance
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   405
    if (orig_mode_provider.isInstanceOf[ModeProvider]) {
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   406
      pide_mode_provider = new Token_Markup.Mode_Provider(orig_mode_provider)
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   407
      ModeProvider.instance = pide_mode_provider
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   408
    }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   409
  }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   410
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   411
  def exit_mode_provider(): Unit = {
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   412
    if (ModeProvider.instance == pide_mode_provider)
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   413
      ModeProvider.instance = orig_mode_provider
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   414
  }
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   415
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   416
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   417
  /* HTTP server */
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   418
79717
da4e82434985 tuned signature;
wenzelm
parents: 76777
diff changeset
   419
  val http_root: String = "/" + UUID.random_string()
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   420
75107
7c0217c8b8a5 clarified signature;
wenzelm
parents: 74094
diff changeset
   421
  val http_server: HTTP.Server =
75145
e721880779be clarified signature;
wenzelm
parents: 75113
diff changeset
   422
    HTTP.server(services = Document_Model.Preview_Service :: HTTP.isabelle_services)
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   423
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   424
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   425
  /* start and stop */
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   426
71471
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   427
  private val shutting_down = Synchronized(false)
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   428
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   429
  override def start(): Unit = {
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   430
    /* strict initialization */
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   431
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   432
    init_options()
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   433
    init_resources()
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   434
    init_session()
65262
0fe4ebab9fdf resources are part of early/strict initialization, but session_base is permissive to avoid crash of "isabelle jedit -l BAD";
wenzelm
parents: 65261
diff changeset
   435
    PIDE._plugin = this
65257
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   436
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   437
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   438
    /* non-strict initialization */
2307b91159bb more explicit strict vs. non-strict initialization;
wenzelm
parents: 65256
diff changeset
   439
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   440
    try {
65242
wenzelm
parents: 65241
diff changeset
   441
      completion_history.load()
65249
c3ee88b9c3cb tuned signature;
wenzelm
parents: 65248
diff changeset
   442
      spell_checker.update(options.value)
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 49195
diff changeset
   443
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   444
      JEdit_Lib.jedit_views().foreach(init_title)
82410
4ca84abb16ef support for navigation, independently of Navigator plugin;
wenzelm
parents: 82409
diff changeset
   445
      navigator.init(JEdit_Lib.jedit_buffers())
68080
17f79ae49401 set view title dynamically;
wenzelm
parents: 67885
diff changeset
   446
73997
0f61cd0ce803 tuned signature;
wenzelm
parents: 73987
diff changeset
   447
      Syntax_Style.set_extender(Syntax_Style.Main_Extender)
65260
ff9cc2f38dd3 clarified initialization;
wenzelm
parents: 65258
diff changeset
   448
      init_mode_provider()
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   449
      JEdit_Lib.jedit_text_areas().foreach(Completion_Popup.Text_Area.init)
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   450
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   451
      http_server.start()
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   452
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   453
      startup_failure = None
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   454
    }
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   455
    catch {
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   456
      case exn: Throwable =>
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   457
        startup_failure = Some(exn)
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   458
        startup_notified = false
62228
6dfe5b12c5b2 report error on internal channel as well: startup_failure dialog may be too late;
wenzelm
parents: 62227
diff changeset
   459
        Log.log(Log.ERROR, this, exn)
49099
10e899bb6530 more permissive handling of plugin startup failure;
wenzelm
parents: 49098
diff changeset
   460
    }
66459
b578ef1a8b40 more robust plugin restart;
wenzelm
parents: 66458
diff changeset
   461
71471
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   462
    shutting_down.change(_ => false)
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   463
82403
6e80327eb30a tuned: fewer warnings in IntelliJ IDEA;
wenzelm
parents: 82321
diff changeset
   464
    val view = jEdit.getActiveView
71724
522994a6c10e tuned signature --- avoid confusion with init_view(buffer: Buffer, text_area: JEditTextArea);
wenzelm
parents: 71704
diff changeset
   465
    if (view != null) init_editor(view)
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   466
  }
34618
e45052ff7233 added Boolean_Property, Int_Property;
wenzelm
parents: 34615
diff changeset
   467
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 75145
diff changeset
   468
  override def stop(): Unit = {
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   469
    http_server.stop()
66019
69b5ef78fb07 HTML preview via builtin HTTP server;
wenzelm
parents: 65571
diff changeset
   470
73987
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73947
diff changeset
   471
    Syntax_Style.set_extender(Syntax_Style.Base_Extender)
fc363a3b690a build.props for isabelle.jar, including isabelle.jedit;
wenzelm
parents: 73947
diff changeset
   472
65261
034c49653a8e clarified initialization;
wenzelm
parents: 65260
diff changeset
   473
    exit_mode_provider()
73367
77ef8bef0593 clarified signature --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   474
    JEdit_Lib.jedit_text_areas().foreach(Completion_Popup.Text_Area.exit)
53274
1760c01f1c78 maintain Completion_Popup.Text_Area as client property like Document_View;
wenzelm
parents: 53272
diff changeset
   475
65241
6f00727f0d41 prefer local variables;
wenzelm
parents: 65240
diff changeset
   476
    if (startup_failure.isEmpty) {
82321
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
   477
      val save_options =
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
   478
        more_options.foldLeft(options.value) {
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
   479
          case (opts, opt) => opts + initial_options.spec(opt.name)
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
   480
        }
0811cfce1f5b support for "isabelle jedit -o OPTION";
wenzelm
parents: 82142
diff changeset
   481
      save_options.save_prefs()
65242
wenzelm
parents: 65241
diff changeset
   482
      completion_history.value.save()
53337
b3817a0e3211 sort items according to persistent history of frequency of use;
wenzelm
parents: 53277
diff changeset
   483
    }
49245
cb70157293c0 manage Isabelle/jEdit options as Isabelle/Scala options (with persistent preferences);
wenzelm
parents: 49195
diff changeset
   484
65244
1f53b9470116 clarified modules;
wenzelm
parents: 65243
diff changeset
   485
    exit_models(JEdit_Lib.jedit_buffers().toList)
71471
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   486
c06604896c3d more robust shutdown while Isabelle_Process is starting up, e.g. quit after dialog Isabelle version for testing";
wenzelm
parents: 70775
diff changeset
   487
    shutting_down.change(_ => true)
65263
c97abf0fa0c1 strict initialization of plugin.session: no user errors to be expected before session.start;
wenzelm
parents: 65262
diff changeset
   488
    session.stop()
65245
e955b33f432c proper plugin access;
wenzelm
parents: 65244
diff changeset
   489
    file_watcher.shutdown()
66458
42d0d5c77c78 more robust shutdown, e.g. when plugin is stopped;
wenzelm
parents: 66351
diff changeset
   490
    PIDE.editor.shutdown()
73999
6b213c0115f5 clarified global state: allow to deactivate main plugin;
wenzelm
parents: 73998
diff changeset
   491
6b213c0115f5 clarified global state: allow to deactivate main plugin;
wenzelm
parents: 73998
diff changeset
   492
    Document_Model.reset()
34318
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   493
  }
c13e168a8ae6 original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff changeset
   494
}