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