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