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