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