src/Tools/jEdit/src/plugin.scala
author wenzelm
Mon Aug 12 11:39:29 2013 +0200 (2013-08-12)
changeset 52971 31926d2c04ee
parent 52876 78989972d5b8
child 52973 d5f7fa1498b7
permissions -rw-r--r--
tuned signature -- more abstract PIDE editor operations;
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@39043
    17
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@50362
    18
import org.gjt.sp.jedit.syntax.ModeProvider
wenzelm@49100
    19
import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
wenzelm@34429
    20
wenzelm@43443
    21
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    22
wenzelm@44721
    23
import scala.actors.Actor._
wenzelm@39630
    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@49245
    31
wenzelm@49099
    32
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    33
  @volatile var startup_notified = false
wenzelm@34784
    34
wenzelm@49101
    35
  @volatile var plugin: Plugin = null
wenzelm@49101
    36
  @volatile var session: Session = new Session(new JEdit_Thy_Load(Set.empty, Outer_Syntax.empty))
wenzelm@49101
    37
wenzelm@52815
    38
  def options_changed() { session.global_options.event(Session.Global_Options(options.value)) }
wenzelm@52815
    39
wenzelm@48870
    40
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    41
    session.thy_load.asInstanceOf[JEdit_Thy_Load]
wenzelm@44955
    42
wenzelm@48884
    43
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48884
    44
  {
wenzelm@48884
    45
    val current_session = session
wenzelm@49101
    46
    if (current_session.recent_syntax == Outer_Syntax.empty) None
wenzelm@49101
    47
    else Some(current_session.recent_syntax)
wenzelm@48884
    48
  }
wenzelm@48884
    49
wenzelm@52971
    50
  lazy val editor = new JEdit_Editor
wenzelm@52971
    51
wenzelm@34784
    52
wenzelm@43449
    53
  /* document model and view */
wenzelm@43449
    54
wenzelm@52876
    55
  def document_snapshot(name: Document.Node.Name): Document.Snapshot =
wenzelm@52876
    56
  {
wenzelm@52876
    57
    Swing_Thread.require()
wenzelm@52876
    58
wenzelm@52876
    59
    JEdit_Lib.jedit_buffer(name.node) match {
wenzelm@52876
    60
      case Some(buffer) =>
wenzelm@52876
    61
        document_model(buffer) match {
wenzelm@52876
    62
          case Some(model) => model.snapshot
wenzelm@52876
    63
          case None => session.snapshot(name)
wenzelm@52876
    64
        }
wenzelm@52876
    65
      case None => session.snapshot(name)
wenzelm@52876
    66
    }
wenzelm@52876
    67
  }
wenzelm@52876
    68
wenzelm@43449
    69
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
    70
  def document_view(text_area: JEditTextArea): 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@46997
    75
      doc_view = document_view(text_area)
wenzelm@44379
    76
      if doc_view.isDefined
wenzelm@44379
    77
    } yield doc_view.get
wenzelm@44379
    78
wenzelm@50344
    79
  def exit_models(buffers: List[Buffer])
wenzelm@47058
    80
  {
wenzelm@50344
    81
    Swing_Thread.now {
wenzelm@50344
    82
      buffers.foreach(buffer =>
wenzelm@50344
    83
        JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    84
          JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@50344
    85
          Document_Model.exit(buffer)
wenzelm@50344
    86
        })
wenzelm@50344
    87
      }
wenzelm@47058
    88
  }
wenzelm@47058
    89
wenzelm@50344
    90
  def init_models(buffers: List[Buffer])
