src/Tools/jEdit/src/main_plugin.scala
changeset 75393 87ebf5a50283
parent 75145 e721880779be
child 75751 204b97d05abe
equal deleted inserted replaced
75388:b3ca4a6ed74b 75393:87ebf5a50283
    20   ViewUpdate}
    20   ViewUpdate}
    21 import org.gjt.sp.util.SyntaxUtilities
    21 import org.gjt.sp.util.SyntaxUtilities
    22 import org.gjt.sp.util.Log
    22 import org.gjt.sp.util.Log
    23 
    23 
    24 
    24 
    25 object PIDE
    25 object PIDE {
    26 {
       
    27   /* semantic document content */
    26   /* semantic document content */
    28 
    27 
    29   def maybe_snapshot(view: View = null): Option[Document.Snapshot] = GUI_Thread.now
    28   def maybe_snapshot(view: View = null): Option[Document.Snapshot] = GUI_Thread.now {
    30   {
       
    31     val buffer = JEdit_Lib.jedit_view(view).getBuffer
    29     val buffer = JEdit_Lib.jedit_view(view).getBuffer
    32     Document_Model.get(buffer).map(_.snapshot())
    30     Document_Model.get(buffer).map(_.snapshot())
    33   }
    31   }
    34 
    32 
    35   def maybe_rendering(view: View = null): Option[JEdit_Rendering] = GUI_Thread.now
    33   def maybe_rendering(view: View = null): Option[JEdit_Rendering] = GUI_Thread.now {
    36   {
       
    37     val text_area = JEdit_Lib.jedit_view(view).getTextArea
    34     val text_area = JEdit_Lib.jedit_view(view).getTextArea
    38     Document_View.get(text_area).map(_.get_rendering())
    35     Document_View.get(text_area).map(_.get_rendering())
    39   }
    36   }
    40 
    37 
    41   def snapshot(view: View = null): Document.Snapshot =
    38   def snapshot(view: View = null): Document.Snapshot =
    58   def session: Session = plugin.session
    55   def session: Session = plugin.session
    59 
    56 
    60   object editor extends JEdit_Editor
    57   object editor extends JEdit_Editor
    61 }
    58 }
    62 
    59 
    63 class Main_Plugin extends EBPlugin
    60 class Main_Plugin extends EBPlugin {
    64 {
       
    65   /* options */
    61   /* options */
    66 
    62 
    67   private var _options: JEdit_Options = null
    63   private var _options: JEdit_Options = null
    68   private def init_options(): Unit =
    64   private def init_options(): Unit =
    69     _options = new JEdit_Options(Options.init())
    65     _options = new JEdit_Options(Options.init())
    90   val spell_checker = new Spell_Checker_Variable
    86   val spell_checker = new Spell_Checker_Variable
    91 
    87 
    92 
    88 
    93   /* global changes */
    89   /* global changes */
    94 
    90 
    95   def options_changed(): Unit =
    91   def options_changed(): Unit = {
    96   {
       
    97     session.global_options.post(Session.Global_Options(options.value))
    92     session.global_options.post(Session.Global_Options(options.value))
    98     delay_load.invoke()
    93     delay_load.invoke()
    99   }
    94   }
   100 
    95 
   101   def deps_changed(): Unit =
    96   def deps_changed(): Unit = {
   102   {
       
   103     delay_load.invoke()
    97     delay_load.invoke()
   104   }
    98   }
   105 
    99 
   106 
   100 
   107   /* theory files */
   101   /* theory files */
   108 
   102 
   109   lazy val delay_init: Delay =
   103   lazy val delay_init: Delay =
   110     Delay.last(options.seconds("editor_load_delay"), gui = true)
   104     Delay.last(options.seconds("editor_load_delay"), gui = true) {
   111     {
       
   112       init_models()
   105       init_models()
   113     }
   106     }
   114 
   107 
   115   private val delay_load_active = Synchronized(false)
   108   private val delay_load_active = Synchronized(false)
   116   private def delay_load_activated(): Boolean =
   109   private def delay_load_activated(): Boolean =
   117     delay_load_active.guarded_access(a => Some((!a, true)))
   110     delay_load_active.guarded_access(a => Some((!a, true)))
   118   private def delay_load_action(): Unit =
   111   private def delay_load_action(): Unit = {
   119   {
       
   120     if (Isabelle.continuous_checking && delay_load_activated() &&
   112     if (Isabelle.continuous_checking && delay_load_activated() &&
   121         PerspectiveManager.isPerspectiveEnabled)
   113         PerspectiveManager.isPerspectiveEnabled) {
   122     {
       
   123       if (JEdit_Lib.jedit_buffers().exists(_.isLoading)) delay_load.invoke()
   114       if (JEdit_Lib.jedit_buffers().exists(_.isLoading)) delay_load.invoke()
   124       else {
   115       else {
   125         val required_files =
   116         val required_files = {
   126         {
       
   127           val models = Document_Model.get_models()
   117           val models = Document_Model.get_models()
   128 
   118 
   129           val thys =
   119           val thys =
   130             (for ((node_name, model) <- models.iterator if model.is_theory)
   120             (for ((node_name, model) <- models.iterator if model.is_theory)
   131               yield (node_name, Position.none)).toList
   121               yield (node_name, Position.none)).toList
   187     File_Watcher(file_watcher_action, options.seconds("editor_load_delay"))
   177     File_Watcher(file_watcher_action, options.seconds("editor_load_delay"))
   188 
   178 
   189 
   179 
   190   /* session phase */
   180   /* session phase */
   191 
   181 
   192   val session_phase_changed: Session.Consumer[Session.Phase] = Session.Consumer("Isabelle/jEdit")
   182   val session_phase_changed: Session.Consumer[Session.Phase] = Session.Consumer("Isabelle/jEdit") {
   193   {
       
   194     case Session.Terminated(result) if !result.ok =>
   183     case Session.Terminated(result) if !result.ok =>
   195       GUI_Thread.later {
   184       GUI_Thread.later {
   196         GUI.error_dialog(jEdit.getActiveView, "Prover process terminated with error",
   185         GUI.error_dialog(jEdit.getActiveView, "Prover process terminated with error",
   197           "Isabelle Syslog", GUI.scrollable_text(session.syslog_content()))
   186           "Isabelle Syslog", GUI.scrollable_text(session.syslog_content()))
   198       }
   187       }
   227   }
   216   }
   228 
   217 
   229 
   218 
   230   /* document model and view */
   219   /* document model and view */
   231 
   220 
   232   def exit_models(buffers: List[Buffer]): Unit =
   221   def exit_models(buffers: List[Buffer]): Unit = {
   233   {
       
   234     GUI_Thread.now {
   222     GUI_Thread.now {
   235       buffers.foreach(buffer =>
   223       buffers.foreach(buffer =>
   236         JEdit_Lib.buffer_lock(buffer) {
   224         JEdit_Lib.buffer_lock(buffer) {
   237           JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
   225           JEdit_Lib.jedit_text_areas(buffer).foreach(Document_View.exit)
   238           Document_Model.exit(buffer)
   226           Document_Model.exit(buffer)
   239         })
   227         })
   240       }
   228       }
   241   }
   229   }
   242 
   230 
   243   def init_models(): Unit =
   231   def init_models(): Unit = {
   244   {
       
   245     GUI_Thread.now {
   232     GUI_Thread.now {
   246       PIDE.editor.flush()
   233       PIDE.editor.flush()
   247 
   234 
   248       for {
   235       for {
   249         buffer <- JEdit_Lib.jedit_buffers()
   236         buffer <- JEdit_Lib.jedit_buffers()
   287   /* main plugin plumbing */
   274   /* main plugin plumbing */
   288 
   275 
   289   @volatile private var startup_failure: Option[Throwable] = None
   276   @volatile private var startup_failure: Option[Throwable] = None
   290   @volatile private var startup_notified = false
   277   @volatile private var startup_notified = false
   291 
   278 
   292   private def init_editor(view: View): Unit =
   279   private def init_editor(view: View): Unit = {
   293   {
       
   294     Keymap_Merge.check_dialog(view)
   280     Keymap_Merge.check_dialog(view)
   295     Session_Build.check_dialog(view)
   281     Session_Build.check_dialog(view)
   296   }
   282   }
   297 
   283 
   298   private def init_title(view: View): Unit =
   284   private def init_title(view: View): Unit = {
   299   {
       
   300     val title =
   285     val title =
   301       proper_string(Isabelle_System.getenv("ISABELLE_IDENTIFIER")).getOrElse("Isabelle") +
   286       proper_string(Isabelle_System.getenv("ISABELLE_IDENTIFIER")).getOrElse("Isabelle") +
   302         "/" + PIDE.resources.session_name
   287         "/" + PIDE.resources.session_name
   303     val marker = "\u200B"
   288     val marker = "\u200B"
   304 
   289 
   306     if (old_title == null || old_title.startsWith(marker)) {
   291     if (old_title == null || old_title.startsWith(marker)) {
   307       view.setUserTitle(marker + title)
   292       view.setUserTitle(marker + title)
   308     }
   293     }
   309   }
   294   }
   310 
   295 
   311   override def handleMessage(message: EBMessage): Unit =
   296   override def handleMessage(message: EBMessage): Unit = {
   312   {
       
   313     GUI_Thread.assert {}
   297     GUI_Thread.assert {}
   314 
   298 
   315     if (startup_failure.isDefined && !startup_notified) {
   299     if (startup_failure.isDefined && !startup_notified) {
   316       message match {
   300       message match {
   317         case msg: EditorStarted =>
   301         case msg: EditorStarted =>
   407   /* mode provider */
   391   /* mode provider */
   408 
   392 
   409   private var orig_mode_provider: ModeProvider = null
   393   private var orig_mode_provider: ModeProvider = null
   410   private var pide_mode_provider: ModeProvider = null
   394   private var pide_mode_provider: ModeProvider = null
   411 
   395 
   412   def init_mode_provider(): Unit =
   396   def init_mode_provider(): Unit = {
   413   {
       
   414     orig_mode_provider = ModeProvider.instance
   397     orig_mode_provider = ModeProvider.instance
   415     if (orig_mode_provider.isInstanceOf[ModeProvider]) {
   398     if (orig_mode_provider.isInstanceOf[ModeProvider]) {
   416       pide_mode_provider = new Token_Markup.Mode_Provider(orig_mode_provider)
   399       pide_mode_provider = new Token_Markup.Mode_Provider(orig_mode_provider)
   417       ModeProvider.instance = pide_mode_provider
   400       ModeProvider.instance = pide_mode_provider
   418     }
   401     }
   419   }
   402   }
   420 
   403 
   421   def exit_mode_provider(): Unit =
   404   def exit_mode_provider(): Unit = {
   422   {
       
   423     if (ModeProvider.instance == pide_mode_provider)
   405     if (ModeProvider.instance == pide_mode_provider)
   424       ModeProvider.instance = orig_mode_provider
   406       ModeProvider.instance = orig_mode_provider
   425   }
   407   }
   426 
   408 
   427 
   409 
   435 
   417 
   436   /* start and stop */
   418   /* start and stop */
   437 
   419 
   438   private val shutting_down = Synchronized(false)
   420   private val shutting_down = Synchronized(false)
   439 
   421 
   440   override def start(): Unit =
   422   override def start(): Unit = {
   441   {
       
   442     /* strict initialization */
   423     /* strict initialization */
   443 
   424 
   444     init_options()
   425     init_options()
   445     init_resources()
   426     init_resources()
   446     init_session()
   427     init_session()
   474 
   455 
   475     val view = jEdit.getActiveView()
   456     val view = jEdit.getActiveView()
   476     if (view != null) init_editor(view)
   457     if (view != null) init_editor(view)
   477   }
   458   }
   478 
   459 
   479   override def stop(): Unit =
   460   override def stop(): Unit = {
   480   {
       
   481     http_server.stop()
   461     http_server.stop()
   482 
   462 
   483     Syntax_Style.set_extender(Syntax_Style.Base_Extender)
   463     Syntax_Style.set_extender(Syntax_Style.Base_Extender)
   484 
   464 
   485     exit_mode_provider()
   465     exit_mode_provider()