src/Tools/jEdit/src/isabelle_hyperlinks.scala
author wenzelm
Fri Jul 20 22:29:25 2012 +0200 (2012-07-20)
changeset 48409 0d2114eb412a
parent 47867 dd9cbe708e6b
permissions -rw-r--r--
more explicit java.io.{File => JFile};
     1 /*  Title:      Tools/jEdit/src/isabelle_hyperlinks.scala
     2     Author:     Fabian Immler, TU Munich
     3 
     4 Hyperlink setup for Isabelle proof documents.
     5 */
     6 
     7 package isabelle.jedit
     8 
     9 
    10 import isabelle._
    11 
    12 import java.io.{File => JFile}
    13 
    14 import gatchan.jedit.hyperlinks.{Hyperlink, HyperlinkSource, AbstractHyperlink}
    15 
    16 import org.gjt.sp.jedit.{View, jEdit, Buffer, TextUtilities}
    17 
    18 
    19 private class Internal_Hyperlink(name: String, start: Int, end: Int, line: Int, offset: Int)
    20   extends AbstractHyperlink(start, end, line, "")
    21 {
    22   override def click(view: View)
    23   {
    24     val text_area = view.getTextArea
    25     if (Isabelle.buffer_name(view.getBuffer) == name)
    26       text_area.moveCaretPosition(offset)
    27     else
    28       Isabelle.jedit_buffer(name) match {
    29         case Some(buffer) =>
    30           view.setBuffer(buffer)
    31           text_area.moveCaretPosition(offset)
    32         case None =>
    33       }
    34   }
    35 }
    36 
    37 class External_Hyperlink(start: Int, end: Int, line: Int, def_file: String, def_line: Int)
    38   extends AbstractHyperlink(start, end, line, "")
    39 {
    40   override def click(view: View) = {
    41     Isabelle_System.source_file(Path.explode(def_file)) match {
    42       case None =>
    43         Library.error_dialog(view, "File not found",
    44           Library.scrollable_text("Could not find source file " + def_file))
    45       case Some(file) =>
    46         jEdit.openFiles(view, file.getParent, Array(file.getName, "+line:" + def_line))
    47     }
    48   }
    49 }
    50 
    51 class Isabelle_Hyperlinks extends HyperlinkSource
    52 {
    53   def getHyperlink(buffer: Buffer, buffer_offset: Int): Hyperlink =
    54   {
    55     Swing_Thread.assert()
    56     Isabelle.buffer_lock(buffer) {
    57       Document_Model(buffer) match {
    58         case Some(model) =>
    59           val snapshot = model.snapshot()
    60           val markup =
    61             snapshot.select_markup[Hyperlink](
    62               Text.Range(buffer_offset, buffer_offset + 1),
    63               Some(Set(Isabelle_Markup.ENTITY)),
    64               {
    65                 // FIXME Isabelle_Rendering.hyperlink
    66                 case Text.Info(info_range,
    67                     XML.Elem(Markup(Isabelle_Markup.ENTITY, props), _))
    68                   if (props.find(
    69                     { case (Markup.KIND, Isabelle_Markup.ML_OPEN) => true
    70                       case (Markup.KIND, Isabelle_Markup.ML_STRUCT) => true
    71                       case _ => false }).isEmpty) =>
    72                   val Text.Range(begin, end) = info_range
    73                   val line = buffer.getLineOfOffset(begin)
    74                   (Position.File.unapply(props), Position.Line.unapply(props)) match {
    75                     case (Some(def_file), def_line) =>
    76                       new External_Hyperlink(begin, end, line, def_file, def_line.getOrElse(1))
    77                     case _ if !snapshot.is_outdated =>
    78                       (props, props) match {
    79                         case (Position.Id(def_id), Position.Offset(def_offset)) =>
    80                           snapshot.state.find_command(snapshot.version, def_id) match {
    81                             case Some((def_node, def_cmd)) =>
    82                               def_node.command_start(def_cmd) match {
    83                                 case Some(def_cmd_start) =>
    84                                   new Internal_Hyperlink(def_cmd.node_name.node, begin, end, line,
    85                                     def_cmd_start + def_cmd.decode(def_offset))
    86                                 case None => null
    87                               }
    88                             case None => null
    89                           }
    90                         case _ => null
    91                       }
    92                     case _ => null
    93                   }
    94               })
    95           markup match {
    96             case Text.Info(_, link) #:: _ => link
    97             case _ => null
    98           }
    99         case None => null
   100       }
   101     }
   102   }
   103 }