src/Tools/jEdit/src/plugin.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 57611 b6256ea3b7c5
child 57640 0a28cf866d5d
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
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@49246
    14
import scala.swing.{ListView, ScrollPane}
immler@34406
    15
wenzelm@50709
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View, Debug}
wenzelm@53487
    17
import org.jedit.options.CombinedOptions
wenzelm@53487
    18
import org.gjt.sp.jedit.gui.AboutDialog
wenzelm@39043
    19
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@50362
    20
import org.gjt.sp.jedit.syntax.ModeProvider
wenzelm@49100
    21
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    22
wenzelm@43443
    23
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    24
wenzelm@34318
    25
wenzelm@50205
    26
object PIDE
wenzelm@34618
    27
{
wenzelm@34784
    28
  /* plugin instance */
wenzelm@34784
    29
wenzelm@49245
    30
  val options = new JEdit_Options
wenzelm@53337
    31
  val completion_history = new Completion.History_Variable
wenzelm@56558
    32
  val spell_checker = new Spell_Checker_Variable
wenzelm@49245
    33
wenzelm@49099
    34
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    35
  @volatile var startup_notified = false
wenzelm@34784
    36
wenzelm@49101
    37
  @volatile var plugin: Plugin = null
wenzelm@56801
    38
  @volatile var session: Session =
wenzelm@56801
    39
    new Session(new JEdit_Resources(Set.empty, Map.empty, Outer_Syntax.empty))
wenzelm@49101
    40
wenzelm@55791
    41
  def options_changed() { plugin.options_changed() }
wenzelm@56316
    42
  def deps_changed() { plugin.deps_changed() }
wenzelm@52815
    43
wenzelm@56208
    44
  def resources(): JEdit_Resources =
wenzelm@56208
    45
    session.resources.asInstanceOf[JEdit_Resources]
wenzelm@44955
    46
wenzelm@52971
    47
  lazy val editor = new JEdit_Editor
wenzelm@52971
    48
wenzelm@34784
    49
wenzelm@53244
    50
  /* popups */
wenzelm@53244
    51
wenzelm@53244
    52
  def dismissed_popups(view: View): Boolean =
wenzelm@53244
    53
  {
wenzelm@53272
    54
    var dismissed = false
wenzelm@53272
    55
wenzelm@53274
    56
    JEdit_Lib.jedit_text_areas(view).foreach(text_area =>
wenzelm@53274
    57
      if (Completion_Popup.Text_Area.dismissed(text_area)) dismissed = true)
wenzelm@53272
    58
wenzelm@53272
    59
    if (Pretty_Tooltip.dismissed_all()) dismissed = true
wenzelm@53272
    60
wenzelm@53272
    61
    dismissed
wenzelm@53244
    62
  }
wenzelm@53244
    63
wenzelm@53244
    64
wenzelm@43449
    65
  /* document model and view */
wenzelm@43449
    66
wenzelm@43449
    67
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@53274
    68
wenzelm@55712
    69
  def document_view(text_area: TextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    70
wenzelm@44379
    71
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    72
    for {
wenzelm@49406
    73
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@54529
    74
      doc_view <- document_view(text_area)
wenzelm@54529
    75
    } yield doc_view
wenzelm@44379
    76
wenzelm@54522
    77
  def document_models(): List[Document_Model] =
wenzelm@54522
    78
    for {
wenzelm@54522
    79
      buffer <- JEdit_Lib.jedit_buffers().toList
wenzelm@54522
    80
      model <- document_model(buffer)
wenzelm@54522
    81
    } yield model
wenzelm@54522
    82
wenzelm@54522
    83
  def document_blobs(): Document.Blobs =
wenzelm@55783
    84
    Document.Blobs(
wenzelm@55783
    85
      (for {
wenzelm@55783
    86
        buffer <- JEdit_Lib.jedit_buffers()
wenzelm@55783
    87
        model <- document_model(buffer)
wenzelm@55783
    88
        blob <- model.get_blob()
wenzelm@55783
    89
      } yield (model.node_name -> blob)).toMap)
wenzelm@54522
    90
wenzelm@50344
    91
  def exit_models(buffers: List[Buffer])
wenzelm@47058
    92
  {
wenzelm@57612
    93
    GUI_Thread.now {
wenzelm@54461
    94
      PIDE.editor.flush()
wenzelm@50344
    95
      buffers.foreach(buffer =>
wenzelm@50344
    96
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    97
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@50344
    98
          Document_Model.exit(buffer)
wenzelm@50344
    99
        })
wenzelm@50344
   100
      }
wenzelm@47058
   101
  }
wenzelm@47058
   102
wenzelm@55791
   103
  def init_models()
wenzelm@43510
   104
  {
wenzelm@57612
   105
    GUI_Thread.now {
wenzelm@54461
   106
      PIDE.editor.flush()
wenzelm@55791
   107
wenzelm@55791
   108
      for {
wenzelm@55791
   109
        buffer <- JEdit_Lib.jedit_buffers()
wenzelm@55791
   110
        if buffer != null && !buffer.isLoading && !buffer.getBooleanProperty(Buffer.GZIPPED)
wenzelm@55791
   111
      } {
wenzelm@55791
   112
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@56208
   113
          val node_name = resources.node_name(buffer)
wenzelm@55791
   114
          val model =
wenzelm@55791
   115
            document_model(buffer) match {
wenzelm@55791
   116
              case Some(model) if model.node_name == node_name => model
wenzelm@55791
   117
              case _ => Document_Model.init(session, buffer, node_name)
wenzelm@43510
   118
            }
wenzelm@55791
   119
          for {
wenzelm@55791
   120
            text_area <- JEdit_Lib.jedit_text_areas(buffer)
wenzelm@55791
   121
            if document_view(text_area).map(_.model) != Some(model)
wenzelm@55791
   122
          } Document_View.init(model, text_area)
wenzelm@43510
   123
        }
wenzelm@55791
   124
      }
wenzelm@55791
   125
wenzelm@55791
   126
      PIDE.editor.invoke()
wenzelm@43510
   127
    }
wenzelm@43510
   128
  }
