src/Tools/jEdit/src/plugin.scala
author wenzelm
Sun Nov 17 16:02:06 2013 +0100 (2013-11-17)
changeset 54461 6c360a6ade0e
parent 54326 c5556b404902
child 54509 1f77110c94ef
permissions -rw-r--r--
centralized management of pending buffer edits;
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@43449
    71
  def document_view(text_area: JEditTextArea): 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@46997
    76
      doc_view = document_view(text_area)
wenzelm@44379
    77
      if doc_view.isDefined
wenzelm@44379
    78
    } yield doc_view.get
wenzelm@44379
    79
wenzelm@50344
    80
  def exit_models(buffers: List[Buffer])
wenzelm@47058
    81
  {
wenzelm@50344
    82
    Swing_Thread.now {
wenzelm@54461
    83
      PIDE.editor.flush()
wenzelm@50344
    84
      buffers.foreach(buffer =>
wenzelm@50344
    85
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    86
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@50344
    87
          Document_Model.exit(buffer)
wenzelm@50344
    88
        })
wenzelm@50344
    89
      }
wenzelm@47058
    90
  }
wenzelm@47058
    91
wenzelm@50344
    92
  def init_models(buffers: List[Buffer])
wenzelm@43510
    93
  {
wenzelm@50344
    94
    Swing_Thread.now {
wenzelm@54461
    95
      PIDE.editor.flush()
wenzelm@50344
    96
      val init_edits =
wenzelm@50344
    97
        (List.empty[Document.Edit_Text] /: buffers) { case (edits, buffer) =>
wenzelm@50344
    98
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    99
            val (model_edits, opt_model) =
wenzelm@50566
   100
              thy_load.buffer_node_name(buffer) match {
wenzelm@50344
   101
                case Some(node_name) =>
wenzelm@50344
   102
                  document_model(buffer) match {
wenzelm@52973
   103
                    case Some(model) if model.node_name == node_name => (Nil, Some(model))
wenzelm@50344
   104
                    case _ =>
wenzelm@50344
   105
                      val model = Document_Model.init(session, buffer, node_name)
wenzelm@50344
   106
                      (model.init_edits(), Some(model))
wenzelm@50344
   107
                  }
wenzelm@50344
   108
                case None => (Nil, None)
wenzelm@50344
   109
              }
wenzelm@50344
   110
            if (opt_model.isDefined) {
wenzelm@50344
   111
              for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@50344
   112
                if (document_view(text_area).map(_.model) != opt_model)
wenzelm@50344
   113
                  Document_View.init(opt_model.get, text_area)
wenzelm@50344
   114
              }
wenzelm@43510
   115
            }
wenzelm@50344
   116
            model_edits ::: edits
wenzelm@50344
   117
          }
wenzelm@43510
   118
        }
wenzelm@50566
   119
      session.update(init_edits)
wenzelm@43510
   120
    }
wenzelm@43510
   121
  }
wenzelm@43510
   122
wenzelm@43510
   123
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   124
  {
wenzelm@49406
   125
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   126
      document_model(buffer) match {
wenzelm@43510
   127
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   128
        case None =>
wenzelm@43510
   129
      }
wenzelm@43510
   130
    }
wenzelm@43510
   131
  }
wenzelm@43510
   132
wenzelm@43510
   133
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   134
  {
wenzelm@49406
   135
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   136
      Document_View.exit(text_area)
wenzelm@43510
   137
    }
wenzelm@43510
   138
  }
wenzelm@34318
   139
}
wenzelm@34318
   140
wenzelm@34429
   141
wenzelm@34618
   142
