src/Tools/jEdit/src/plugin.scala
author wenzelm
Tue Mar 14 22:05:59 2017 +0100 (2017-03-14)
changeset 65248 fef7b7a9e5b0
parent 65247 63d91d5de121
child 65249 c3ee88b9c3cb
permissions -rw-r--r--
tuned;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/plugin.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@50362
     4
Main plumbing for PIDE infrastructure as jEdit plugin.
wenzelm@36760
     5
*/
wenzelm@34407
     6
wenzelm@34318
     7
package isabelle.jedit
wenzelm@34318
     8
wenzelm@34429
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
wenzelm@44573
    12
import javax.swing.JOptionPane
wenzelm@34318
    13
wenzelm@64858
    14
import java.io.{File => JFile}
wenzelm@64858
    15
wenzelm@60205
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, Debug, PerspectiveManager}
wenzelm@65248
    17
import org.gjt.sp.jedit.textarea.JEditTextArea
wenzelm@50362
    18
import org.gjt.sp.jedit.syntax.ModeProvider
wenzelm@49100
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@43443
    20
import org.gjt.sp.util.SyntaxUtilities
wenzelm@62228
    21
import org.gjt.sp.util.Log
wenzelm@39241
    22
wenzelm@34318
    23
wenzelm@50205
    24
object PIDE
wenzelm@34618
    25
{
wenzelm@34784
    26
  /* plugin instance */
wenzelm@34784
    27
wenzelm@65238
    28
  @volatile var _plugin: Plugin = null
wenzelm@65238
    29
wenzelm@65238
    30
  def plugin: Plugin =
wenzelm@65238
    31
    if (_plugin == null) error("Uninitialized Isabelle/jEdit plugin")
wenzelm@65238
    32
    else _plugin
wenzelm@65238
    33
  def options: JEdit_Options = plugin.options
wenzelm@65238
    34
wenzelm@60835
    35
  @volatile var session: Session = new Session(JEdit_Resources.empty)
wenzelm@49101
    36
wenzelm@56208
    37
  def resources(): JEdit_Resources =
wenzelm@56208
    38
    session.resources.asInstanceOf[JEdit_Resources]
wenzelm@44955
    39
wenzelm@34784
    40
wenzelm@65248
    41
  /* semantic document content */
wenzelm@55621
    42
wenzelm@62062
    43
  def snapshot(view: View): Document.Snapshot = GUI_Thread.now
wenzelm@55621
    44
  {
wenzelm@64813
    45
    Document_Model.get(view.getBuffer) match {
wenzelm@55621
    46
      case Some(model) => model.snapshot
wenzelm@55621
    47
      case None => error("No document model for current buffer")
wenzelm@55621
    48
    }
wenzelm@55621
    49
  }
wenzelm@55621
    50
wenzelm@64621
    51
  def rendering(view: View): JEdit_Rendering = GUI_Thread.now
wenzelm@55621
    52
  {
wenzelm@55621
    53
    val text_area = view.getTextArea
wenzelm@64882
    54
    Document_View.get(text_area) match {
wenzelm@55621
    55
      case Some(doc_view) => doc_view.get_rendering()
wenzelm@55621
    56
      case None => error("No document view for current text area")
wenzelm@55621
    57
    }
wenzelm@55621
    58
  }
wenzelm@34318
    59
}
wenzelm@34318
    60
wenzelm@34429
    61
wenzelm@34618
    62