wenzelm@43510
    91
  {
wenzelm@50344
    92
    Swing_Thread.now {
wenzelm@50344
    93
      val init_edits =
wenzelm@50344
    94
        (List.empty[Document.Edit_Text] /: buffers) { case (edits, buffer) =>
wenzelm@50344
    95
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@50344
    96
            val (model_edits, opt_model) =
wenzelm@50566
    97
              thy_load.buffer_node_name(buffer) match {
wenzelm@50344
    98
                case Some(node_name) =>
wenzelm@50344
    99
                  document_model(buffer) match {
wenzelm@50344
   100
                    case Some(model) if model.name == node_name => (Nil, Some(model))
wenzelm@50344
   101
                    case _ =>
wenzelm@50344
   102
                      val model = Document_Model.init(session, buffer, node_name)
wenzelm@50344
   103
                      (model.init_edits(), Some(model))
wenzelm@50344
   104
                  }
wenzelm@50344
   105
                case None => (Nil, None)
wenzelm@50344
   106
              }
wenzelm@50344
   107
            if (opt_model.isDefined) {
wenzelm@50344
   108
              for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@50344
   109
                if (document_view(text_area).map(_.model) != opt_model)
wenzelm@50344
   110
                  Document_View.init(opt_model.get, text_area)
wenzelm@50344
   111
              }
wenzelm@43510
   112
            }
wenzelm@50344
   113
            model_edits ::: edits
wenzelm@50344
   114
          }
wenzelm@43510
   115
        }
wenzelm@50566
   116
      session.update(init_edits)
wenzelm@43510
   117
    }
wenzelm@43510
   118
  }
wenzelm@43510
   119
wenzelm@43510
   120
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   121
  {
wenzelm@49406
   122
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   123
      document_model(buffer) match {
wenzelm@43510
   124
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
   125
        case None =>
wenzelm@43510
   126
      }
wenzelm@43510
   127
    }
wenzelm@43510
   128
  }
wenzelm@43510
   129
wenzelm@43510
   130
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   131
  {
wenzelm@49406
   132
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   133
      Document_View.exit(text_area)
wenzelm@43510
   134
    }
wenzelm@43510
   135
  }
wenzelm@52816
   136
wenzelm@52816
   137
  def flush_buffers()
wenzelm@52816
   138
  {
wenzelm@52816
   139
    Swing_Thread.require()
wenzelm@52816
   140
wenzelm@52816
   141
    session.update(
wenzelm@52816
   142
      (List.empty[Document.Edit_Text] /: JEdit_Lib.jedit_buffers().toList) {
wenzelm@52816
   143
        case (edits, buffer) =>
wenzelm@52816
   144
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@52816
   145
            document_model(buffer) match {
wenzelm@52816
   146
              case Some(model) => model.flushed_edits() ::: edits
wenzelm@52816
   147
              case None => edits
wenzelm@52816
   148
            }
wenzelm@52816
   149
          }
wenzelm@52816
   150
      }
wenzelm@52816
   151
    )
wenzelm@52816
   152
  }
wenzelm@34318
   153
}
wenzelm@34318
   154
wenzelm@34429
   155
wenzelm@34618
   156
class Plugin extends EBPlugin
wenzelm@34618
   157
{
wenzelm@44577
   158
  /* theory files */
wenzelm@44574
   159
wenzelm@50344
   160
  private lazy val delay_init =
wenzelm@50344
   161
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@50344
   162
    {
wenzelm@50344
   163
      PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50344
   164
    }
wenzelm@50344
   165
wenzelm@44573
   166
  private lazy val delay_load =
wenzelm@50207
   167
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   168
    {
wenzelm@44963
   169
      val view = jEdit.getActiveView()
wenzelm@44963
   170
wenzelm@49406
   171
      val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@46761
   172
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   173
        def loaded_buffer(name: String): Boolean =
wenzelm@49406
   174
          buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   175
wenzelm@46761
   176
        val thys =
wenzelm@50205
   177
          for (buffer <- buffers; model <- PIDE.document_model(buffer))
wenzelm@46761
   178
            yield model.name
wenzelm@46761
   179
wenzelm@50205
   180
        val thy_info = new Thy_Info(PIDE.thy_load)
wenzelm@46761
   181
        // FIXME avoid I/O in Swing thread!?!
wenzelm@51294
   182
        val files = thy_info.dependencies(thys).deps.map(_.name.node).
wenzelm@50205
   183
          filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
wenzelm@44574
   184
wenzelm@46761
   185
        if (!files.isEmpty) {
wenzelm@46761
   186
          val files_list = new ListView(files.sorted)
wenzelm@46761
   187
          for (i <- 0 until files.length)
wenzelm@46761
   188
            files_list.selection.indices += i
wenzelm@44606
   189
wenzelm@46761
   190
          val answer =
wenzelm@51616
   191
            GUI.confirm_dialog(view,
wenzelm@46761
   192
              "Auto loading of required files",
wenzelm@46761
   193
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   194
              "The following files are required to resolve theory imports.",
wenzelm@46761
   195
              "Reload selected files now?",
wenzelm@46761
   196
              new ScrollPane(files_list))
wenzelm@46761
   197
          if (answer == 0) {
wenzelm@46761
   198
            files.foreach(file =>
wenzelm@46761
   199
              if (files_list.selection.items.contains(file))
wenzelm@46761
   200
                jEdit.openFile(null: View, file))
wenzelm@46761
   201
          }
wenzelm@46761
   202
        }
wenzelm@44606
   203
      }
wenzelm@44574
   204
    }