class Plugin extends EBPlugin
wenzelm@34618
   143
{
wenzelm@44577
   144
  /* theory files */
wenzelm@44574
   145
wenzelm@50344
   146
  private lazy val delay_init =
wenzelm@50344
   147
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   148
    {
wenzelm@50344
   149
      PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50344
   150
    }
wenzelm@50344
   151
wenzelm@53074
   152
  def load_theories() { Swing_Thread.later { delay_load.invoke() }}
wenzelm@53074
   153
wenzelm@44573
   154
  private lazy val delay_load =
wenzelm@50207
   155
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   156
    {
wenzelm@53074
   157
      if (Isabelle.continuous_checking) {
wenzelm@53074
   158
        val view = jEdit.getActiveView()
wenzelm@44963
   159
wenzelm@53074
   160
        val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@53074
   161
        if (buffers.forall(_.isLoaded)) {
wenzelm@53074
   162
          def loaded_buffer(name: String): Boolean =
wenzelm@53074
   163
            buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   164
wenzelm@53074
   165
          val thys =
wenzelm@53074
   166
            for (buffer <- buffers; model <- PIDE.document_model(buffer))
wenzelm@53074
   167
              yield model.node_name
wenzelm@46761
   168
wenzelm@53074
   169
          val thy_info = new Thy_Info(PIDE.thy_load)
wenzelm@53074
   170
          // FIXME avoid I/O in Swing thread!?!
wenzelm@53074
   171
          val files = thy_info.dependencies(thys).deps.map(_.name.node).
wenzelm@53074
   172
            filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
wenzelm@44574
   173
wenzelm@53074
   174
          if (!files.isEmpty) {
wenzelm@53715
   175
            if (PIDE.options.bool("jedit_auto_load")) {
wenzelm@53715
   176
              files.foreach(file => jEdit.openFile(null: View, file))
wenzelm@53715
   177
            }
wenzelm@53715
   178
            else {
wenzelm@53715
   179
              val files_list = new ListView(files.sorted)
wenzelm@53715
   180
              for (i <- 0 until files.length)
wenzelm@53715
   181
                files_list.selection.indices += i
wenzelm@44606
   182
wenzelm@53715
   183
              val answer =
wenzelm@53715
   184
                GUI.confirm_dialog(view,
wenzelm@53715
   185
                  "Auto loading of required files",
wenzelm@53715
   186
                  JOptionPane.YES_NO_OPTION,
wenzelm@53715
   187
                  "The following files are required to resolve theory imports.",
wenzelm@53715
   188
                  "Reload selected files now?",
wenzelm@53715
   189
                  new ScrollPane(files_list),
wenzelm@53715
   190
                  new Isabelle.Continuous_Checking)
wenzelm@53715
   191
              if (answer == 0) {
wenzelm@53715
   192
                files.foreach(file =>
wenzelm@53715
   193
                  if (files_list.selection.items.contains(file))
wenzelm@53715
   194
                    jEdit.openFile(null: View, file))
wenzelm@53715
   195
              }
wenzelm@53074
   196
            }
wenzelm@46761
   197
          }
wenzelm@46761
   198
        }
wenzelm@44606
   199
      }
wenzelm@44574
   200
    }
wenzelm@44573
   201
wenzelm@44574
   202
wenzelm@44574
   203
  /* session manager */
wenzelm@44573
   204
wenzelm@39633
   205
  private val session_manager = actor {
wenzelm@39633
   206
    loop {
wenzelm@39633
   207
      react {
wenzelm@39735
   208
        case phase: Session.Phase =>
wenzelm@39735
   209
          phase match {
wenzelm@50808
   210
            case Session.Inactive | Session.Failed =>
wenzelm@46918
   211
              Swing_Thread.later {
wenzelm@51616
   212
                GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@51616
   213
                    "Isabelle Syslog", GUI.scrollable_text(PIDE.session.current_syslog()))
wenzelm@39735
   214
              }
wenzelm@39735
   215
wenzelm@44573
   216
            case Session.Ready =>
wenzelm@52084
   217
              PIDE.session.update_options(PIDE.options.value)
wenzelm@50344
   218
              PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   219
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   220
wenzelm@46740
   221
            case Session.Shutdown =>
wenzelm@50344
   222
              PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   223
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   224
wenzelm@39735
   225
            case _ =>
wenzelm@39735
   226
          }
wenzelm@49406
   227
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   228
      }
wenzelm@39630
   229
    }
wenzelm@39630
   230
  }
wenzelm@39630
   231
wenzelm@39630
   232
wenzelm@34618
   233
  /* main plugin plumbing */
wenzelm@34433
   234
wenzelm@34767
   235
  override def handleMessage(message: EBMessage)
wenzelm@34618
   236
  {
wenzelm@43510
   237
    Swing_Thread.assert()
wenzelm@39630
   238
wenzelm@50205
   239
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   240
      message match {
wenzelm@49100
   241
        case msg: EditorStarted =>
wenzelm@51616
   242
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@51616
   243
            GUI.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   244
            "Prover IDE inactive!")
wenzelm@50205
   245
          PIDE.startup_notified = true
wenzelm@49099
   246
        case _ =>
wenzelm@49099
   247
      }
wenzelm@49099
   248
    }
