src/Tools/jEdit/src/plugin.scala
author wenzelm
Sun Nov 25 21:40:34 2012 +0100 (2012-11-25)
changeset 50209 907373a080b9
parent 50208 1382ad6d4774
child 50344 608265769ce0
permissions -rw-r--r--
tuned signature;
wenzelm@43282
     1
/*  Title:      Tools/jEdit/src/plugin.scala
wenzelm@36760
     2
    Author:     Makarius
wenzelm@36760
     3
wenzelm@36760
     4
Main Isabelle/jEdit plugin setup.
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@49406
    16
import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View}
wenzelm@39043
    17
import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
wenzelm@43452
    18
import org.gjt.sp.jedit.syntax.{Token => JEditToken, 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@48870
    38
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    39
    session.thy_load.asInstanceOf[JEdit_Thy_Load]
wenzelm@44955
    40
wenzelm@48884
    41
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48884
    42
  {
wenzelm@48884
    43
    val current_session = session
wenzelm@49101
    44
    if (current_session.recent_syntax == Outer_Syntax.empty) None
wenzelm@49101
    45
    else Some(current_session.recent_syntax)
wenzelm@48884
    46
  }
wenzelm@48884
    47
wenzelm@34784
    48
wenzelm@43449
    49
  /* document model and view */
wenzelm@43449
    50
wenzelm@43449
    51
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
    52
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    53
wenzelm@44379
    54
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    55
    for {
wenzelm@49406
    56
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@46997
    57
      doc_view = document_view(text_area)
wenzelm@44379
    58
      if doc_view.isDefined
wenzelm@44379
    59
    } yield doc_view.get
wenzelm@44379
    60
wenzelm@47058
    61
  def exit_model(buffer: Buffer)
wenzelm@47058
    62
  {
wenzelm@49406
    63
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@49406
    64
      JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@47058
    65
      Document_Model.exit(buffer)
wenzelm@47058
    66
    }
wenzelm@47058
    67
  }
wenzelm@47058
    68
wenzelm@43510
    69
  def init_model(buffer: Buffer)
wenzelm@43510
    70
  {
wenzelm@49406
    71
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
    72
      val opt_model =
wenzelm@49406
    73
        JEdit_Lib.buffer_node_name(buffer) match {
wenzelm@48717
    74
          case Some(node_name) =>
wenzelm@47058
    75
            document_model(buffer) match {
wenzelm@47058
    76
              case Some(model) if model.name == node_name => Some(model)
wenzelm@47058
    77
              case _ => Some(Document_Model.init(session, buffer, node_name))
wenzelm@43510
    78
            }
wenzelm@47058
    79
          case None => None
wenzelm@43510
    80
        }
wenzelm@43510
    81
      if (opt_model.isDefined) {
wenzelm@49406
    82
        for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@43510
    83
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
    84
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
    85
        }
wenzelm@43510
    86
      }
wenzelm@43510
    87
    }
wenzelm@43510
    88
  }
wenzelm@43510
    89
wenzelm@43510
    90
  def init_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
    91
  {
wenzelm@49406
    92
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
    93
      document_model(buffer) match {
wenzelm@43510
    94
        case Some(model) => Document_View.init(model, text_area)
wenzelm@43510
    95
        case None =>
wenzelm@43510
    96
      }
wenzelm@43510
    97
    }
wenzelm@43510
    98
  }
wenzelm@43510
    99
wenzelm@43510
   100
  def exit_view(buffer: Buffer, text_area: JEditTextArea)
wenzelm@43510
   101
  {
wenzelm@49406
   102
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   103
      Document_View.exit(text_area)
wenzelm@43510
   104
    }
wenzelm@43510
   105
  }
wenzelm@50209
   106
wenzelm@50209
   107
wenzelm@50209
   108
  /* full checking */
wenzelm@50209
   109
wenzelm@50209
   110
  def check_buffer(buffer: Buffer)
wenzelm@50209
   111
  {
wenzelm@50209
   112
    PIDE.document_model(buffer) match {
wenzelm@50209
   113
      case None =>
wenzelm@50209
   114
      case Some(model) => model.full_perspective()
wenzelm@50209
   115
    }
wenzelm@50209
   116
  }
wenzelm@50209
   117
wenzelm@50209
   118
  def cancel_execution() { PIDE.session.cancel_execution() }
wenzelm@34318
   119
}
wenzelm@34318
   120
