src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Wed Jul 23 11:19:24 2014 +0200 (2014-07-23)
changeset 57612 990ffb84489b
parent 57611 b6256ea3b7c5
child 57615 df1b3452d71c
permissions -rw-r--r--
clarified module name: facilitate alternative GUI frameworks;
     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}
    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.empty_perspective_text))
    42 
    43     val edits = models.flatMap(_.flushed_edits(doc_blobs)) ::: removed_perspective
    44     if (!edits.isEmpty) 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 
    53   /* current situation */
    54 
    55   override def current_context: View =
    56     GUI_Thread.require { jEdit.getActiveView() }
    57 
    58   override def current_node(view: View): Option[Document.Node.Name] =
    59     GUI_Thread.require { PIDE.document_model(view.getBuffer).map(_.node_name) }
    60 
    61   override def current_node_snapshot(view: View): Option[Document.Snapshot] =
    62     GUI_Thread.require { PIDE.document_model(view.getBuffer).map(_.snapshot()) }
    63 
    64   override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
    65   {
    66     GUI_Thread.require {}
    67 
    68     JEdit_Lib.jedit_buffer(name) match {
    69       case Some(buffer) =>
    70         PIDE.document_model(buffer) match {
    71           case Some(model) => model.snapshot
    72           case None => session.snapshot(name)
    73         }
    74       case None => session.snapshot(name)
    75     }
    76   }
    77 
    78   override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] =
    79   {
    80     GUI_Thread.require {}
    81 
    82     val text_area = view.getTextArea
    83     val buffer = view.getBuffer
    84 
    85     PIDE.document_view(text_area) match {
    86       case Some(doc_view) if doc_view.model.is_theory =>
    87         val node = snapshot.version.nodes(doc_view.model.node_name)
    88         val caret = snapshot.revert(text_area.getCaretPosition)
    89         if (caret < buffer.getLength) {
    90           val caret_command_iterator = node.command_iterator(caret)
    91           if (caret_command_iterator.hasNext) {
    92             val (cmd0, _) = caret_command_iterator.next
    93             node.commands.reverse.iterator(cmd0).find(cmd => !cmd.is_ignored)
    94           }
    95           else None
    96         }
    97         else node.commands.reverse.iterator.find(cmd => !cmd.is_ignored)
    98       case _ =>
    99         PIDE.document_model(buffer) match {
   100           case Some(model) if !model.is_theory =>
   101             snapshot.version.nodes.load_commands(model.node_name) match {
   102               case cmd :: _ => Some(cmd)
   103               case Nil => None
   104             }
   105           case _ => None
   106         }
   107     }
   108   }
   109 
   110 
   111   /* overlays */
   112 
   113   private var overlays = Document.Overlays.empty
   114 
   115   override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
   116     synchronized { overlays(name) }
   117 
   118   override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
   119     synchronized { overlays = overlays.insert(command, fn, args) }
   120 
   121   override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
   122     synchronized { overlays = overlays.remove(command, fn, args) }
   123 
   124 
   125   /* navigation */
   126 
   127   def push_position(view: View)
   128   {
   129     val navigator = jEdit.getPlugin("ise.plugin.nav.NavigatorPlugin")
   130     if (navigator != null) {
   131       try {
   132         val m = navigator.getClass.getDeclaredMethod("pushPosition", view.getClass)
   133         m.invoke(null, view)
   134       }
   135       catch { case _: NoSuchMethodException => }
   136     }
   137   }
   138 
   139   def goto_file(view: View, name: String, line: Int = 0, column: Int = 0)
   140   {
   141     GUI_Thread.require {}
   142 
   143     push_position(view)
   144 
   145     JEdit_Lib.jedit_buffer(name) match {
   146       case Some(buffer) =>
   147         view.goToBuffer(buffer)
   148         val text_area = view.getTextArea
   149 
   150         try {
   151           val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
   152           text_area.moveCaretPosition(line_start)
   153           if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
   154         }
   155         catch {
   156           case _: ArrayIndexOutOfBoundsException =>
   157           case _: IllegalArgumentException =>
   158         }
   159 
   160       case None if (new JFile(name)).isDirectory =>
   161         VFSBrowser.browseDirectory(view, name)
   162 
   163       case None =>
   164         val args =
   165           if (line <= 0) Array(name)
   166           else if (column <= 0) Array(name, "+line:" + line.toInt)
   167           else Array(name, "+line:" + line.toInt + "," + column.toInt)
   168         jEdit.openFiles(view, null, args)
   169     }
   170   }
   171 
   172 
   173   /* hyperlinks */
   174 
   175   def hyperlink_url(name: String): Hyperlink =
   176     new Hyperlink {
   177       val external = true
   178       def follow(view: View): Unit =
   179         Future.fork {
   180           try { Isabelle_System.open(name) }
   181           catch {
   182             case exn: Throwable =>
   183               GUI.error_dialog(view, "System error", GUI.scrollable_text(Exn.message(exn)))
   184           }
   185         }
   186       override def toString: String = "URL " + quote(name)
   187     }
   188 
   189   def hyperlink_file(name: String, line: Int = 0, column: Int = 0): Hyperlink =
   190     new Hyperlink {
   191       val external = false
   192       def follow(view: View): Unit = goto_file(view, name, line, column)
   193       override def toString: String = "file " + quote(name)
   194     }
   195 
   196   def hyperlink_source_file(source_name: String, line: Int, offset: Symbol.Offset)
   197     : Option[Hyperlink] =
   198   {
   199     val opt_name =
   200       if (Path.is_wellformed(source_name)) {
   201         if (Path.is_valid(source_name)) {
   202           val path = Path.explode(source_name)
   203           Some(Isabelle_System.platform_path(Isabelle_System.source_file(path) getOrElse path))
   204         }
   205         else None
   206       }
   207       else Some(source_name)
   208 
   209     opt_name match {
   210       case Some(name) =>
   211         JEdit_Lib.jedit_buffer(name) match {
   212           case Some(buffer) if offset > 0 =>
   213             val (line, column) =
   214               JEdit_Lib.buffer_lock(buffer) {
   215                 ((1, 1) /:
   216                   (Symbol.iterator(JEdit_Lib.buffer_text(buffer)).
   217                     zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1)))(
   218                       Symbol.advance_line_column)
   219               }
   220             Some(hyperlink_file(name, line, column))
   221           case _ => Some(hyperlink_file(name, line))
   222         }
   223       case None => None
   224     }
   225   }
   226 
   227   override def hyperlink_command(
   228     snapshot: Document.Snapshot, command: Command, offset: Symbol.Offset = 0): Option[Hyperlink] =
   229   {
   230     if (snapshot.is_outdated) None
   231     else {
   232       snapshot.state.find_command(snapshot.version, command.id) match {
   233         case None => None
   234         case Some((node, _)) =>
   235           val file_name = command.node_name.node
   236           val sources_iterator =
   237             node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   238               (if (offset == 0) Iterator.empty
   239                else Iterator.single(command.source(Text.Range(0, command.chunk.decode(offset)))))
   240           val (line, column) = ((1, 1) /: sources_iterator)(Symbol.advance_line_column)
   241           Some(hyperlink_file(file_name, line, column))
   242       }
   243     }
   244   }
   245 
   246   def hyperlink_command_id(
   247     snapshot: Document.Snapshot,
   248     id: Document_ID.Generic,
   249     offset: Symbol.Offset): Option[Hyperlink] =
   250   {
   251     snapshot.state.find_command(snapshot.version, id) match {
   252       case Some((node, command)) => hyperlink_command(snapshot, command, offset)
   253       case None => None
   254     }
   255   }
   256 }