wenzelm@49099
   249
wenzelm@50205
   250
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   251
      message match {
wenzelm@49099
   252
        case msg: EditorStarted =>
wenzelm@50408
   253
          PIDE.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   254
wenzelm@49099
   255
        case msg: BufferUpdate
wenzelm@49099
   256
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@50205
   257
          if (PIDE.session.is_ready) {
wenzelm@49099
   258
            val buffer = msg.getBuffer
wenzelm@50344
   259
            if (buffer != null && !buffer.isLoading) delay_init.invoke()
wenzelm@53074
   260
            load_theories()
wenzelm@49099
   261
          }
wenzelm@34784
   262
wenzelm@49099
   263
        case msg: EditPaneUpdate
wenzelm@49099
   264
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   265
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   266
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   267
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   268
          val edit_pane = msg.getEditPane
wenzelm@49099
   269
          val buffer = edit_pane.getBuffer
wenzelm@49099
   270
          val text_area = edit_pane.getTextArea
wenzelm@49099
   271
wenzelm@49099
   272
          if (buffer != null && text_area != null) {
wenzelm@49099
   273
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   274
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   275
              if (PIDE.session.is_ready)
wenzelm@50205
   276
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   277
            }
wenzelm@52867
   278
            else {
wenzelm@53244
   279
              PIDE.dismissed_popups(text_area.getView)
wenzelm@52867
   280
              PIDE.exit_view(buffer, text_area)
wenzelm@52867
   281
            }
wenzelm@53274
   282
wenzelm@53274
   283
            if (msg.getWhat == EditPaneUpdate.CREATED)
wenzelm@53274
   284
              Completion_Popup.Text_Area.init(text_area)
wenzelm@53274
   285
wenzelm@53274
   286
            if (msg.getWhat == EditPaneUpdate.DESTROYED)
wenzelm@53274
   287
              Completion_Popup.Text_Area.exit(text_area)
wenzelm@43510
   288
          }
wenzelm@34784
   289
wenzelm@49099
   290
        case msg: PropertiesChanged =>
wenzelm@52084
   291
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   292
wenzelm@49099
   293
        case _ =>
wenzelm@49099
   294
      }
wenzelm@34318
   295
    }
wenzelm@34318
   296
  }
wenzelm@34318
   297
wenzelm@34618
   298
  override def start()
wenzelm@49099
   299
  {
wenzelm@49099
   300
    try {
wenzelm@50709
   301
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   302
wenzelm@50205
   303
      PIDE.plugin = this
wenzelm@49099
   304
      Isabelle_System.init()
wenzelm@51614
   305
      Isabelle_Font.install_fonts()
wenzelm@49099
   306
wenzelm@53338
   307
      PIDE.options.update(Options.init())
wenzelm@53337
   308
      PIDE.completion_history.load()
wenzelm@49245
   309
wenzelm@49099
   310
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   311
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   312
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   313
wenzelm@53274
   314
      JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.init _)
wenzelm@53274
   315
wenzelm@49246
   316
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   317
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   318
wenzelm@50205
   319
      PIDE.session = new Session(thy_load) {
wenzelm@50207
   320
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   321
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   322
      }
wenzelm@48870
   323
wenzelm@50205
   324
      PIDE.session.phase_changed += session_manager
wenzelm@50205
   325
      PIDE.startup_failure = None
wenzelm@49099
   326
    }
wenzelm@49099
   327
    catch {
wenzelm@49099
   328
      case exn: Throwable =>
wenzelm@50205
   329
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   330
        PIDE.startup_notified = false
wenzelm@49099
   331
    }
wenzelm@34318
   332
  }
wenzelm@34618
   333
wenzelm@39628
   334
  override def stop()
wenzelm@34618
   335
  {
wenzelm@53274
   336
    JEdit_Lib.jedit_text_areas.foreach(Completion_Popup.Text_Area.exit _)
wenzelm@53274
   337
wenzelm@53337
   338
    if (PIDE.startup_failure.isEmpty) {
wenzelm@50205
   339
      PIDE.options.value.save_prefs()
wenzelm@53337
   340
      PIDE.completion_history.value.save()
wenzelm@53337
   341
    }
wenzelm@49245
   342
wenzelm@50205
   343
    PIDE.session.phase_changed -= session_manager
wenzelm@50344
   344
    PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   345
    PIDE.session.stop()
wenzelm@34318
   346
  }
wenzelm@34318
   347
}