src/Tools/jEdit/src/plugin.scala
author wenzelm
Wed Dec 05 11:34:04 2012 +0100 (2012-12-05)
changeset 50362 1a539d7a0438
parent 50344 608265769ce0
child 50363 2f8dc9e65401
permissions -rw-r--r--
tuned;
     1 /*  Title:      Tools/jEdit/src/plugin.scala
     2     Author:     Makarius
     3 
     4 Main plumbing for PIDE infrastructure as jEdit plugin.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import javax.swing.JOptionPane
    13 
    14 import scala.swing.{ListView, ScrollPane}
    15 
    16 import org.gjt.sp.jedit.{jEdit, EBMessage, EBPlugin, Buffer, View}
    17 import org.gjt.sp.jedit.textarea.{JEditTextArea, TextArea}
    18 import org.gjt.sp.jedit.syntax.ModeProvider
    19 import org.gjt.sp.jedit.msg.{EditorStarted, BufferUpdate, EditPaneUpdate, PropertiesChanged}
    20 
    21 import org.gjt.sp.util.SyntaxUtilities
    22 
    23 import scala.actors.Actor._
    24 
    25 
    26 object PIDE
    27 {
    28   /* plugin instance */
    29 
    30   val options = new JEdit_Options
    31 
    32   @volatile var startup_failure: Option[Throwable] = None
    33   @volatile var startup_notified = false
    34 
    35   @volatile var plugin: Plugin = null
    36   @volatile var session: Session = new Session(new JEdit_Thy_Load(Set.empty, Outer_Syntax.empty))
    37 
    38   def thy_load(): JEdit_Thy_Load =
    39     session.thy_load.asInstanceOf[JEdit_Thy_Load]
    40 
    41   def get_recent_syntax(): Option[Outer_Syntax] =
    42   {
    43     val current_session = session
    44     if (current_session.recent_syntax == Outer_Syntax.empty) None
    45     else Some(current_session.recent_syntax)
    46   }
    47 
    48 
    49   /* document model and view */
    50 
    51   def document_model(buffer: Buffer): Option[Document_Model] = Document_Model(buffer)
    52   def document_view(text_area: JEditTextArea): Option[Document_View] = Document_View(text_area)
    53 
    54   def document_views(buffer: Buffer): List[Document_View] =
    55     for {
    56       text_area <- JEdit_Lib.jedit_text_areas(buffer).toList
    57       doc_view = document_view(text_area)
    58       if doc_view.isDefined
    59     } yield doc_view.get
    60 
    61   def exit_models(buffers: List[Buffer])
    62   {
    63     Swing_Thread.now {
    64       buffers.foreach(buffer =>
    65         JEdit_Lib.buffer_lock(buffer) {
    66           JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
    67           Document_Model.exit(buffer)
    68         })
    69       }
    70   }
    71 
    72   def init_models(buffers: List[Buffer])
    73   {
    74     Swing_Thread.now {
    75       val init_edits =
    76         (List.empty[Document.Edit_Text] /: buffers) { case (edits, buffer) =>
    77           JEdit_Lib.buffer_lock(buffer) {
    78             val (model_edits, opt_model) =
    79               JEdit_Lib.buffer_node_name(buffer) match {
    80                 case Some(node_name) =>
    81                   document_model(buffer) match {
    82                     case Some(model) if model.name == node_name => (Nil, Some(model))
    83                     case _ =>
    84                       val model = Document_Model.init(session, buffer, node_name)
    85                       (model.init_edits(), Some(model))
    86                   }
    87                 case None => (Nil, None)
    88               }
    89             if (opt_model.isDefined) {
    90               for (text_area <- JEdit_Lib.jedit_text_areas(buffer)) {
    91                 if (document_view(text_area).map(_.model) != opt_model)
    92                   Document_View.init(opt_model.get, text_area)
    93               }
    94             }
    95             model_edits ::: edits
    96           }
    97         }
    98       PIDE.session.edit(init_edits)
    99     }
   100   }
   101 
   102   def init_view(buffer: Buffer, text_area: JEditTextArea)
   103   {
   104     JEdit_Lib.swing_buffer_lock(buffer) {
   105       document_model(buffer) match {
   106         case Some(model) => Document_View.init(model, text_area)
   107         case None =>
   108       }
   109     }
   110   }
   111 
   112   def exit_view(buffer: Buffer, text_area: JEditTextArea)
   113   {
   114     JEdit_Lib.swing_buffer_lock(buffer) {
   115       Document_View.exit(text_area)
   116     }
   117   }
   118 
   119 
   120   /* full checking */
   121 
   122   def check_buffer(buffer: Buffer)
   123   {
   124     PIDE.document_model(buffer) match {
   125       case Some(model) => model.full_perspective()
   126       case None =>
   127     }
   128   }
   129 
   130   def cancel_execution() { PIDE.session.cancel_execution() }
   131 }
   132 
   133 
   134 class Plugin extends EBPlugin
   135 {
   136   /* theory files */
   137 
   138   private lazy val delay_init =
   139     Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
   140     {
   141       PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
   142     }
   143 
   144   private lazy val delay_load =
   145     Swing_Thread.delay_last(PIDE.options.seconds("editor_load_delay"))
   146     {
   147       val view = jEdit.getActiveView()
   148 
   149       val buffers = JEdit_Lib.jedit_buffers().toList
   150       if (buffers.forall(_.isLoaded)) {
   151         def loaded_buffer(name: String): Boolean =
   152           buffers.exists(buffer => JEdit_Lib.buffer_name(buffer) == name)
   153 
   154         val thys =
   155           for (buffer <- buffers; model <- PIDE.document_model(buffer))
   156             yield model.name
   157 
   158         val thy_info = new Thy_Info(PIDE.thy_load)
   159         // FIXME avoid I/O in Swing thread!?!
   160         val files = thy_info.dependencies(true, thys).deps.map(_._1.node).
   161           filter(file => !loaded_buffer(file) && PIDE.thy_load.check_file(view, file))
   162 
   163         if (!files.isEmpty) {
   164           val files_list = new ListView(files.sorted)
   165           for (i <- 0 until files.length)
   166             files_list.selection.indices += i
   167 
   168           val answer =
   169             Library.confirm_dialog(view,
   170               "Auto loading of required files",
   171               JOptionPane.YES_NO_OPTION,
   172               "The following files are required to resolve theory imports.",
   173               "Reload selected files now?",
   174               new ScrollPane(files_list))
   175           if (answer == 0) {
   176             files.foreach(file =>
   177               if (files_list.selection.items.contains(file))
   178                 jEdit.openFile(null: View, file))
   179           }
   180         }
   181       }
   182     }
   183 
   184 
   185   /* session manager */
   186 
   187   private val session_manager = actor {
   188     loop {
   189       react {
   190         case phase: Session.Phase =>
   191           phase match {
   192             case Session.Failed =>
   193               Swing_Thread.later {
   194                 Library.error_dialog(jEdit.getActiveView, "Prover process failure",
   195                     "Isabelle Syslog", Library.scrollable_text(PIDE.session.current_syslog()))
   196               }
   197 
   198             case Session.Ready =>
   199               PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
   200               PIDE.init_models(JEdit_Lib.jedit_buffers().toList)
   201               Swing_Thread.later { delay_load.invoke() }
   202 
   203             case Session.Shutdown =>
   204               PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
   205               Swing_Thread.later { delay_load.revoke() }
   206 
   207             case _ =>
   208           }
   209         case bad => java.lang.System.err.println("session_manager: ignoring bad message " + bad)
   210       }
   211     }
   212   }
   213 
   214 
   215   /* main plugin plumbing */
   216 
   217   override def handleMessage(message: EBMessage)
   218   {
   219     Swing_Thread.assert()
   220 
   221     if (PIDE.startup_failure.isDefined && !PIDE.startup_notified) {
   222       message match {
   223         case msg: EditorStarted =>
   224           Library.error_dialog(null, "Isabelle plugin startup failure",
   225             Library.scrollable_text(Exn.message(PIDE.startup_failure.get)),
   226             "Prover IDE inactive!")
   227           PIDE.startup_notified = true
   228         case _ =>
   229       }
   230     }
   231 
   232     if (PIDE.startup_failure.isEmpty) {
   233       message match {
   234         case msg: EditorStarted =>
   235           if (PIDE.options.bool("jedit_auto_start"))
   236             PIDE.session.start(Isabelle_Logic.session_args())
   237 
   238         case msg: BufferUpdate
   239         if msg.getWhat == BufferUpdate.LOADED || msg.getWhat == BufferUpdate.PROPERTIES_CHANGED =>
   240           if (PIDE.session.is_ready) {
   241             val buffer = msg.getBuffer
   242             if (buffer != null && !buffer.isLoading) delay_init.invoke()
   243             Swing_Thread.later { delay_load.invoke() }
   244           }
   245 
   246         case msg: EditPaneUpdate
   247         if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGING ||
   248             msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   249             msg.getWhat == EditPaneUpdate.CREATED ||
   250             msg.getWhat == EditPaneUpdate.DESTROYED) =>
   251           val edit_pane = msg.getEditPane
   252           val buffer = edit_pane.getBuffer
   253           val text_area = edit_pane.getTextArea
   254 
   255           if (buffer != null && text_area != null) {
   256             if (msg.getWhat == EditPaneUpdate.BUFFER_CHANGED ||
   257                 msg.getWhat == EditPaneUpdate.CREATED) {
   258               if (PIDE.session.is_ready)
   259                 PIDE.init_view(buffer, text_area)
   260             }
   261             else PIDE.exit_view(buffer, text_area)
   262           }
   263 
   264         case msg: PropertiesChanged =>
   265           PIDE.session.global_options.event(Session.Global_Options(PIDE.options.value))
   266 
   267         case _ =>
   268       }
   269     }
   270   }
   271 
   272   override def start()
   273   {
   274     try {
   275       PIDE.plugin = this
   276       Isabelle_System.init()
   277       Isabelle_System.install_fonts()
   278 
   279       val init_options = Options.init()
   280       Swing_Thread.now { PIDE.options.update(init_options)  }
   281 
   282       SyntaxUtilities.setStyleExtender(new Token_Markup.Style_Extender)
   283       if (ModeProvider.instance.isInstanceOf[ModeProvider])
   284         ModeProvider.instance = new Token_Markup.Mode_Provider(ModeProvider.instance)
   285 
   286       val content = Isabelle_Logic.session_content(false)
   287       val thy_load = new JEdit_Thy_Load(content.loaded_theories, content.syntax)
   288 
   289       PIDE.session = new Session(thy_load) {
   290         override def output_delay = PIDE.options.seconds("editor_output_delay")
   291         override def reparse_limit = PIDE.options.int("editor_reparse_limit")
   292       }
   293 
   294       PIDE.session.phase_changed += session_manager
   295       PIDE.startup_failure = None
   296     }
   297     catch {
   298       case exn: Throwable =>
   299         PIDE.startup_failure = Some(exn)
   300         PIDE.startup_notified = false
   301     }
   302   }
   303 
   304   override def stop()
   305   {
   306     if (PIDE.startup_failure.isEmpty)
   307       PIDE.options.value.save_prefs()
   308 
   309     PIDE.session.phase_changed -= session_manager
   310     PIDE.exit_models(JEdit_Lib.jedit_buffers().toList)
   311     PIDE.session.stop()
   312   }
   313 }