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