src/Tools/jEdit/src/jedit/isabelle_hyperlinks.scala
author wenzelm
Sun Aug 29 15:09:11 2010 +0200 (2010-08-29)
changeset 38845 a9e37daf5bd0
parent 38843 d95522496593
child 38853 f593754b0772
permissions -rw-r--r--
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
Markup_Tree.select: plain Iterator;
misc tuning and simplification;
wenzelm@36760
     1
/*  Title:      Tools/jEdit/src/jedit/isabelle_hyperlinks.scala
wenzelm@36760
     2
    Author:     Fabian Immler, TU Munich
wenzelm@36760
     3
wenzelm@36760
     4
Hyperlink setup for Isabelle proof documents.
wenzelm@36760
     5
*/
immler@34568
     6
immler@34568
     7
package isabelle.jedit
immler@34569
     8
wenzelm@34760
     9
wenzelm@36015
    10
import isabelle._
wenzelm@36015
    11
immler@34569
    12
import java.io.File
immler@34569
    13
wenzelm@34791
    14
import gatchan.jedit.hyperlinks.{Hyperlink, HyperlinkSource, AbstractHyperlink}
immler@34568
    15
wenzelm@34791
    16
import org.gjt.sp.jedit.{View, jEdit, Buffer, TextUtilities}
immler@34568
    17
wenzelm@34588
    18
wenzelm@34760
    19
private class Internal_Hyperlink(start: Int, end: Int, line: Int, ref_offset: Int)
immler@34569
    20
  extends AbstractHyperlink(start, end, line, "")
immler@34568
    21
{
wenzelm@34582
    22
  override def click(view: View) {
immler@34568
    23
    view.getTextArea.moveCaretPosition(ref_offset)
immler@34568
    24
  }
immler@34568
    25
}
immler@34568
    26
wenzelm@34760
    27
class External_Hyperlink(start: Int, end: Int, line: Int, ref_file: String, ref_line: Int)
immler@34569
    28
  extends AbstractHyperlink(start, end, line, "")
immler@34569
    29
{
wenzelm@34602
    30
  override def click(view: View) = {
wenzelm@34602
    31
    Isabelle.system.source_file(ref_file) match {
wenzelm@34760
    32
      case None => System.err.println("Could not find source file " + ref_file)  // FIXME ??
wenzelm@34602
    33
      case Some(file) =>
immler@34573
    34
        jEdit.openFiles(view, file.getParent, Array(file.getName, "+line:" + ref_line))
immler@34573
    35
    }
wenzelm@34602
    36
  }
immler@34569
    37
}
immler@34569
    38
wenzelm@34760
    39
class Isabelle_Hyperlinks extends HyperlinkSource
immler@34568
    40
{
wenzelm@38575
    41
  def getHyperlink(buffer: Buffer, buffer_offset: Int): Hyperlink =
wenzelm@37175
    42
  {
wenzelm@38223
    43
    Swing_Thread.assert()
wenzelm@38843
    44
    Isabelle.buffer_lock(buffer) {
wenzelm@38843
    45
      Document_Model(buffer) match {
wenzelm@38843
    46
        case Some(model) =>
wenzelm@38843
    47
          val snapshot = model.snapshot()
wenzelm@38845
    48
          val markup =
wenzelm@38845
    49
            snapshot.select_markup(Text.Range(buffer_offset, buffer_offset + 1)) {
wenzelm@38843
    50
              // FIXME Isar_Document.Hyperlink extractor
wenzelm@38845
    51
              case Text.Info(info_range, XML.Elem(Markup(Markup.ML_REF, _),
wenzelm@38845
    52
                  List(XML.Elem(Markup(Markup.ML_DEF, props), _)))) =>
wenzelm@38845
    53
                val Text.Range(begin, end) = info_range
wenzelm@38845
    54
                val line = buffer.getLineOfOffset(begin)
wenzelm@38845
    55
                (Position.File.unapply(props), Position.Line.unapply(props)) match {
wenzelm@38845
    56
                  case (Some(ref_file), Some(ref_line)) =>
wenzelm@38845
    57
                    new External_Hyperlink(begin, end, line, ref_file, ref_line)
wenzelm@38845
    58
                  case _ =>
wenzelm@38845
    59
                    (props, props) match {
wenzelm@38845
    60
                      case (Position.Id(ref_id), Position.Offset(ref_offset)) =>
wenzelm@38845
    61
                        snapshot.lookup_command(ref_id) match {
wenzelm@38845
    62
                          case Some(ref_cmd) =>
wenzelm@38845
    63
                            snapshot.node.command_start(ref_cmd) match {
wenzelm@38845
    64
                              case Some(ref_cmd_start) =>
wenzelm@38845
    65
                                new Internal_Hyperlink(begin, end, line,
wenzelm@38845
    66
                                  snapshot.convert(ref_cmd_start + ref_cmd.decode(ref_offset)))
wenzelm@38845
    67
                              case None => null
wenzelm@38845
    68
                            }
wenzelm@38845
    69
                          case None => null
wenzelm@38845
    70
                        }
wenzelm@38845
    71
                      case _ => null
wenzelm@38845
    72
                    }
wenzelm@38845
    73
                }
wenzelm@38845
    74
            } { null }
wenzelm@38845
    75
          if (markup.hasNext) markup.next.info else null
wenzelm@38575
    76
wenzelm@38843
    77
        case None => null
wenzelm@38843
    78
      }
immler@34568
    79
    }
immler@34568
    80
  }
immler@34568
    81
}