src/Tools/jEdit/src/jedit/isabelle_hyperlinks.scala
author wenzelm
Tue Mar 30 00:13:27 2010 +0200 (2010-03-30)
changeset 36015 6111de7c916a
parent 34871 e596a0b71f3c
child 36760 b82a698ef6c9
permissions -rw-r--r--
adapted to Scala 2.8.0 Beta 1;
immler@34568
     1
/*
wenzelm@34588
     2
 * Hyperlink setup for Isabelle proof documents
immler@34568
     3
 *
wenzelm@34588
     4
 * @author Fabian Immler, TU Munich
immler@34568
     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@34760
    41
	def getHyperlink(buffer: Buffer, original_offset: Int): Hyperlink =
wenzelm@34760
    42
	{
wenzelm@34788
    43
    Document_Model(buffer) match {
wenzelm@34784
    44
      case Some(model) =>
wenzelm@34824
    45
        val document = model.recent_document()
wenzelm@34784
    46
        val offset = model.from_current(document, original_offset)
wenzelm@34777
    47
        document.command_at(offset) match {
wenzelm@34855
    48
          case Some((command, command_start)) =>
wenzelm@34867
    49
            document.current_state(command).get.ref_at(offset - command_start) match {
wenzelm@34777
    50
              case Some(ref) =>
wenzelm@34784
    51
                val begin = model.to_current(document, command_start + ref.start)
wenzelm@34777
    52
                val line = buffer.getLineOfOffset(begin)
wenzelm@34784
    53
                val end = model.to_current(document, command_start + ref.stop)
wenzelm@34777
    54
                ref.info match {
wenzelm@34777
    55
                  case Command.RefInfo(Some(ref_file), Some(ref_line), _, _) =>
wenzelm@34777
    56
                    new External_Hyperlink(begin, end, line, ref_file, ref_line)
wenzelm@34777
    57
                  case Command.RefInfo(_, _, Some(id), Some(offset)) =>
wenzelm@34816
    58
                    Isabelle.session.lookup_entity(id) match {
wenzelm@34816
    59
                      case Some(ref_cmd: Command) =>
wenzelm@34858
    60
                        document.command_start(ref_cmd) match {
wenzelm@34858
    61
                          case Some(ref_cmd_start) =>
wenzelm@34858
    62
                            new Internal_Hyperlink(begin, end, line,
wenzelm@34858
    63
                              model.to_current(document, ref_cmd_start + offset - 1))
wenzelm@34858
    64
                          case None => null // FIXME external ref
wenzelm@34858
    65
                        }
wenzelm@34816
    66
                      case _ => null
wenzelm@34777
    67
                    }
wenzelm@34777
    68
                  case _ => null
wenzelm@34777
    69
                }
wenzelm@34777
    70
              case None => null
wenzelm@34777
    71
            }
wenzelm@34777
    72
          case None => null
wenzelm@34777
    73
        }
wenzelm@34777
    74
      case None => null
immler@34568
    75
    }
immler@34568
    76
  }
immler@34568
    77
}