src/Tools/jEdit/src/plugin.scala
author wenzelm
Sat Sep 22 14:41:41 2012 +0200 (2012-09-22)
changeset 49524 68796a77c42b
parent 49523 dc0670364008
child 49701 e2762f962042
permissions -rw-r--r--
Thy_Syntax.consolidate_spans is subject to editor_reparse_limit, for improved experience of unbalanced comments etc.;
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@37068
    20
import org.gjt.sp.jedit.gui.DockableWindowManager
wenzelm@34429
    21
wenzelm@43443
    22
import org.gjt.sp.util.SyntaxUtilities
wenzelm@39241
    23
wenzelm@44721
    24
import scala.actors.Actor._
wenzelm@39630
    25
wenzelm@34318
    26
wenzelm@34618
    27
object Isabelle
wenzelm@34618
    28
{
wenzelm@34784
    29
  /* plugin instance */
wenzelm@34784
    30
wenzelm@49245
    31
  val options = new JEdit_Options
wenzelm@49245
    32
wenzelm@49099
    33
  @volatile var startup_failure: Option[Throwable] = None
wenzelm@49099
    34
  @volatile var startup_notified = false
wenzelm@34784
    35
wenzelm@49101
    36
  @volatile var plugin: Plugin = null
wenzelm@49101
    37
  @volatile var session: Session = new Session(new JEdit_Thy_Load(Set.empty, Outer_Syntax.empty))
wenzelm@49101
    38
wenzelm@48870
    39
  def thy_load(): JEdit_Thy_Load =
wenzelm@48870
    40
    session.thy_load.asInstanceOf[JEdit_Thy_Load]
wenzelm@44955
    41
wenzelm@48884
    42
  def get_recent_syntax(): Option[Outer_Syntax] =
wenzelm@48884
    43
  {
wenzelm@48884
    44
    val current_session = session
wenzelm@49101
    45
    if (current_session.recent_syntax == Outer_Syntax.empty) None
wenzelm@49101
    46
    else Some(current_session.recent_syntax)
wenzelm@48884
    47
  }
wenzelm@48884
    48
wenzelm@34784
    49
wenzelm@37164
    50
  /* font */
wenzelm@37164
    51
wenzelm@37164
    52
  def font_family(): String = jEdit.getProperty("view.font")
wenzelm@37164
    53
wenzelm@37019
    54
  def font_size(): Float =
wenzelm@49272
    55
    (jEdit.getIntegerProperty("view.fontsize", 16) * options.real("jedit_font_scale")).toFloat
wenzelm@36814
    56
wenzelm@34618
    57
wenzelm@37201
    58
  /* tooltip markup */
wenzelm@37201
    59
wenzelm@37201
    60
  def tooltip(text: String): String =
wenzelm@37203
    61
    "<html><pre style=\"font-family: " + font_family() + "; font-size: " +
wenzelm@49245
    62
        options.int("jedit_tooltip_font_size").toString + "px; \">" +  // FIXME proper scaling (!?)
wenzelm@37203
    63
      HTML.encode(text) + "</pre></html>"
wenzelm@37201
    64
wenzelm@44699
    65
  private val tooltip_lb = Time.seconds(0.5)
wenzelm@44699
    66
  private val tooltip_ub = Time.seconds(60.0)
wenzelm@40849
    67
  def tooltip_dismiss_delay(): Time =
wenzelm@49245
    68
    Time.seconds(options.real("jedit_tooltip_dismiss_delay")) max tooltip_lb min tooltip_ub
wenzelm@38854
    69
wenzelm@38854
    70
  def setup_tooltips()
wenzelm@38854
    71
  {
wenzelm@38854
    72
    Swing_Thread.now {
wenzelm@38854
    73
      val manager = javax.swing.ToolTipManager.sharedInstance
wenzelm@40849
    74
      manager.setDismissDelay(tooltip_dismiss_delay().ms.toInt)
wenzelm@38854
    75
    }
wenzelm@38854
    76
  }
wenzelm@38854
    77
wenzelm@37201
    78
wenzelm@43449
    79
  /* document model and view */
wenzelm@43449
    80
wenzelm@43449
    81
  def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
wenzelm@43449
    82
  def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
wenzelm@43449
    83
wenzelm@44379
    84
  def document_views(buffer: Buffer): List[Document_View] =
wenzelm@44379
    85
    for {
wenzelm@49406
    86
      text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
wenzelm@46997
    87
      doc_view = document_view(text_area)
wenzelm@44379
    88
      if doc_view.isDefined
wenzelm@44379
    89
    } yield doc_view.get
wenzelm@44379
    90
wenzelm@47058
    91
  def exit_model(buffer: Buffer)
wenzelm@47058
    92
  {
wenzelm@49406
    93
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@49406
    94
      JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
wenzelm@47058
    95
      Document_Model.exit(buffer)
wenzelm@47058
    96
    }
wenzelm@47058
    97
  }