wenzelm@34429
   121
wenzelm@34618
   122
class Plugin extends EBPlugin
wenzelm@34618
   123
{
wenzelm@44577
   124
  /* theory files */
wenzelm@44574
   125
wenzelm@44573
   126
  private lazy val delay_load =
wenzelm@50207
   127
    Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
wenzelm@44574
   128
    {
wenzelm@44963
   129
      val view = jEdit.getActiveView()
wenzelm@44963
   130
wenzelm@49406
   131
      val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@46761
   132
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   133
        def loaded_buffer(name: String): Boolean =
wenzelm@49406
   134
          buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   135
wenzelm@46761
   136
        val thys =
wenzelm@50205
   137
          for (buffer <- buffers; model <- PIDE.document_model(buffer))
wenzelm@46761
   138
            yield model.name
wenzelm@46761
   139
wenzelm@50205
   140
        val thy_info = new Thy_Info(PIDE.thy_load)
wenzelm@46761
   141
        // FIXME avoid I/O in Swing thread!?!
wenzelm@49098
   142
        val files = thy_info.dependencies(true, thys).deps.map(_._1.node).
wenzelm@50205
   143
          filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
wenzelm@44574
   144
wenzelm@46761
   145
        if (!files.isEmpty) {
wenzelm@46761
   146
          val files_list = new ListView(files.sorted)
wenzelm@46761
   147
          for (i <- 0 until files.length)
wenzelm@46761
   148
            files_list.selection.indices += i
wenzelm@44606
   149
wenzelm@46761
   150
          val answer =
wenzelm@46761
   151
            Library.confirm_dialog(view,
wenzelm@46761
   152
              "Auto loading of required files",
wenzelm@46761
   153
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   154
              "The following files are required to resolve theory imports.",
wenzelm@46761
   155
              "Reload selected files now?",
wenzelm@46761
   156
              new ScrollPane(files_list))
wenzelm@46761
   157
          if (answer == 0) {
wenzelm@46761
   158
            files.foreach(file =>
wenzelm@46761
   159
              if (files_list.selection.items.contains(file))
wenzelm@46761
   160
                jEdit.openFile(null: View, file))
wenzelm@46761
   161
          }
wenzelm@46761
   162
        }
wenzelm@44606
   163
      }
wenzelm@44574
   164
    }
wenzelm@44573
   165
wenzelm@44574
   166
wenzelm@44574
   167
  /* session manager */
wenzelm@44573
   168
wenzelm@39633
   169
  private val session_manager = actor {
wenzelm@39633
   170
    loop {
wenzelm@39633
   171
      react {
wenzelm@39735
   172
        case phase: Session.Phase =>
wenzelm@39735
   173
          phase match {
wenzelm@39735
   174
            case Session.Failed =>
wenzelm@46918
   175
              Swing_Thread.later {
wenzelm@48022
   176
                Library.error_dialog(jEdit.getActiveView, "Prover process failure",
wenzelm@50205
   177
                    "Isabelle Syslog", Library.scrollable_text(PIDE.session.current_syslog()))
wenzelm@39735
   178
              }
wenzelm@39735
   179
wenzelm@44573
   180
            case Session.Ready =>
wenzelm@50205
   181
              PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
wenzelm@50205
   182
              JEdit_Lib.jedit_buffers.foreach(PIDE.init_model)
wenzelm@49195
   183
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   184
wenzelm@46740
   185
            case Session.Shutdown =>
wenzelm@50205
   186
              JEdit_Lib.jedit_buffers.foreach(PIDE.exit_model)
wenzelm@49195
   187
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   188
wenzelm@39735
   189
            case _ =>
wenzelm@39735
   190
          }
wenzelm@49406
   191
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   192
      }
wenzelm@39630
   193
    }
wenzelm@39630
   194
  }
wenzelm@39630
   195
wenzelm@39630
   196
wenzelm@34618
   197
  /* main plugin plumbing */
wenzelm@34433
   198
wenzelm@34767
   199
  override def handleMessage(message: EBMessage)
wenzelm@34618
   200
  {
wenzelm@43510
   201
    Swing_Thread.assert()
wenzelm@39630
   202
wenzelm@50205
   203
    if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
wenzelm@49099
   204
      message match {
wenzelm@49100
   205
        case msg: EditorStarted =>
wenzelm@49100
   206
          Library.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@50205
   207
            Library.scrollable_text(Exn.message(PIDE.startup_failure.get)),
wenzelm@49099
   208
            "Prover IDE inactive!")
wenzelm@50205
   209
          PIDE.startup_notified = true
wenzelm@49099
   210
        case _ =>
wenzelm@49099
   211
      }
