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