src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Sat Aug 15 18:59:31 2015 +0200 (2015-08-15)
changeset 60933 6d03e05ef041
parent 60893 3c8b9b4b577c
child 60988 1d7a7e33fd67
permissions -rw-r--r--
more robust access to stable tip version: take all pending edits into account, don't assume model for current buffer;
     1 /*  Title:      Tools/jEdit/src/jedit_editor.scala
     2     Author:     Makarius
     3 
     4 PIDE editor operations for jEdit.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 
    13 import java.io.{File => JFile}
    14 
    15 import org.gjt.sp.jedit.{jEdit, View, Buffer}
    16 import org.gjt.sp.jedit.browser.VFSBrowser
    17 
    18 
    19 class JEdit_Editor extends Editor[View]
    20 {
    21   /* session */
    22 
    23   override def session: Session = PIDE.session
    24 
    25   // owned by GUI thread
    26   private var removed_nodes = Set.empty[Document.Node.Name]
    27 
    28   def remove_node(name: Document.Node.Name): Unit =
    29     GUI_Thread.require { removed_nodes += name }
    30 
    31   override def flush()
    32   {
    33     GUI_Thread.require {}
    34 
    35     val doc_blobs = PIDE.document_blobs()
    36     val models = PIDE.document_models()
    37 
    38     val removed = removed_nodes; removed_nodes = Set.empty
    39     val removed_perspective =
    40       (removed -- models.iterator.map(_.node_name)).toList.map(
    41         name => (name, Document.Node.no_perspective_text))
    42 
    43     val edits = models.flatMap(_.flushed_edits(doc_blobs)) ::: removed_perspective
    44     if (edits.nonEmpty) session.update(doc_blobs, edits)
    45   }
    46 
    47   private val delay_flush =
    48     GUI_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
    49 
    50   def invoke(): Unit = delay_flush.invoke()
    51 
    52   def stable_tip_version(): Option[Document.Version] =
    53     GUI_Thread.require {
    54       if (removed_nodes.isEmpty && PIDE.document_models().forall(_.is_stable))
    55         session.current_state().stable_tip_version
    56       else None
    57     }
    58 
    59 
    60   /* current situation */
    61 
    62   override def current_context: View =
    63     GUI_Thread.require { jEdit.getActiveView() }
    64 
    65   override def current_node(view: View): Option[Document.Node.Name] =
    66     GUI_Thread.require { PIDE.document_model(view.getBuffer).map(_.node_name) }
    67 
    68   override def current_node_snapshot(view: View): Option[Document.Snapshot] =
    69     GUI_Thread.require { PIDE.document_model(view.getBuffer).map(_.snapshot()) }
    70 
    71   override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
    72   {
    73     GUI_Thread.require {}
    74 
    75     JEdit_Lib.jedit_buffer(name) match {
    76       case Some(buffer) =>
    77         PIDE.document_model(buffer) match {
    78           case Some(model) => model.snapshot
    79           case None => session.snapshot(name)
    80         }
    81       case None => session.snapshot(name)
    82     }
    83   }
    84 
    85   override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] =
    86   {
    87     GUI_Thread.require {}
    88 
    89     val text_area = view.getTextArea
    90     val buffer = view.getBuffer
    91 
    92     PIDE.document_view(text_area) match {
    93       case Some(doc_view) if doc_view.model.is_theory =>
    94         val node = snapshot.version.nodes(doc_view.model.node_name)
    95         val caret = snapshot.revert(text_area.getCaretPosition)
    96         if (caret < buffer.getLength) {
    97           val caret_command_iterator = node.command_iterator(caret)
    98           if (caret_command_iterator.hasNext) {
    99             val (cmd0, _) = caret_command_iterator.next
   100             node.commands.reverse.iterator(cmd0).find(cmd => !cmd.is_ignored)
   101           }
   102           else None
   103         }
   104         else node.commands.reverse.iterator.find(cmd => !cmd.is_ignored)
   105       case _ =>
   106         PIDE.document_model(buffer) match {
   107           case Some(model) if !model.is_theory =>
   108             snapshot.version.nodes.load_commands(model.node_name) match {
   109               case cmd :: _ => Some(cmd)
   110               case Nil => None
   111             }
   112           case _ => None
   113         }
   114     }
   115   }
   116 
   117 
   118   /* overlays */
   119 
   120   private val overlays = Synchronized(Document.Overlays.empty)
   121 
   122   override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
   123     overlays.value(name)
   124 
   125   override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
   126     overlays.change(_.insert(command, fn, args))
   127 
   128   override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
   129     overlays.change(_.remove(command, fn, args))
   130 
   131 
   132   /* navigation */
   133 
   134   def push_position(view: View)
   135   {
   136     val navigator = jEdit.getPlugin("ise.plugin.nav.NavigatorPlugin")
   137     if (navigator != null) {
   138       try { Untyped.method(navigator.getClass, "pushPosition", view.getClass).invoke(null, view) }
   139       catch { case _: NoSuchMethodException => }
   140     }
   141   }
   142 
   143   def goto_buffer(focus: Boolean, view: View, buffer: Buffer, offset: Text.Offset)
   144   {
   145     GUI_Thread.require {}
   146 
   147     push_position(view)
   148 
   149     if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
   150     try { view.getTextArea.moveCaretPosition(offset) }
   151     catch {
   152       case _: ArrayIndexOutOfBoundsException =>
   153       case _: IllegalArgumentException =>
   154     }
   155   }
   156 
   157   def goto_file(focus: Boolean, view: View, name: String, line: Int = 0, column: Int = 0)
   158   {
   159     GUI_Thread.require {}
   160 
   161     push_position(view)
   162 
   163     JEdit_Lib.jedit_buffer(name) match {
   164       case Some(buffer) =>
   165         if (focus) view.goToBuffer(buffer) else view.showBuffer(buffer)
   166         val text_area = view.getTextArea
   167 
   168         try {
   169           val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
   170           text_area.moveCaretPosition(line_start)
   171           if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
   172         }
   173         catch {
   174           case _: ArrayIndexOutOfBoundsException =>
   175           case _: IllegalArgumentException =>
   176         }
   177 
   178       case None if (new JFile(name)).isDirectory =>
   179         VFSBrowser.browseDirectory(view, name)
   180 
   181       case None =>
   182         val args =
   183           if (line <= 0) Array(name)
   184           else if (column <= 0) Array(name, "+line:" + line.toInt)
   185           else Array(name, "+line:" + line.toInt + "," + column.toInt)
   186         jEdit.openFiles(view, null, args)
   187     }
   188   }
   189 
   190 
   191   /* hyperlinks */
   192 
   193   def hyperlink_url(name: String): Hyperlink =
   194     new Hyperlink {
   195       val external = true
   196       def follow(view: View): Unit =
   197         Future.fork {
   198           try { Isabelle_System.open(name) }
   199           catch {
   200             case exn: Throwable =>
   201               GUI.error_dialog(view, "System error", GUI.scrollable_text(Exn.message(exn)))
   202           }
   203         }
   204       override def toString: String = "URL " + quote(name)
   205     }
   206 
   207   def hyperlink_buffer(focus: Boolean, buffer: Buffer, offset: Text.Offset): Hyperlink =
   208     new Hyperlink {
   209       val external = false
   210       def follow(view: View): Unit = goto_buffer(focus, view, buffer, offset)
   211       override def toString: String = "buffer " + quote(JEdit_Lib.buffer_name(buffer))
   212     }
   213 
   214   def hyperlink_file(focus: Boolean, name: String, line: Int = 0, column: Int = 0): Hyperlink =
   215     new Hyperlink {
   216       val external = false
   217       def follow(view: View): Unit = goto_file(focus, view, name, line, column)
   218       override def toString: String = "file " + quote(name)
   219     }
   220 
   221   def hyperlink_source_file(focus: Boolean, source_name: String, line: Int, offset: Symbol.Offset)
   222     : Option[Hyperlink] =
   223   {
   224     val opt_name =
   225       if (Path.is_wellformed(source_name)) {
   226         if (Path.is_valid(source_name)) {
   227           val path = Path.explode(source_name)
   228           Some(Isabelle_System.platform_path(Isabelle_System.source_file(path) getOrElse path))
   229         }
   230         else None
   231       }
   232       else Some(source_name)
   233 
   234     opt_name match {
   235       case Some(name) =>
   236         JEdit_Lib.jedit_buffer(name) match {
   237           case Some(buffer) if offset > 0 =>
   238             val (line, column) =
   239               JEdit_Lib.buffer_lock(buffer) {
   240                 ((1, 1) /:
   241                   (Symbol.iterator(JEdit_Lib.buffer_text(buffer)).
   242                     zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1)))(
   243                       Symbol.advance_line_column)
   244               }
   245             Some(hyperlink_file(focus, name, line, column))
   246           case _ => Some(hyperlink_file(focus, name, line))
   247         }
   248       case None => None
   249     }
   250   }
   251 
   252   override def hyperlink_command(
   253     focus: Boolean, snapshot: Document.Snapshot, command: Command, offset: Symbol.Offset = 0)
   254       : Option[Hyperlink] =
   255   {
   256     if (snapshot.is_outdated) None
   257     else {
   258       snapshot.state.find_command(snapshot.version, command.id) match {
   259         case None => None
   260         case Some((node, _)) =>
   261           val file_name = command.node_name.node
   262           val sources_iterator =
   263             node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   264               (if (offset == 0) Iterator.empty
   265                else Iterator.single(command.source(Text.Range(0, command.chunk.decode(offset)))))
   266           val (line, column) = ((1, 1) /: sources_iterator)(Symbol.advance_line_column)
   267           Some(hyperlink_file(focus, file_name, line, column))
   268       }
   269     }
   270   }
   271 
   272   def hyperlink_command_id(
   273     focus: Boolean, snapshot: Document.Snapshot, id: Document_ID.Generic, offset: Symbol.Offset = 0)
   274       : Option[Hyperlink] =
   275   {
   276     snapshot.state.find_command(snapshot.version, id) match {
   277       case Some((node, command)) => hyperlink_command(focus, snapshot, command, offset)
   278       case None => None
   279     }
   280   }
   281 
   282   def hyperlink_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
   283       : Option[Hyperlink] =
   284     pos match {
   285       case Position.Line_File(line, name) =>
   286         val offset = Position.Offset.unapply(pos) getOrElse 0
   287         hyperlink_source_file(focus, name, line, offset)
   288       case Position.Id_Offset0(id, offset) =>
   289         hyperlink_command_id(focus, snapshot, id, offset)
   290       case _ => None
   291     }
   292 
   293   def hyperlink_def_position(focus: Boolean, snapshot: Document.Snapshot, pos: Position.T)
   294       : Option[Hyperlink] =
   295     pos match {
   296       case Position.Def_Line_File(line, name) =>
   297         val offset = Position.Def_Offset.unapply(pos) getOrElse 0
   298         hyperlink_source_file(focus, name, line, offset)
   299       case Position.Def_Id_Offset0(id, offset) =>
   300         hyperlink_command_id(focus, snapshot, id, offset)
   301       case _ => None
   302     }
   303 }