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