class Plugin extends EBPlugin
wenzelm@34618
    63
{
wenzelm@65238
    64
  /* early initialization */
wenzelm@65238
    65
wenzelm@65238
    66
  val options: JEdit_Options =
wenzelm@65238
    67
  {
wenzelm@65238
    68
    // adhoc patch of confusing message
wenzelm@65238
    69
    val plugin_error = jEdit.getProperty("plugin-error.start-error")
wenzelm@65238
    70
    jEdit.setProperty("plugin-error.start-error", "Cannot start plugin:\n{0}")
wenzelm@65238
    71
wenzelm@65238
    72
    val options = new JEdit_Options(Options.init())
wenzelm@65238
    73
wenzelm@65238
    74
    jEdit.setProperty("plugin-error.start-error", plugin_error)
wenzelm@65238
    75
wenzelm@65238
    76
    options
wenzelm@65238
    77
  }
wenzelm@65239
    78
wenzelm@65239
    79
  val completion_history = new Completion.History_Variable
wenzelm@65239
    80
  val spell_checker = new Spell_Checker_Variable
wenzelm@65239
    81
wenzelm@65238
    82
  PIDE._plugin = this
wenzelm@65238
    83
wenzelm@65238
    84
wenzelm@56316
    85
  /* global changes */
wenzelm@55791
    86
wenzelm@56316
    87
  def options_changed()
wenzelm@56316
    88
  {
wenzelm@56715
    89
    PIDE.session.global_options.post(Session.Global_Options(PIDE.options.value))
wenzelm@56770
    90
    delay_load.invoke()
wenzelm@55791
    91
  }
wenzelm@55791
    92
wenzelm@56316
    93
  def deps_changed()
wenzelm@56316
    94
  {
wenzelm@56770
    95
    delay_load.invoke()
wenzelm@56316
    96
  }
wenzelm@56316
    97
wenzelm@55791
    98
wenzelm@44577
    99
  /* theory files */
wenzelm@44574
   100
wenzelm@62068
   101
  lazy val delay_init =
wenzelm@57612
   102
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   103
    {
wenzelm@65244
   104
      init_models()
wenzelm@50344
   105
    }
wenzelm@50344
   106
wenzelm@57582
   107
  private val delay_load_active = Synchronized(false)
wenzelm@57582
   108
  private def delay_load_activated(): Boolean =
wenzelm@57582
   109
    delay_load_active.guarded_access(a => Some((!a, true)))
wenzelm@62068
   110
  private def delay_load_action()
wenzelm@62068
   111
  {
wenzelm@62068
   112
    if (Isabelle.continuous_checking && delay_load_activated() &&
wenzelm@62068
   113
        PerspectiveManager.isPerspectiveEnabled)
wenzelm@44574
   114
    {
wenzelm@64835
   115
      if (JEdit_Lib.jedit_buffers().exists(_.isLoading)) delay_load.invoke()
wenzelm@64835
   116
      else {
wenzelm@64835
   117
        val required_files =
wenzelm@64835
   118
        {
wenzelm@64835
   119
          val models = Document_Model.get_models()
wenzelm@44574
   120
wenzelm@62068
   121
          val thys =
wenzelm@64835
   122
            (for ((node_name, model) <- models.iterator if model.is_theory)
wenzelm@64835
   123
              yield (node_name, Position.none)).toList
wenzelm@64839
   124
          val thy_files = PIDE.resources.thy_info.dependencies("", thys).deps.map(_.name)
wenzelm@46761
   125
wenzelm@64839
   126
          val aux_files =
wenzelm@62068
   127
            if (PIDE.options.bool("jedit_auto_resolve")) {
wenzelm@64835
   128
              val stable_tip_version =
wenzelm@64835
   129
                if (models.forall(p => p._2.is_stable))
wenzelm@64835
   130
                  PIDE.session.current_state().stable_tip_version
wenzelm@64835
   131
                else None
wenzelm@64835
   132
              stable_tip_version match {
wenzelm@64835
   133
                case Some(version) => PIDE.resources.undefined_blobs(version.nodes)
wenzelm@64478
   134
                case None => delay_load.invoke(); Nil
wenzelm@57582
   135
              }
wenzelm@62068
   136
            }
wenzelm@62068
   137
            else Nil
wenzelm@62068
   138
wenzelm@64835
   139
          (thy_files ::: aux_files).filterNot(models.isDefinedAt(_))
wenzelm@64835
   140
        }
wenzelm@64835
   141
        if (required_files.nonEmpty) {
wenzelm@64835
   142
          try {
wenzelm@64835
   143
            Standard_Thread.fork("resolve_dependencies") {
wenzelm@64835
   144
              val loaded_files =
wenzelm@64835
   145
                for {
wenzelm@64835
   146
                  name <- required_files
wenzelm@64835
   147
                  text <- PIDE.resources.read_file_content(name)
wenzelm@64835
   148
                } yield (name, text)
wenzelm@62068
   149
wenzelm@64835
   150
              GUI_Thread.later {
wenzelm@64835
   151
                try {
wenzelm@64835
   152
                  Document_Model.provide_files(PIDE.session, loaded_files)
wenzelm@64835
   153
                  delay_init.invoke()
wenzelm@64835
   154
                }
wenzelm@64835
   155
                finally { delay_load_active.change(_ => false) }
wenzelm@53715
   156
              }
wenzelm@53074
   157
            }
wenzelm@46761
   158
          }
wenzelm@64835
   159
          catch { case _: Throwable => delay_load_active.change(_ => false) }
wenzelm@46761
   160
        }
wenzelm@64835
   161
        else delay_load_active.change(_ => false)
wenzelm@44606
   162
      }
wenzelm@44574
   163
    }
wenzelm@62068
   164
  }
wenzelm@62068
   165
wenzelm@64478
   166
  private lazy val delay_load =
wenzelm@62068
   167
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay")) { delay_load_action() }
wenzelm@44573
   168
wenzelm@64858
   169
  private def file_watcher_action(changed: Set[JFile]): Unit =
wenzelm@65246
   170
    if (Document_Model.sync_files(changed)) JEdit_Editor.invoke_generated()
wenzelm@64858
   171
wenzelm@64858
   172
  lazy val file_watcher: File_Watcher =
wenzelm@64858
   173
    File_Watcher(file_watcher_action _, PIDE.options.seconds("editor_load_delay"))
wenzelm@64858
   174
wenzelm@44574
   175
wenzelm@56715
   176
  /* session phase */
wenzelm@44573
   177
wenzelm@65226
   178
  val session_phase_changed: Session.Phase => Unit =
wenzelm@65226
   179
  {
wenzelm@65226
   180
    case Session.Terminated(_) =>
wenzelm@65226
   181
      GUI_Thread.later {
wenzelm@65226
   182
        GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@65226
   183
          "Isabelle Syslog", GUI.scrollable_text(PIDE.session.syslog_content()))
wenzelm@65226
   184
      }
wenzelm@39735
   185
wenzelm@65226
   186
    case Session.Ready =>
wenzelm@65226
   187
      PIDE.session.update_options(PIDE.options.value)
wenzelm@65244
   188
      init_models()
wenzelm@57640
   189
wenzelm@65226
   190
      if (!Isabelle.continuous_checking) {
wenzelm@65226
   191
        GUI_Thread.later {
wenzelm@65226
   192
          val answer =
wenzelm@65226
   193
            GUI.confirm_dialog(jEdit.getActiveView,
wenzelm@65226
   194
              "Continuous checking of PIDE document",
wenzelm@65226
   195
              JOptionPane.YES_NO_OPTION,
wenzelm@65226
   196
              "Continuous checking is presently disabled:",
wenzelm@65226
   197
              "editor buffers will remain inactive!",
wenzelm@65226
   198
              "Enable continuous checking now?")
wenzelm@65226
   199
          if (answer == 0) Isabelle.continuous_checking = true
wenzelm@57640
   200
        }
wenzelm@65226
   201
      }
wenzelm@57640
   202
wenzelm@65226
   203
      delay_load.invoke()
wenzelm@44573
   204
wenzelm@65226
   205
    case Session.Shutdown =>
wenzelm@65226
   206
      GUI_Thread.later {
wenzelm@65226
   207
        delay_load.revoke()
wenzelm@65226
   208
        delay_init.revoke()
wenzelm@65246
   209
        JEdit_Editor.flush()
wenzelm@65244
   210
        exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@65226
   211
      }
wenzelm@46740
   212
wenzelm@65226
   213
    case _ =>
wenzelm@65226
   214
  }
