2 * IsabelleHyperlinkSource.scala
4 * To change this template, choose Tools | Template Manager
5 * and open the template in the editor.
12 import gatchan.jedit.hyperlinks.Hyperlink
13 import gatchan.jedit.hyperlinks.HyperlinkSource
14 import gatchan.jedit.hyperlinks.AbstractHyperlink
16 import org.gjt.sp.jedit.View
17 import org.gjt.sp.jedit.jEdit
18 import org.gjt.sp.jedit.Buffer
19 import org.gjt.sp.jedit.TextUtilities
21 import isabelle.prover.RefInfo
23 class InternalHyperlink(start: Int, end: Int, line: Int, ref_offset: Int)
24 extends AbstractHyperlink(start, end, line, "")
26 override def click(view: View) = {
27 view.getTextArea.moveCaretPosition(ref_offset)
31 class ExternalHyperlink(start: Int, end: Int, line: Int, ref_file: String, ref_line: Int)
32 extends AbstractHyperlink(start, end, line, "")
34 val srcs = List(new File(Isabelle.system.getenv("ISABELLE_HOME"), "src"),
35 new File(Isabelle.system.getenv("ML_SOURCES")))
37 def find_file(file: File, filename: String): Option[File] =
39 if (file.getName == new File(filename).getName) Some(file)
40 else if (file.isDirectory) file.listFiles.map(find_file(_, filename)).find(_.isDefined).getOrElse(None)
44 override def click(view: View) = {
46 find_file(src, ref_file) match {
49 jEdit.openFiles(view, file.getParent, Array(file.getName, "+line:" + ref_line))
52 case None => System.err.println("Could not find file " + ref_file)
58 class IsabelleHyperlinkSource extends HyperlinkSource
60 def getHyperlink(buffer: Buffer, original_offset: Int): Hyperlink = {
61 val prover = Isabelle.prover_setup(buffer).map(_.prover)
62 val theory_view_opt = Isabelle.prover_setup(buffer).map(_.theory_view)
63 if (!prover.isDefined || !theory_view_opt.isDefined) null
64 else if (prover.get == null || theory_view_opt.get == null) null
66 val document = prover.get.document
67 val theory_view = theory_view_opt.get
68 val offset = theory_view.from_current(document, original_offset)
69 val cmd = document.find_command_at(offset)
71 val ref_o = cmd.ref_at(offset - cmd.start(document))
72 if (!ref_o.isDefined) null
75 val start = theory_view.to_current(document, ref.abs_start(document))
76 val line = buffer.getLineOfOffset(start)
77 val end = theory_view.to_current(document, ref.abs_stop(document))
79 case RefInfo(Some(ref_file), Some(ref_line), _, _) =>
80 new ExternalHyperlink(start, end, line, ref_file, ref_line)
81 case RefInfo(_, _, Some(id), Some(offset)) =>
82 prover.get.command(id) match {
84 new InternalHyperlink(start, end, line,
85 theory_view.to_current(document, ref_cmd.start(document) + offset - 1))