wenzelm@43510
   129
wenzelm@56774
   130
  def init_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@57612
   131
    GUI_Thread.now {
wenzelm@56774
   132
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@56774
   133
        document_model(buffer) match {
wenzelm@56774
   134
          case Some(model) => Document_View.init(model, text_area)
wenzelm@56774
   135
          case None =>
wenzelm@56774
   136
        }
wenzelm@43510
   137
      }
wenzelm@43510
   138
    }
wenzelm@43510
   139
wenzelm@56774
   140
  def exit_view(buffer: Buffer, text_area: JEditTextArea): Unit =
wenzelm@57612
   141
    GUI_Thread.now {
wenzelm@56774
   142
      JEdit_Lib.buffer_lock(buffer) {
wenzelm@56774
   143
        Document_View.exit(text_area)
wenzelm@56774
   144
      }
wenzelm@43510
   145
    }
wenzelm@55621
   146
wenzelm@55621
   147
wenzelm@55621
   148
  /* current document content */
wenzelm@55621
   149
wenzelm@57612
   150
  def snapshot(view: View): Document.Snapshot = GUI_Thread.now
wenzelm@55621
   151
  {
wenzelm@55621
   152
    val buffer = view.getBuffer
wenzelm@55621
   153
    document_model(buffer) match {
wenzelm@55621
   154
      case Some(model) => model.snapshot
wenzelm@55621
   155
      case None => error("No document model for current buffer")
wenzelm@55621
   156
    }
wenzelm@55621
   157
  }
wenzelm@55621
   158
wenzelm@57612
   159
  def rendering(view: View): Rendering = GUI_Thread.now
wenzelm@55621
   160
  {
wenzelm@55621
   161
    val text_area = view.getTextArea
wenzelm@55621
   162
    document_view(text_area) match {
wenzelm@55621
   163
      case Some(doc_view) => doc_view.get_rendering()
wenzelm@55621
   164
      case None => error("No document view for current text area")
wenzelm@55621
   165
    }
wenzelm@55621
   166
  }