wenzelm@39630
   215
wenzelm@39630
   216
wenzelm@65244
   217
  /* document model and view */
wenzelm@65244
   218
wenzelm@65244
   219
  def exit_models(buffers: List[Buffer])
wenzelm@65244
   220
  {
wenzelm@65244
   221
    GUI_Thread.now {
wenzelm@65244
   222
      buffers.foreach(buffer =>
wenzelm@65244
   223
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@65244
   224
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@65244
   225
          Document_Model.exit(buffer)
wenzelm@65244
   226
        })
wenzelm@65244
   227
      }
wenzelm@65244
   228
  }
wenzelm@65244
   229
wenzelm@65244
   230
  def init_models()
wenzelm@65244
   231
  {
wenzelm@65244
   232
    GUI_Thread.now {
wenzelm@65246
   233
      JEdit_Editor.flush()
wenzelm@65244
   234
wenzelm@65244
   235
      for {
wenzelm@65244
   236
        buffer <- JEdit_Lib.jedit_buffers()
wenzelm@65244
   237
        if buffer != null && !buffer.getBooleanProperty(Buffer.GZIPPED)
wenzelm@65244
   238
      } {
wenzelm@65244
   239
        if (buffer.isLoaded) {
wenzelm@65244
   240
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@65244
   241
            val node_name = PIDE.resources.node_name(buffer)
wenzelm@65244
   242
            val model = Document_Model.init(PIDE.session, node_name, buffer)
wenzelm@65244
   243
            for {
wenzelm@65244
   244
              text_area <- JEdit_Lib.jedit_text_areas(buffer)
wenzelm@65244
   245
              if Document_View.get(text_area).map(_.model) != Some(model)
wenzelm@65244
   246
            } Document_View.init(model, text_area)
wenzelm@65244
   247
          }
wenzelm@65244
   248
        }
wenzelm@65244
   249
        else delay_init.invoke()
wenzelm@65244
   250
      }
wenzelm@65244
   251
wenzelm@65246
   252
      JEdit_Editor.invoke_generated()
wenzelm@65244
   253
    }
