src/Tools/jEdit/src/jedit_editor.scala
author wenzelm
Mon Aug 10 14:13:49 2015 +0200 (2015-08-10)
changeset 60874 7865e03a7fc1
parent 59319 677615cba30d
child 60893 3c8b9b4b577c
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, 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 
    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 val overlays = Synchronized(Document.Overlays.empty)
   114 
   115   override def node_overlays(name: Document.Node.Name): Document.Node.Overlays =
   116     overlays.value(name)
   117 
   118   override def insert_overlay(command: Command, fn: String, args: List[String]): Unit =
   119     overlays.change(_.insert(command, fn, args))
   120 
   121   override def remove_overlay(command: Command, fn: String, args: List[String]): Unit =
   122     overlays.change(_.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 { Untyped.method(navigator.getClass, "pushPosition", view.getClass).invoke(null, view) }
   132       catch { case _: NoSuchMethodException => }
   133     }
   134   }
   135 
   136   def goto_buffer(view: View, buffer: Buffer, offset: Text.Offset)
   137   {
   138     GUI_Thread.require {}
   139 
   140     push_position(view)
   141 
   142     view.goToBuffer(buffer)
   143     try { view.getTextArea.moveCaretPosition(offset) }
   144     catch {
   145       case _: ArrayIndexOutOfBoundsException =>
   146       case _: IllegalArgumentException =>
   147     }
   148   }
   149 
   150   def goto_file(view: View, name: String, line: Int = 0, column: Int = 0)
   151   {
   152     GUI_Thread.require {}
   153 
   154     push_position(view)
   155 
   156     JEdit_Lib.jedit_buffer(name) match {
   157       case Some(buffer) =>
   158         view.goToBuffer(buffer)
   159         val text_area = view.getTextArea
   160 
   161         try {
   162           val line_start = text_area.getBuffer.getLineStartOffset(line - 1)
   163           text_area.moveCaretPosition(line_start)
   164           if (column > 0) text_area.moveCaretPosition(line_start + column - 1)
   165         }
   166         catch {
   167           case _: ArrayIndexOutOfBoundsException =>
   168           case _: IllegalArgumentException =>
   169         }
   170 
   171       case None if (new JFile(name)).isDirectory =>
   172         VFSBrowser.browseDirectory(view, name)
   173 
   174       case None =>
   175         val args =
   176           if (line <= 0) Array(name)
   177           else if (column <= 0) Array(name, "+line:" + line.toInt)
   178           else Array(name, "+line:" + line.toInt + "," + column.toInt)
   179         jEdit.openFiles(view, null, args)
   180     }
   181   }
   182 
   183 
   184   /* hyperlinks */
   185 
   186   def hyperlink_url(name: String): Hyperlink =
   187     new Hyperlink {
   188       val external = true
   189       def follow(view: View): Unit =
   190         Future.fork {
   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       override def toString: String = "URL " + quote(name)
   198     }
   199 
   200   def hyperlink_buffer(buffer: Buffer, offset: Text.Offset): Hyperlink =
   201     new Hyperlink {
   202       val external = false
   203       def follow(view: View): Unit = goto_buffer(view, buffer, offset)
   204       override def toString: String = "buffer " + quote(JEdit_Lib.buffer_name(buffer))
   205     }
   206 
   207   def hyperlink_file(name: String, line: Int = 0, column: Int = 0): Hyperlink =
   208     new Hyperlink {
   209       val external = false
   210       def follow(view: View): Unit = goto_file(view, name, line, column)
   211       override def toString: String = "file " + quote(name)
   212     }
   213 
   214   def hyperlink_source_file(source_name: String, line: Int, offset: Symbol.Offset)
   215     : Option[Hyperlink] =
   216   {
   217     val opt_name =
   218       if (Path.is_wellformed(source_name)) {
   219         if (Path.is_valid(source_name)) {
   220           val path = Path.explode(source_name)
   221           Some(Isabelle_System.platform_path(Isabelle_System.source_file(path) getOrElse path))
   222         }
   223         else None
   224       }
   225       else Some(source_name)
   226 
   227     opt_name match {
   228       case Some(name) =>
   229         JEdit_Lib.jedit_buffer(name) match {
   230           case Some(buffer) if offset > 0 =>
   231             val (line, column) =
   232               JEdit_Lib.buffer_lock(buffer) {
   233                 ((1, 1) /:
   234                   (Symbol.iterator(JEdit_Lib.buffer_text(buffer)).
   235                     zipWithIndex.takeWhile(p => p._2 < offset - 1).map(_._1)))(
   236                       Symbol.advance_line_column)
   237               }
   238             Some(hyperlink_file(name, line, column))
   239           case _ => Some(hyperlink_file(name, line))
   240         }
   241       case None => None
   242     }
   243   }
   244 
   245   override def hyperlink_command(
   246     snapshot: Document.Snapshot, command: Command, offset: Symbol.Offset = 0): Option[Hyperlink] =
   247   {
   248     if (snapshot.is_outdated) None
   249     else {
   250       snapshot.state.find_command(snapshot.version, command.id) match {
   251         case None => None
   252         case Some((node, _)) =>
   253           val file_name = command.node_name.node
   254           val sources_iterator =
   255             node.commands.iterator.takeWhile(_ != command).map(_.source) ++
   256               (if (offset == 0) Iterator.empty
   257                else Iterator.single(command.source(Text.Range(0, command.chunk.decode(offset)))))
   258           val (line, column) = ((1, 1) /: sources_iterator)(Symbol.advance_line_column)
   259           Some(hyperlink_file(file_name, line, column))
   260       }
   261     }
   262   }
   263 
   264   def hyperlink_command_id(
   265     snapshot: Document.Snapshot,
   266     id: Document_ID.Generic,
   267     offset: Symbol.Offset = 0): Option[Hyperlink] =
   268   {
   269     snapshot.state.find_command(snapshot.version, id) match {
   270       case Some((node, command)) => hyperlink_command(snapshot, command, offset)
   271       case None => None
   272     }
   273   }
   274 
   275   def hyperlink_position(snapshot: Document.Snapshot, pos: Position.T): Option[Hyperlink] =
   276     pos match {
   277       case Position.Line_File(line, name) =>
   278         val offset = Position.Offset.unapply(pos) getOrElse 0
   279         hyperlink_source_file(name, line, offset)
   280       case Position.Id_Offset0(id, offset) =>
   281         hyperlink_command_id(snapshot, id, offset)
   282       case _ => None
   283     }
   284 
   285   def hyperlink_def_position(snapshot: Document.Snapshot, pos: Position.T): Option[Hyperlink] =
   286     pos match {
   287       case Position.Def_Line_File(line, name) =>
   288         val offset = Position.Def_Offset.unapply(pos) getOrElse 0
   289         hyperlink_source_file(name, line, offset)
   290       case Position.Def_Id_Offset0(id, offset) =>
   291         hyperlink_command_id(snapshot, id, offset)
   292       case _ => None
   293     }
   294 }