wenzelm@44573
   205
wenzelm@44574
   206
wenzelm@44574
   207
  /* session manager */
wenzelm@44573
   208
wenzelm@39633
   209
  private val session_manager = actor {
wenzelm@39633
   210
    loop {
wenzelm@39633
   211
      react {
wenzelm@39735
   212
        case phase: Session.Phase =>
wenzelm@39735
   213
          phase match {
wenzelm@50808
   214
            case Session.Inactive | Session.Failed =>
wenzelm@46918
   215
              Swing_Thread.later {
wenzelm@51616
   216
                GUI.error_dialog(jEdit.getActiveView, "Prover process terminated",
wenzelm@51616
   217
                    "Isabelle Syslog", GUI.scrollable_text(PIDE.session.current_syslog()))
wenzelm@39735
   218
              }
wenzelm@39735
   219
wenzelm@44573
   220
            case Session.Ready =>
wenzelm@52084
   221
              PIDE.session.update_options(PIDE.options.value)
wenzelm@50344
   222
              PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   223
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   224
wenzelm@46740
   225
            case Session.Shutdown =>
wenzelm@50344
   226
              PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@49195
   227
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   228
wenzelm@39735
   229
            case _ =>
wenzelm@39735
   230
          }
wenzelm@49406
   231
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   232
      }
wenzelm@39630
   233
    }
wenzelm@39630
   234
  }
wenzelm@39630
   235
wenzelm@39630
   236
wenzelm@51071
   237
  /* Mac OS X application hooks */
wenzelm@51071
   238
wenzelm@51071
   239
  def handle_quit(): Boolean =
wenzelm@51071
   240
  {
wenzelm@51071
   241
    jEdit.exit(jEdit.getActiveView(), true)
wenzelm@51071
   242
    false
wenzelm@51071
   243
  }
wenzelm@51071
   244
wenzelm@51071
   245
wenzelm@34618
   246
  /* main plugin plumbing */
wenzelm@34433
   247
wenzelm@34767
   248
  override def handleMessage(message: EBMessage)
wenzelm@34618
   249
  {
wenzelm@43510
   250
    Swing_Thread.assert()
wenzelm@39630
   251
wenzelm@50205
   252
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   253
      message match {
wenzelm@49100
   254
        case msg: EditorStarted =>
wenzelm@51616
   255
          GUI.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@51616
   256
            GUI.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   257
            "Prover IDE inactive!")
wenzelm@50205
   258
          PIDE.startup_notified = true
wenzelm@49099
   259
        case _ =>
wenzelm@49099
   260
      }
wenzelm@49099
   261
    }
wenzelm@49099
   262