wenzelm@34318
   167
}
wenzelm@34318
   168
wenzelm@34429
   169
wenzelm@34618
   170
class Plugin extends EBPlugin
wenzelm@34618
   171
{
wenzelm@56316
   172
  /* global changes */
wenzelm@55791
   173
wenzelm@56316
   174
  def options_changed()
wenzelm@56316
   175
  {
wenzelm@56715
   176
    PIDE.session.global_options.post(Session.Global_Options(PIDE.options.value))
wenzelm@56770
   177
    delay_load.invoke()
wenzelm@55791
   178
  }
wenzelm@55791
   179
wenzelm@56316
   180
  def deps_changed()
wenzelm@56316
   181
  {
wenzelm@56770
   182
    delay_load.invoke()
wenzelm@56316
   183
  }
wenzelm@56316
   184
wenzelm@55791
   185
wenzelm@44577
   186
  /* theory files */
wenzelm@44574
   187
wenzelm@50344
   188
  private lazy val delay_init =
wenzelm@57612
   189
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   190
    {
wenzelm@55791
   191
      PIDE.init_models()
wenzelm@50344
   192
    }
wenzelm@50344
   193
wenzelm@57582
   194
  private val delay_load_active = Synchronized(false)
wenzelm@57582
   195
  private def delay_load_activated(): Boolean =
wenzelm@57582
   196
    delay_load_active.guarded_access(a => Some((!a, true)))
wenzelm@57582
   197
wenzelm@44573
   198
  private lazy val delay_load =
wenzelm@57612
   199
    GUI_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   200
    {
wenzelm@57582
   201
      if (Isabelle.continuous_checking && delay_load_activated()) {
wenzelm@57582
   202
        try {
wenzelm@57582
   203
          val view = jEdit.getActiveView()
wenzelm@44963
   204
wenzelm@57582
   205
          val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@57582
   206
          if (buffers.forall(_.isLoaded)) {
wenzelm@57582
   207
            def loaded_buffer(name: String): Boolean =
wenzelm@57582
   208
              buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   209
wenzelm@57582
   210
            val thys =
wenzelm@57582
   211
              for {
wenzelm@57582
   212
                buffer <- buffers
wenzelm@57582
   213
                model <- PIDE.document_model(buffer)
wenzelm@57582
   214
                if model.is_theory
wenzelm@57582
   215
              } yield (model.node_name, Position.none)
wenzelm@46761
   216
wenzelm@57582
   217
            val thy_info = new Thy_Info(PIDE.resources)
wenzelm@57612
   218
            // FIXME avoid I/O on GUI thread!?!
wenzelm@57582
   219
            val files = thy_info.dependencies("", thys).deps.map(_.name.node).
wenzelm@57582
   220
              filter(file => !loaded_buffer(file) && PIDE.resources.check_file(view, file))
wenzelm@44574
   221
wenzelm@57582
   222
            if (!files.isEmpty) {
wenzelm@57582
   223
              if (PIDE.options.bool("jedit_auto_load")) {
wenzelm@57582
   224
                files.foreach(file => jEdit.openFile(null: View, file))
wenzelm@57582
   225
              }
wenzelm@57582
   226
              else {
wenzelm@57582
   227
                val files_list = new ListView(files.sorted)
wenzelm@57582
   228
                for (i <- 0 until files.length)
wenzelm@57582
   229
                  files_list.selection.indices += i
wenzelm@44606
   230
wenzelm@57582
   231
                val answer =
wenzelm@57582
   232
                  GUI.confirm_dialog(view,
wenzelm@57582
   233
                    "Auto loading of required files",
wenzelm@57582
   234
                    JOptionPane.YES_NO_OPTION,
wenzelm@57582
   235
                    "The following files are required to resolve theory imports.",
wenzelm@57582
   236
                    "Reload selected files now?",
wenzelm@57582
   237
                    new ScrollPane(files_list),
wenzelm@57582
   238
                    new Isabelle.Continuous_Checking)
wenzelm@57582
   239
                if (answer == 0) {
wenzelm@57582
   240
                  files.foreach(file =>
wenzelm@57582
   241
                    if (files_list.selection.items.contains(file))
wenzelm@57582
   242
                      jEdit.openFile(null: View, file))
wenzelm@57582
   243
                }
wenzelm@53715
   244
              }
wenzelm@53074
   245
            }
wenzelm@46761
   246
          }
wenzelm@46761
   247
        }
wenzelm@57582
   248
        finally { delay_load_active.change(_ => false) }
wenzelm@44606
   249
      }
wenzelm@44574
   250
    }
wenzelm@44573
   251
wenzelm@44574
   252
wenzelm@56715
   253
  /* session phase */
wenzelm@44573
   254
wenzelm@56715
   255
  private val session_phase =
wenzelm@56715
   256
    Session.Consumer[Session.Phase](getClass.getName) {
wenzelm@56715
   257
      case Session.Inactive | Session.Failed =>
wenzelm@57612
   258
        GUI_Thread.later {
wenzelm@56715
   259
          GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@56775
   260
            "Isabelle Syslog", GUI.scrollable_text(PIDE.session.syslog_content()))
wenzelm@56715
   261
        }
wenzelm@39735
   262
wenzelm@56715
   263
      case Session.Ready =>
wenzelm@56715
   264
        PIDE.session.update_options(PIDE.options.value)
wenzelm@56715
   265
        PIDE.init_models()
wenzelm@56770
   266
        delay_load.invoke()
wenzelm@44573
   267
wenzelm@56715
   268
      case Session.Shutdown =>
wenzelm@56715
   269
        PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@56770
   270
        delay_load.revoke()
wenzelm@46740
   271
wenzelm@56715
   272
      case _ =>
wenzelm@39630
   273
    }
