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