wenzelm@65244
   254
  }
wenzelm@65244
   255
wenzelm@65244
   256
  def init_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@65244
   257
    GUI_Thread.now {
wenzelm@65244
   258
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@65244
   259
        Document_Model.get(buffer) match {
wenzelm@65244
   260
          case Some(model) => Document_View.init(model, text_area)
wenzelm@65244
   261
          case None =>
wenzelm@65244
   262
        }
wenzelm@65244
   263
      }
wenzelm@65244
   264
    }
wenzelm@65244
   265
wenzelm@65244
   266
  def exit_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@65244
   267
    GUI_Thread.now {
wenzelm@65244
   268
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@65244
   269
        Document_View.exit(text_area)
wenzelm@65244
   270
      }
wenzelm@65244
   271
    }
wenzelm@65244
   272
wenzelm@65244
   273
wenzelm@34618
   274
  /* main plugin plumbing */
wenzelm@34433
   275
wenzelm@65241
   276
  @volatile private var startup_failure: Option[Throwable] = None
wenzelm@65241
   277
  @volatile private var startup_notified = false
wenzelm@65241
   278
wenzelm@34767
   279
  override def handleMessage(message: EBMessage)
wenzelm@34618
   280
  {
wenzelm@57612
   281
    GUI_Thread.assert {}
wenzelm@39630
   282
wenzelm@65241
   283
    if (startup_failure.isDefined && !startup_notified) {
wenzelm@49099
   284
      message match {
wenzelm@49100
   285
        case msg: EditorStarted =>
wenzelm@51616
   286
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@65241
   287
            GUI.scrollable_text(Exn.message(startup_failure.get)),
wenzelm@49099
   288
            "Prover IDE inactive!")
wenzelm@65241
   289
          startup_notified = true
wenzelm@49099
   290
        case _ =>
wenzelm@49099
   291
      }
wenzelm@49099
   292
    }
wenzelm@49099
   293