wenzelm@47058
    98
wenzelm@43510
    99
  def init_model(buffer: Buffer)
wenzelm@43510
   100
  {
wenzelm@49406
   101
    JEdit_Lib.swing_buffer_lock(buffer) {
wenzelm@43510
   102
      val opt_model =
wenzelm@49406
   103
        JEdit_Lib.buffer_node_name(buffer) match {
wenzelm@48717
   104
          case Some(node_name) =>
wenzelm@47058
   105
            document_model(buffer) match {
wenzelm@47058
   106
              case Some(model) if model.name == node_name => Some(model)
wenzelm@47058
   107
              case _ => Some(Document_Model.init(session, buffer, node_name))
wenzelm@43510
   108
            }
wenzelm@47058
   109
          case None => None
wenzelm@43510
   110
        }
wenzelm@43510
   111
      if (opt_model.isDefined) {
wenzelm@49406
   112
        for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
wenzelm@43510
   113
          if (document_view(text_area).map(_.model) != opt_model)
wenzelm@43510
   114
            Document_View.init(opt_model.get, text_area)
wenzelm@43510
   115
        }
wenzelm@43510
   116
      }
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@43510
   136
wenzelm@43449
   137
wenzelm@37068
   138
  /* dockable windows */
wenzelm@37068
   139
wenzelm@37068
   140
  private def wm(view: View): DockableWindowManager = view.getDockableWindowManager
wenzelm@37068
   141
wenzelm@39515
   142
  def docked_session(view: View): Option[Session_Dockable] =
wenzelm@39515
   143
    wm(view).getDockableWindow("isabelle-session") match {
wenzelm@39515
   144
      case dockable: Session_Dockable => Some(dockable)
wenzelm@39515
   145
      case _ => None
wenzelm@39515
   146
    }
wenzelm@39515
   147
wenzelm@37068
   148
  def docked_output(view: View): Option[Output_Dockable] =
wenzelm@37068
   149
    wm(view).getDockableWindow("isabelle-output") match {
wenzelm@37068
   150
      case dockable: Output_Dockable => Some(dockable)
wenzelm@37068
   151
      case _ => None
wenzelm@37068
   152
    }
wenzelm@37068
   153
wenzelm@37068
   154
  def docked_raw_output(view: View): Option[Raw_Output_Dockable] =
wenzelm@37068
   155
    wm(view).getDockableWindow("isabelle-raw-output") match {
wenzelm@37068
   156
      case dockable: Raw_Output_Dockable => Some(dockable)
wenzelm@37068
   157
      case _ => None
wenzelm@37068
   158
    }
wenzelm@37068
   159
wenzelm@37068
   160
  def docked_protocol(view: View): Option[Protocol_Dockable] =
wenzelm@37068
   161
    wm(view).getDockableWindow("isabelle-protocol") match {
wenzelm@37068
   162
      case dockable: Protocol_Dockable => Some(dockable)
wenzelm@37068
   163
      case _ => None
wenzelm@37068
   164
    }
wenzelm@37068
   165
wenzelm@37068
   166
wenzelm@44238
   167
  /* convenience actions */
wenzelm@44238
   168
wenzelm@44238
   169
  private def user_input(text_area: JEditTextArea, s1: String, s2: String = "")
wenzelm@44238
   170
  {
wenzelm@44238
   171
    s1.foreach(text_area.userInput(_))
wenzelm@44238
   172
    s2.foreach(text_area.userInput(_))
wenzelm@44238
   173
    s2.foreach(_ => text_area.goToPrevCharacter(false))
wenzelm@44238
   174
  }
wenzelm@44238
   175
wenzelm@44238
   176
  def input_sub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sub_decoded)
