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