src/Tools/jEdit/src/isabelle_hyperlinks.scala
author wenzelm
Wed Mar 14 15:37:51 2012 +0100 (2012-03-14)
changeset 46920 5f44c8bea84e
parent 46211 2616e68877c9
child 47541 4eca121e5bf5
permissions -rw-r--r--
more explicit indication of swing thread context;
     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
    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", "Could not find source file " + def_file)
    44       case Some(file) =>
    45         jEdit.openFiles(view, file.getParent, Array(file.getName, "+line:" + def_line))
    46     }
    47   }
    48 }
    49 
    50 class Isabelle_Hyperlinks extends HyperlinkSource
    51 {
    52   def getHyperlink(buffer: Buffer, buffer_offset: Int): Hyperlink =
    53   {
    54     Swing_Thread.assert()
    55     Isabelle.buffer_lock(buffer) {
    56       Document_Model(buffer) match {
    57         case Some(model) =>
    58           val snapshot = model.snapshot()
    59           val markup =
    60             snapshot.select_markup[Hyperlink](
    61               Text.Range(buffer_offset, buffer_offset + 1),
    62               Some(Set(Isabelle_Markup.ENTITY)),
    63               {
    64                 // FIXME Isabelle_Rendering.hyperlink
    65                 case Text.Info(info_range,
    66                     XML.Elem(Markup(Isabelle_Markup.ENTITY, props), _))
    67                   if (props.find(
    68                     { case (Markup.KIND, Isabelle_Markup.ML_OPEN) => true
    69                       case (Markup.KIND, Isabelle_Markup.ML_STRUCT) => true
    70                       case _ => false }).isEmpty) =>
    71                   val Text.Range(begin, end) = info_range
    72                   val line = buffer.getLineOfOffset(begin)
    73                   (Position.File.unapply(props), Position.Line.unapply(props)) match {
    74                     case (Some(def_file), Some(def_line)) =>
    75                       new External_Hyperlink(begin, end, line, def_file, def_line)
    76                     case _ if !snapshot.is_outdated =>
    77                       (props, props) match {
    78                         case (Position.Id(def_id), Position.Offset(def_offset)) =>
    79                           snapshot.state.find_command(snapshot.version, def_id) match {
    80                             case Some((def_node, def_cmd)) =>
    81                               def_node.command_start(def_cmd) match {
    82                                 case Some(def_cmd_start) =>
    83                                   new Internal_Hyperlink(def_cmd.node_name.node, begin, end, line,
    84                                     def_cmd_start + def_cmd.decode(def_offset))
    85                                 case None => null
    86                               }
    87                             case None => null
    88                           }
    89                         case _ => null
    90                       }
    91                     case _ => null
    92                   }
    93               })
    94           markup match {
    95             case Text.Info(_, link) #:: _ => link
    96             case _ => null
    97           }
    98         case None => null
    99       }
   100     }
   101   }
   102 }