src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Mon Mar 03 10:59:33 2014 +0100 (2014-03-03)
changeset 55877 65c9968286d5
parent 55876 142139457653
child 55878 6d092a5166f1
permissions -rw-r--r--
tuned signature;
     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   override def flush()
    26   {
    27     Swing_Thread.require()
    28 
    29     val doc_blobs = PIDE.document_blobs()
    30     val edits = PIDE.document_models().flatMap(_.flushed_edits(doc_blobs))
    31     if (!edits.isEmpty) session.update(doc_blobs, edits)
    32   }
    33 
    34   private val delay_flush =
    35     Swing_Thread.delay_last(PIDE.options.seconds("editor_input_delay")) { flush() }
    36 
    37   def invoke(): Unit = Swing_Thread.require { delay_flush.invoke() }
    38 
    39 
    40   /* current situation */
    41 
    42   override def current_context: View =
    43     Swing_Thread.require { jEdit.getActiveView() }
    44 
    45   override def current_node(view: View): Option[Document.Node.Name] =
    46     Swing_Thread.require { PIDE.document_model(view.getBuffer).map(_.node_name) }
    47 
    48   override def current_node_snapshot(view: View): Option[Document.Snapshot] =
    49     Swing_Thread.require { PIDE.document_model(view.getBuffer).map(_.snapshot()) }
    50 
    51   override def node_snapshot(name: Document.Node.Name): Document.Snapshot =
    52   {
    53     Swing_Thread.require()
    54 
    55     JEdit_Lib.jedit_buffer(name.node) match {
    56       case Some(buffer) =>
    57         PIDE.document_model(buffer) match {
    58           case Some(model) => model.snapshot
    59           case None => session.snapshot(name)
    60         }
    61       case None => session.snapshot(name)
    62     }
    63   }
    64 
    65   override def current_command(view: View, snapshot: Document.Snapshot): Option[Command] =
    66   {
    67     Swing_Thread.require()
    68 
    69     val text_area = view.getTextArea
    70     val buffer = view.getBuffer
    71 
    72     PIDE.document_view(text_area) match {
    73       case Some(doc_view) if doc_view.model.is_theory =>
    74         val node = snapshot.version.nodes(doc_view.model.node_name)
    75         val caret = snapshot.revert(text_area.getCaretPosition)
    76         if (caret < buffer.getLength) {
    77           val caret_commands = node.command_range(caret)
    78           if (caret_commands.hasNext) {
    79             val (cmd0, _) = caret_commands.next
    80             node.commands.reverse.iterator(cmd0).find(cmd => !cmd.is_ignored)
    81           }
    82           else None
    83         }
    84         else node.commands.reverse.iterator.find(cmd => !cmd.is_ignored)
    85       case _ =>
    86         PIDE.document_model(buffer) match {
    87           case Some(model) if !model.is_theory =>
    88             snapshot.version.nodes.thy_load_commands(model.node_name) match {
    89               case cmd :: _ => Some(cmd)
    90               case Nil => None
    91             }
    92           case _ => None
    93         }
    94     }
    95   }
    96 
    97 
    98   /* overlays */
    99 
   100   private var overlays = Document.Overlays.empty
   101 
   102   override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
   103     synchronized { overlays(name) }
   104 
   105   override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
   106     synchronized { overlays = overlays.insert(command, fn, args) }
   107 
   108   override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
   109     synchronized { overlays = overlays.remove(command, fn, args) }
   110 
   111 
   112   /* navigation */
   113 
   114   def goto_file(view: View, name: String, line: Int = 0, column: Int = 0)
   115   {
   116     Swing_Thread.require()
   117 
   118     JEdit_Lib.jedit_buffer(name) match {
   119       case Some(buffer) =>
   120         view.goToBuffer(buffer)
   121         val text_area = view.getTextArea
   122 
   123         try {
   124           val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
   125           text_area.moveCaretPosition(line_start)
   126           if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
   127         }
   128         catch {
   129           case _: ArrayIndexOutOfBoundsException =>
   130           case _: IllegalArgumentException =>
   131         }
   132 
   133       case None if (new JFile(name)).isDirectory =>
   134         VFSBrowser.browseDirectory(view, name)
   135 
   136       case None =>
   137         val args =
   138           if (line <= 0) Array(name)
   139           else if (column <= 0) Array(name, "+line:" + line.toInt)
   140           else Array(name, "+line:" + line.toInt + "," + column.toInt)
   141         jEdit.openFiles(view, null, args)
   142     }
   143   }
   144 
   145 
   146   /* hyperlinks */
   147 
   148   override def hyperlink_command(snapshot: Document.Snapshot, command: Command, raw_offset: Int = 0)
   149     : Option[Hyperlink] =
   150   {
   151     if (snapshot.is_outdated) None
   152     else {
   153       snapshot.state.find_command(snapshot.version, command.id) match {
   154         case None => None
   155         case Some((node, _)) =>
   156           val file_name = command.node_name.node
   157           val sources =
   158             node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   159               (if (raw_offset == 0) Iterator.empty
   160                else Iterator.single(command.source(Text.Range(0, command.decode(raw_offset)))))
   161           val (line, column) = ((1, 1) /: sources)(Symbol.advance_line_column)
   162           Some(new Hyperlink { def follow(view: View) { goto_file(view, file_name, line, column) } })
   163       }
   164     }
   165   }
   166 
   167   def hyperlink_command_id(
   168     snapshot: Document.Snapshot,
   169     id: Document_ID.Generic,
   170     raw_offset: Text.Offset): Option[Hyperlink] =
   171   {
   172     snapshot.state.find_command(snapshot.version, id) match {
   173       case Some((node, command)) => hyperlink_command(snapshot, command, raw_offset)
   174       case None => None
   175     }
   176   }
   177 
   178   def hyperlink_file(name: String, line: Int = 0, column: Int = 0): Hyperlink =
   179     new Hyperlink { def follow(view: View) = goto_file(view, name, line, column) }
   180 
   181   def hyperlink_source_file(name: String, line: Int): Option[Hyperlink] =
   182     if (Path.is_ok(name))
   183       Isabelle_System.source_file(Path.explode(name)).map(path =>
   184         hyperlink_file(Isabelle_System.platform_path(path), line))
   185     else None
   186 
   187   def hyperlink_url(name: String): Hyperlink =
   188     new Hyperlink {
   189       def follow(view: View) =
   190         default_thread_pool.submit(() =>
   191           try { Isabelle_System.open(name) }
   192           catch {
   193             case exn: Throwable =>
   194               GUI.error_dialog(view, "System error", GUI.scrollable_text(Exn.message(exn)))
   195           })
   196     }
   197 }