wenzelm@65241
   294
    if (startup_failure.isEmpty) {
wenzelm@49099
   295
      message match {
wenzelm@49099
   296
        case msg: EditorStarted =>
wenzelm@57649
   297
          if (Distribution.is_identified && !Distribution.is_official) {
wenzelm@57680
   298
            GUI.warning_dialog(jEdit.getActiveView, "Isabelle version for testing",
wenzelm@60206
   299
              "This is " + Distribution.version + ".",
wenzelm@57649
   300
              "It is for testing only, not for production use.")
wenzelm@57649
   301
          }
wenzelm@57649
   302
wenzelm@64602
   303
          val view = jEdit.getActiveView()
wenzelm@64602
   304
wenzelm@64602
   305
          Session_Build.session_build(view)
wenzelm@63747
   306
wenzelm@64602
   307
          Keymap_Merge.check_dialog(view)
wenzelm@64602
   308
wenzelm@65246
   309
          JEdit_Editor.hyperlink_position(true, Document.Snapshot.init,
wenzelm@64602
   310
            JEdit_Sessions.session_info().open_root).foreach(_.follow(view))
wenzelm@61277
   311
wenzelm@49099
   312
        case msg: BufferUpdate
wenzelm@64818
   313
        if msg.getWhat == BufferUpdate.LOAD_STARTED || msg.getWhat == BufferUpdate.CLOSING =>
wenzelm@64838
   314
          if (msg.getBuffer != null) {
wenzelm@65244
   315
            exit_models(List(msg.getBuffer))
wenzelm@65246
   316
            JEdit_Editor.invoke_generated()
wenzelm@64838
   317
          }
wenzelm@57611
   318
wenzelm@64818
   319
        case msg: BufferUpdate
wenzelm@64818
   320
        if msg.getWhat == BufferUpdate.PROPERTIES_CHANGED || msg.getWhat == BufferUpdate.LOADED =>
wenzelm@50205
   321
          if (PIDE.session.is_ready) {
wenzelm@55791
   322
            delay_init.invoke()
wenzelm@55791
   323
            delay_load.invoke()
wenzelm@49099
   324
          }
wenzelm@34784
   325
wenzelm@49099
   326
        case msg: EditPaneUpdate
wenzelm@60215
   327
        if msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   328
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   329
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@60215
   330
            msg.getWhat == EditPaneUpdate.DESTROYED =>
wenzelm@49099
   331
          val edit_pane = msg.getEditPane
wenzelm@49099
   332
          val buffer = edit_pane.getBuffer
wenzelm@49099
   333
          val text_area = edit_pane.getTextArea
wenzelm@49099
   334
wenzelm@49099
   335
          if (buffer != null && text_area != null) {
wenzelm@49099
   336
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   337
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   338
              if (PIDE.session.is_ready)
wenzelm@65244
   339
                init_view(buffer, text_area)
wenzelm@49099
   340
            }
wenzelm@52867
   341
            else {
wenzelm@65240
   342
              Isabelle.dismissed_popups(text_area.getView)
wenzelm@65244
   343
              exit_view(buffer, text_area)
wenzelm@52867
   344
            }
wenzelm@53274
   345
wenzelm@53274
   346
            if (msg.getWhat == EditPaneUpdate.CREATED)
wenzelm@53274
   347
              Completion_Popup.Text_Area.init(text_area)
wenzelm@53274
   348
wenzelm@53274
   349
            if (msg.getWhat == EditPaneUpdate.DESTROYED)
wenzelm@53274
   350
              Completion_Popup.Text_Area.exit(text_area)
wenzelm@43510
   351
          }
wenzelm@34784
   352
wenzelm@49099
   353
        case msg: PropertiesChanged =>
wenzelm@62264
   354
          for {
wenzelm@62264
   355
            view <- JEdit_Lib.jedit_views
wenzelm@62264
   356
            edit_pane <- JEdit_Lib.jedit_edit_panes(view)
wenzelm@62264
   357
          } {
wenzelm@62264
   358
            val buffer = edit_pane.getBuffer
wenzelm@62264
   359
            val text_area = edit_pane.getTextArea
wenzelm@65244
   360
            if (buffer != null && text_area != null) init_view(buffer, text_area)
wenzelm@62264
   361
          }
wenzelm@62264
   362
wenzelm@65242
   363
          spell_checker.update(PIDE.options.value)
wenzelm@52084
   364
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   365
wenzelm@49099
   366
        case _ =>
wenzelm@49099
   367
      }
wenzelm@34318
   368
    }
wenzelm@34318
   369
  }
wenzelm@34318
   370
wenzelm@34618
   371
  override def start()
wenzelm@49099
   372
  {
wenzelm@49099
   373
    try {
wenzelm@50709
   374
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   375
wenzelm@65242
   376
      completion_history.load()
wenzelm@65242
   377
      spell_checker.update(PIDE.options.value)
wenzelm@49245
   378
wenzelm@49099
   379
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   380
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   381
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   382
wenzelm@53274
   383
      JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.init _)
wenzelm@53274
   384
wenzelm@65210
   385
      val resources = new JEdit_Resources(JEdit_Sessions.session_base())
wenzelm@49524
   386
wenzelm@62051
   387
      PIDE.session.stop()
wenzelm@56208
   388
      PIDE.session = new Session(resources) {
wenzelm@50207
   389
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@57867
   390
        override def prune_delay = PIDE.options.seconds("editor_prune_delay")
wenzelm@57974
   391
        override def syslog_limit = PIDE.options.int("editor_syslog_limit")
wenzelm@50205
   392
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   393
      }
wenzelm@48870
   394
wenzelm@65241
   395
      startup_failure = None
wenzelm@49099
   396
    }
wenzelm@49099
   397
    catch {
wenzelm@49099
   398
      case exn: Throwable =>
wenzelm@65241
   399
        startup_failure = Some(exn)
wenzelm@65241
   400
        startup_notified = false
wenzelm@62228
   401
        Log.log(Log.ERROR, this, exn)
wenzelm@49099
   402
    }
wenzelm@34318
   403
  }
wenzelm@34618
   404
wenzelm@39628
   405
  override def stop()
wenzelm@34618
   406
  {
wenzelm@53274
   407
    JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.exit _)
wenzelm@53274
   408
wenzelm@65241
   409
    if (startup_failure.isEmpty) {
wenzelm@50205
   410
      PIDE.options.value.save_prefs()
wenzelm@65242
   411
      completion_history.value.save()
wenzelm@53337
   412
    }
wenzelm@49245
   413
wenzelm@65244
   414
    exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   415
    PIDE.session.stop()
wenzelm@65245
   416
    file_watcher.shutdown()
wenzelm@34318
   417
  }
wenzelm@34318
   418
}