wenzelm@50205
   263
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   264
      message match {
wenzelm@49099
   265
        case msg: EditorStarted =>
wenzelm@50408
   266
          PIDE.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   267
wenzelm@49099
   268
        case msg: BufferUpdate
wenzelm@49099
   269
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@50205
   270
          if (PIDE.session.is_ready) {
wenzelm@49099
   271
            val buffer = msg.getBuffer
wenzelm@50344
   272
            if (buffer != null && !buffer.isLoading) delay_init.invoke()
wenzelm@49195
   273
            Swing_Thread.later { delay_load.invoke() }
wenzelm@49099
   274
          }
wenzelm@34784
   275
wenzelm@49099
   276
        case msg: EditPaneUpdate
wenzelm@49099
   277
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   278
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   279
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   280
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   281
          val edit_pane = msg.getEditPane
wenzelm@49099
   282
          val buffer = edit_pane.getBuffer
wenzelm@49099
   283
          val text_area = edit_pane.getTextArea
wenzelm@49099
   284
wenzelm@49099
   285
          if (buffer != null && text_area != null) {
wenzelm@49099
   286
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   287
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   288
              if (PIDE.session.is_ready)
wenzelm@50205
   289
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   290
            }
wenzelm@52867
   291
            else {
wenzelm@52867
   292
              Pretty_Tooltip.dismissed_all()
wenzelm@52867
   293
              PIDE.exit_view(buffer, text_area)
wenzelm@52867
   294
            }
wenzelm@43510
   295
          }
wenzelm@34784
   296
wenzelm@49099
   297
        case msg: PropertiesChanged =>
wenzelm@52084
   298
          PIDE.session.update_options(PIDE.options.value)
wenzelm@34784
   299
wenzelm@49099
   300
        case _ =>
wenzelm@49099
   301
      }
wenzelm@34318
   302
    }
wenzelm@34318
   303
  }
wenzelm@34318
   304
wenzelm@34618
   305
  override def start()
wenzelm@49099
   306
  {
wenzelm@49099
   307
    try {
wenzelm@50709
   308
      Debug.DISABLE_SEARCH_DIALOG_POOL = true
wenzelm@50709
   309
wenzelm@50205
   310
      PIDE.plugin = this
wenzelm@49099
   311
      Isabelle_System.init()
wenzelm@51614
   312
      Isabelle_Font.install_fonts()
wenzelm@49099
   313
wenzelm@49245
   314
      val init_options = Options.init()
wenzelm@50205
   315
      Swing_Thread.now { PIDE.options.update(init_options)  }
wenzelm@49245
   316
wenzelm@51071
   317
      if (Platform.is_macos && PIDE.options.bool("jedit_mac_adapter"))
wenzelm@51071
   318
        OSX_Adapter.set_quit_handler(this, this.getClass.getDeclaredMethod("handle_quit"))
wenzelm@51071
   319
wenzelm@49099
   320
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   321
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   322
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   323
wenzelm@49246
   324
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   325
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   326
wenzelm@50205
   327
      PIDE.session = new Session(thy_load) {
wenzelm@50207
   328
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   329
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   330
      }
wenzelm@48870
   331
wenzelm@50205
   332
      PIDE.session.phase_changed += session_manager
wenzelm@50205
   333
      PIDE.startup_failure = None
wenzelm@49099
   334
    }
wenzelm@49099
   335
    catch {
wenzelm@49099
   336
      case exn: Throwable =>
wenzelm@50205
   337
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   338
        PIDE.startup_notified = false
wenzelm@49099
   339
    }
wenzelm@34318
   340
  }
wenzelm@34618
   341
wenzelm@39628
   342
  override def stop()
wenzelm@34618
   343
  {
wenzelm@50205
   344
    if (PIDE.startup_failure.isEmpty)
wenzelm@50205
   345
      PIDE.options.value.save_prefs()
wenzelm@49245
   346
wenzelm@50205
   347
    PIDE.session.phase_changed -= session_manager
wenzelm@50344
   348
    PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
wenzelm@50205
   349
    PIDE.session.stop()
wenzelm@34318
   350
  }
wenzelm@34318
   351
}