wenzelm@49099
   212
    }
wenzelm@49099
   213
wenzelm@50205
   214
    if (PIDE.startup_failure.isEmpty) {
wenzelm@49099
   215
      message match {
wenzelm@49099
   216
        case msg: EditorStarted =>
wenzelm@50205
   217
          if (PIDE.options.bool("jedit_auto_start"))
wenzelm@50205
   218
            PIDE.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   219
wenzelm@49099
   220
        case msg: BufferUpdate
wenzelm@49099
   221
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@50205
   222
          if (PIDE.session.is_ready) {
wenzelm@49099
   223
            val buffer = msg.getBuffer
wenzelm@50205
   224
            if (buffer != null && !buffer.isLoading) PIDE.init_model(buffer)
wenzelm@49195
   225
            Swing_Thread.later { delay_load.invoke() }
wenzelm@49099
   226
          }
wenzelm@34784
   227
wenzelm@49099
   228
        case msg: EditPaneUpdate
wenzelm@49099
   229
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   230
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   231
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   232
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   233
          val edit_pane = msg.getEditPane
wenzelm@49099
   234
          val buffer = edit_pane.getBuffer
wenzelm@49099
   235
          val text_area = edit_pane.getTextArea
wenzelm@49099
   236
wenzelm@49099
   237
          if (buffer != null && text_area != null) {
wenzelm@49099
   238
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   239
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@50205
   240
              if (PIDE.session.is_ready)
wenzelm@50205
   241
                PIDE.init_view(buffer, text_area)
wenzelm@49099
   242
            }
wenzelm@50205
   243
            else PIDE.exit_view(buffer, text_area)
wenzelm@43510
   244
          }
wenzelm@34784
   245
wenzelm@49099
   246
        case msg: PropertiesChanged =>
wenzelm@50205
   247
          PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
wenzelm@34784
   248
wenzelm@49099
   249
        case _ =>
wenzelm@49099
   250
      }
wenzelm@34318
   251
    }
wenzelm@34318
   252
  }
wenzelm@34318
   253
wenzelm@34618
   254
  override def start()
wenzelm@49099
   255
  {
wenzelm@49099
   256
    try {
wenzelm@50205
   257
      PIDE.plugin = this
wenzelm@49099
   258
      Isabelle_System.init()
wenzelm@49099
   259
      Isabelle_System.install_fonts()
wenzelm@49099
   260
wenzelm@49245
   261
      val init_options = Options.init()
wenzelm@50205
   262
      Swing_Thread.now { PIDE.options.update(init_options)  }
wenzelm@49245
   263
wenzelm@49099
   264
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   265
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   266
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   267
wenzelm@49246
   268
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   269
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   270
wenzelm@50205
   271
      PIDE.session = new Session(thy_load) {
wenzelm@50207
   272
        override def output_delay = PIDE.options.seconds("editor_output_delay")
wenzelm@50205
   273
        override def reparse_limit = PIDE.options.int("editor_reparse_limit")
wenzelm@49288
   274
      }
wenzelm@48870
   275
wenzelm@50205
   276
      PIDE.session.phase_changed += session_manager
wenzelm@50205
   277
      PIDE.startup_failure = None
wenzelm@49099
   278
    }
wenzelm@49099
   279
    catch {
wenzelm@49099
   280
      case exn: Throwable =>
wenzelm@50205
   281
        PIDE.startup_failure = Some(exn)
wenzelm@50205
   282
        PIDE.startup_notified = false
wenzelm@49099
   283
    }
wenzelm@34318
   284
  }
wenzelm@34618
   285
wenzelm@39628
   286
  override def stop()
wenzelm@34618
   287
  {
wenzelm@50205
   288
    if (PIDE.startup_failure.isEmpty)
wenzelm@50205
   289
      PIDE.options.value.save_prefs()
wenzelm@49245
   290
wenzelm@50205
   291
    PIDE.session.phase_changed -= session_manager
wenzelm@50205
   292
    JEdit_Lib.jedit_buffers.foreach(PIDE.exit_model)
wenzelm@50205
   293
    PIDE.session.stop()
wenzelm@34318
   294
  }
wenzelm@34318
   295
}