wenzelm@44238
   177
  def input_sup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.sup_decoded)
wenzelm@44238
   178
  def input_isub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isub_decoded)
wenzelm@44238
   179
  def input_isup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.isup_decoded)
wenzelm@44238
   180
  def input_bsub(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsub_decoded, Symbol.esub_decoded)
wenzelm@44238
   181
  def input_bsup(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bsup_decoded, Symbol.esup_decoded)
wenzelm@44238
   182
  def input_bold(text_area: JEditTextArea): Unit = user_input(text_area, Symbol.bold_decoded)
wenzelm@44864
   183
wenzelm@44864
   184
  def check_buffer(buffer: Buffer)
wenzelm@44864
   185
  {
wenzelm@44864
   186
    document_model(buffer) match {
wenzelm@44864
   187
      case None =>
wenzelm@44864
   188
      case Some(model) => model.full_perspective()
wenzelm@44864
   189
    }
wenzelm@44864
   190
  }
wenzelm@44864
   191
wenzelm@44864
   192
  def cancel_execution() { session.cancel_execution() }
wenzelm@34318
   193
}
wenzelm@34318
   194
wenzelm@34429
   195
wenzelm@34618
   196
class Plugin extends EBPlugin
wenzelm@34618
   197
{
wenzelm@44577
   198
  /* theory files */
wenzelm@44574
   199
wenzelm@44573
   200
  private lazy val delay_load =
wenzelm@49288
   201
    Swing_Thread.delay_last(Time.seconds(Isabelle.options.real("editor_load_delay")))
wenzelm@44574
   202
    {
wenzelm@44963
   203
      val view = jEdit.getActiveView()
wenzelm@44963
   204
wenzelm@49406
   205
      val buffers = JEdit_Lib.jedit_buffers().toList
wenzelm@46761
   206
      if (buffers.forall(_.isLoaded)) {
wenzelm@46761
   207
        def loaded_buffer(name: String): Boolean =
wenzelm@49406
   208
          buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
wenzelm@44574
   209
wenzelm@46761
   210
        val thys =
wenzelm@46761
   211
          for (buffer <- buffers; model <- Isabelle.document_model(buffer))
wenzelm@46761
   212
            yield model.name
wenzelm@46761
   213
wenzelm@48870
   214
        val thy_info = new Thy_Info(Isabelle.thy_load)
wenzelm@46761
   215
        // FIXME avoid I/O in Swing thread!?!
wenzelm@49098
   216
        val files = thy_info.dependencies(true, thys).deps.map(_._1.node).
wenzelm@46761
   217
          filter(file => !loaded_buffer(file) && Isabelle.thy_load.check_file(view, file))
wenzelm@44574
   218
wenzelm@46761
   219
        if (!files.isEmpty) {
wenzelm@46761
   220
          val files_list = new ListView(files.sorted)
wenzelm@46761
   221
          for (i <- 0 until files.length)
wenzelm@46761
   222
            files_list.selection.indices += i
wenzelm@44606
   223
wenzelm@46761
   224
          val answer =
wenzelm@46761
   225
            Library.confirm_dialog(view,
wenzelm@46761
   226
              "Auto loading of required files",
wenzelm@46761
   227
              JOptionPane.YES_NO_OPTION,
wenzelm@46761
   228
              "The following files are required to resolve theory imports.",
wenzelm@46761
   229
              "Reload selected files now?",
wenzelm@46761
   230
              new ScrollPane(files_list))
wenzelm@46761
   231
          if (answer == 0) {
wenzelm@46761
   232
            files.foreach(file =>
wenzelm@46761
   233
              if (files_list.selection.items.contains(file))
wenzelm@46761
   234
                jEdit.openFile(null: View, file))
wenzelm@46761
   235
          }
wenzelm@46761
   236
        }
wenzelm@44606
   237
      }
wenzelm@44574
   238
    }
wenzelm@44573
   239
wenzelm@44574
   240
wenzelm@44574
   241
  /* session manager */
wenzelm@44573
   242
wenzelm@39633
   243
  private val session_manager = actor {
wenzelm@39633
   244
    loop {
wenzelm@39633
   245
      react {
wenzelm@39735
   246
        case phase: Session.Phase =>
wenzelm@39735
   247
          phase match {
wenzelm@39735
   248
            case Session.Failed =>
wenzelm@46918
   249
              Swing_Thread.later {
wenzelm@48022
   250
                Library.error_dialog(jEdit.getActiveView, "Prover process failure",
wenzelm@48022
   251
                    "Isabelle Syslog", Library.scrollable_text(Isabelle.session.current_syslog()))
wenzelm@39735
   252
              }
wenzelm@39735
   253
wenzelm@44573
   254
            case Session.Ready =>
wenzelm@49406
   255
              JEdit_Lib.jedit_buffers.foreach(Isabelle.init_model)
wenzelm@49195
   256
              Swing_Thread.later { delay_load.invoke() }
wenzelm@44573
   257
wenzelm@46740
   258
            case Session.Shutdown =>
wenzelm@49406
   259
              JEdit_Lib.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@49195
   260
              Swing_Thread.later { delay_load.revoke() }
wenzelm@46740
   261
wenzelm@39735
   262
            case _ =>
wenzelm@39735
   263
          }
wenzelm@49406
   264
        case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
wenzelm@39630
   265
      }
wenzelm@39630
   266
    }
wenzelm@39630
   267
  }
wenzelm@39630
   268
wenzelm@39630
   269
wenzelm@34618
   270
  /* main plugin plumbing */
wenzelm@34433
   271
wenzelm@34767
   272
  override def handleMessage(message: EBMessage)
wenzelm@34618
   273
  {
wenzelm@43510
   274
    Swing_Thread.assert()
wenzelm@39630
   275
wenzelm@49099
   276
    if (Isabelle.startup_failure.isDefined && !Isabelle.startup_notified) {
wenzelm@49099
   277
      message match {
wenzelm@49100
   278
        case msg: EditorStarted =>
wenzelm@49100
   279
          Library.error_dialog(null, "Isabelle plugin startup failure",
wenzelm@49099
   280
            Library.scrollable_text(Exn.message(Isabelle.startup_failure.get)),
wenzelm@49099
   281
            "Prover IDE inactive!")
wenzelm@49099
   282
          Isabelle.startup_notified = true
wenzelm@49099
   283
        case _ =>
wenzelm@49099
   284
      }
wenzelm@49099
   285
    }
wenzelm@49099
   286
wenzelm@49099
   287
    if (Isabelle.startup_failure.isEmpty) {
wenzelm@49099
   288
      message match {
wenzelm@49099
   289
        case msg: EditorStarted =>
wenzelm@49245
   290
          if (Isabelle.options.bool("jedit_auto_start"))
wenzelm@49246
   291
            Isabelle.session.start(Isabelle_Logic.session_args())
wenzelm@37557
   292
wenzelm@49099
   293
        case msg: BufferUpdate
wenzelm@49099
   294
        if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
wenzelm@49099
   295
          if (Isabelle.session.is_ready) {
wenzelm@49099
   296
            val buffer = msg.getBuffer
wenzelm@49099
   297
            if (buffer != null && !buffer.isLoading) Isabelle.init_model(buffer)
wenzelm@49195
   298
            Swing_Thread.later { delay_load.invoke() }
wenzelm@49099
   299
          }
wenzelm@34784
   300
wenzelm@49099
   301
        case msg: EditPaneUpdate
wenzelm@49099
   302
        if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
wenzelm@49099
   303
            msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   304
            msg.getWhat == EditPaneUpdate.CREATED ||
wenzelm@49099
   305
            msg.getWhat == EditPaneUpdate.DESTROYED) =>
wenzelm@49099
   306
          val edit_pane = msg.getEditPane
wenzelm@49099
   307
          val buffer = edit_pane.getBuffer
wenzelm@49099
   308
          val text_area = edit_pane.getTextArea
wenzelm@49099
   309
wenzelm@49099
   310
          if (buffer != null && text_area != null) {
wenzelm@49099
   311
            if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
wenzelm@49099
   312
                msg.getWhat == EditPaneUpdate.CREATED) {
wenzelm@49099
   313
              if (Isabelle.session.is_ready)
wenzelm@49099
   314
                Isabelle.init_view(buffer, text_area)
wenzelm@49099
   315
            }
wenzelm@49099
   316
            else Isabelle.exit_view(buffer, text_area)
wenzelm@43510
   317
          }
wenzelm@34784
   318
wenzelm@49099
   319
        case msg: PropertiesChanged =>
wenzelm@49099
   320
          Isabelle.setup_tooltips()
wenzelm@49523
   321
          Isabelle.session.global_options.event(Session.Global_Options)
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@49099
   331
      Isabelle.plugin = this
wenzelm@49099
   332
      Isabelle_System.init()
wenzelm@49099
   333
      Isabelle_System.install_fonts()
wenzelm@49099
   334
wenzelm@49245
   335
      val init_options = Options.init()
wenzelm@49245
   336
      Swing_Thread.now {
wenzelm@49245
   337
        Isabelle.options.update(init_options)
wenzelm@49245
   338
        Isabelle.setup_tooltips()
wenzelm@49245
   339
      }
wenzelm@49245
   340
wenzelm@49099
   341
      SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
wenzelm@49099
   342
      if (ModeProvider.instance.isInstanceOf[ModeProvider])
wenzelm@49099
   343
        ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
wenzelm@48870
   344
wenzelm@49246
   345
      val content = Isabelle_Logic.session_content(false)
wenzelm@49099
   346
      val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
wenzelm@49524
   347
wenzelm@49288
   348
      Isabelle.session = new Session(thy_load) {
wenzelm@49288
   349
        override def output_delay = Time.seconds(Isabelle.options.real("editor_output_delay"))
wenzelm@49524
   350
        override def reparse_limit = Isabelle.options.int("editor_reparse_limit")
wenzelm@49288
   351
      }
wenzelm@48870
   352
wenzelm@49099
   353
      Isabelle.session.phase_changed += session_manager
wenzelm@49099
   354
      Isabelle.startup_failure = None
wenzelm@49099
   355
    }
wenzelm@49099
   356
    catch {
wenzelm@49099
   357
      case exn: Throwable =>
wenzelm@49099
   358
        Isabelle.startup_failure = Some(exn)
wenzelm@49099
   359
        Isabelle.startup_notified = false
wenzelm@49099
   360
    }
wenzelm@34318
   361
  }
wenzelm@34618
   362
wenzelm@39628
   363
  override def stop()
wenzelm@34618
   364
  {
wenzelm@49245
   365
    if (Isabelle.startup_failure.isEmpty)
wenzelm@49245
   366
      Isabelle.options.value.save_prefs()
wenzelm@49245
   367
wenzelm@49101
   368
    Isabelle.session.phase_changed -= session_manager
wenzelm@49406
   369
    JEdit_Lib.jedit_buffers.foreach(Isabelle.exit_model)
wenzelm@49101
   370
    Isabelle.session.stop()
wenzelm@34318
   371
  }
wenzelm@34318
   372
}