wenzelm@39630
   274
wenzelm@39630
   275
wenzelm@34618
   276
  /* main plugin plumbing */
wenzelm@34433
   277
wenzelm@34767
   278
  override def handleMessage(message: EBMessage)
wenzelm@34618
   279
  {
wenzelm@57612
   280
    GUI_Thread.assert {}
wenzelm@39630
   281
wenzelm@50205
   282
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   283
      message match {
wenzelm@49100
   284
        case msg: EditorStarted =>
wenzelm@51616
   285
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@51616
   286
            GUI.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   287
            "Prover IDE inactive!")
wenzelm@50205
   288
          PIDE.startup_notified = true
wenzelm@49099
   289
        case _ =>
wenzelm@49099
   290
      }
wenzelm@49099
   291
    }
wenzelm@49099
   292
wenzelm@50205
   293
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   294
      message match {
wenzelm@49099
   295
        case msg: EditorStarted =>
wenzelm@56387
   296
          PIDE.session.start("Isabelle", Isabelle_Logic.session_args())
wenzelm@37557
   297
wenzelm@49099
   298
        case msg: BufferUpdate
wenzelm@57611
   299
        if msg.getWhat == BufferUpdate.LOADED ||
wenzelm@57611
   300
          msg.getWhat == BufferUpdate.PROPERTIES_CHANGED ||
wenzelm@57611
   301
          msg.getWhat == BufferUpdate.CLOSING =>
wenzelm@57611
   302
wenzelm@57611
   303
          if (msg.getWhat == BufferUpdate.CLOSING) {
wenzelm@57611
   304
            val buffer = msg.getBuffer
wenzelm@57611
   305
            if (buffer != null) PIDE.editor.remove_node(PIDE.resources.node_name(msg.getBuffer))
wenzelm@57611
   306
          }
wenzelm@50205
   307
          if (PIDE.session.is_ready) {
wenzelm@55791
   308
            delay_init.invoke()
wenzelm@55791
   309
            delay_load.invoke()
wenzelm@49099
   310
          }
wenzelm@34784
   311
wenzelm@49099
   312
        case msg: EditPaneUpdate
wenzelm@49099
   313
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   314
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   315
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   316
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   317
          val edit_pane = msg.getEditPane
wenzelm@49099
   318
          val buffer = edit_pane.getBuffer
wenzelm@49099
   319
          val text_area = edit_pane.getTextArea
wenzelm@49099
   320
wenzelm@49099
   321
          if (buffer != null && text_area != null) {
wenzelm@49099
   322
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   323
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   324
              if (PIDE.session.is_ready)
wenzelm@50205
   325
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   326
            }
wenzelm@52867
   327
            else {
wenzelm@53244
   328
              PIDE.dismissed_popups(text_area.getView)
wenzelm@52867
   329
              PIDE.exit_view(buffer, text_area)
wenzelm@52867
   330
            }
wenzelm@53274
   331
wenzelm@53274
   332
            if (msg.getWhat == EditPaneUpdate.CREATED)
wenzelm@53274
   333
              Completion_Popup.Text_Area.init(text_area)
wenzelm@53274
   334
wenzelm@53274
   335
            if (msg.getWhat == EditPaneUpdate.DESTROYED)
wenzelm@53274
   336
              Completion_Popup.Text_Area.exit(text_area)
wenzelm@43510
   337
          }
wenzelm@34784
   338
wenzelm@49099
   339
        case msg: PropertiesChanged =>
wenzelm@56558
   340
          PIDE.spell_checker.update(PIDE.options.value)
wenzelm@52084
   341
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   342
wenzelm@49099
   343
        case _ =>
wenzelm@49099
   344
      }
wenzelm@34318
   345
    }
wenzelm@34318
   346
  }
wenzelm@34318
   347
wenzelm@34618
   348
  override def start()
wenzelm@49099
   349
  {
wenzelm@49099
   350
    try {
wenzelm@50709
   351
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   352
wenzelm@50205
   353
      PIDE.plugin = this
wenzelm@49099
   354
      Isabelle_System.init()
wenzelm@51614
   355
      Isabelle_Font.install_fonts()
wenzelm@49099
   356
wenzelm@53338
   357
      PIDE.options.update(Options.init())
wenzelm@53337
   358
      PIDE.completion_history.load()
wenzelm@56558
   359
      PIDE.spell_checker.update(PIDE.options.value)
wenzelm@49245
   360
wenzelm@49099
   361
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   362
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   363
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   364
wenzelm@53274
   365
      JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.init _)
wenzelm@53274
   366
wenzelm@49246
   367
      val content = Isabelle_Logic.session_content(false)
wenzelm@56801
   368
      val resources =
wenzelm@56801
   369
        new JEdit_Resources(content.loaded_theories, content.known_theories, content.syntax)
wenzelm@49524
   370
wenzelm@56208
   371
      PIDE.session = new Session(resources) {
wenzelm@50207
   372
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   373
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   374
      }
wenzelm@48870
   375
wenzelm@56715
   376
      PIDE.session.phase_changed += session_phase
wenzelm@50205
   377
      PIDE.startup_failure = None
wenzelm@49099
   378
    }
wenzelm@49099
   379
    catch {
wenzelm@49099
   380
      case exn: Throwable =>
wenzelm@50205
   381
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   382
        PIDE.startup_notified = false
wenzelm@49099
   383
    }
wenzelm@34318
   384
  }
wenzelm@34618
   385
wenzelm@39628
   386
  override def stop()
wenzelm@34618
   387
  {
wenzelm@53274
   388
    JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.exit _)
wenzelm@53274
   389
wenzelm@53337
   390
    if (PIDE.startup_failure.isEmpty) {
wenzelm@50205
   391
      PIDE.options.value.save_prefs()
wenzelm@53337
   392
      PIDE.completion_history.value.save()
wenzelm@53337
   393
    }
wenzelm@49245
   394
wenzelm@56715
   395
    PIDE.session.phase_changed -= session_phase
wenzelm@50344
   396
    PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   397
    PIDE.session.stop()
wenzelm@34318
   398
  }
wenzelm@34318
   399
}