src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Thu Apr 13 12:19:28 2017 +0200 (2017-04-13 ago)
changeset 65476 a72ae9eb4462
parent 65472 f83081bcdd0e
child 65501 b42743f5b595
permissions -rw-r--r--
tuned signature (again);
     1 /*  Title:      Tools/jEdit/src/jedit_resources.scala
     2     Author:     Makarius
     3 
     4 Resources for theories and auxiliary files, based on jEdit buffer
     5 content and virtual file-systems.
     6 */
     7 
     8 package isabelle.jedit
     9 
    10 
    11 import isabelle._
    12 
    13 import java.io.{File => JFile, ByteArrayOutputStream}
    14 import javax.swing.text.Segment
    15 
    16 import scala.util.parsing.input.Reader
    17 
    18 import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
    19 import org.gjt.sp.jedit.MiscUtilities
    20 import org.gjt.sp.jedit.{jEdit, View, Buffer}
    21 import org.gjt.sp.jedit.bufferio.BufferIORequest
    22 
    23 
    24 class JEdit_Resources(session_base: Sessions.Base) extends Resources(session_base)
    25 {
    26   /* document node name */
    27 
    28   def node_name(path: String): Document.Node.Name =
    29   {
    30     val vfs = VFSManager.getVFSForPath(path)
    31     val node = if (vfs.isInstanceOf[FileVFS]) MiscUtilities.resolveSymlinks(path) else path
    32     theory_name(default_qualifier, Thy_Header.theory_name(node)) match {
    33       case (true, theory) => Document.Node.Name.loaded_theory(theory)
    34       case (false, theory) =>
    35         val master_dir = if (theory == "") "" else vfs.getParentOfPath(path)
    36         Document.Node.Name(node, master_dir, theory)
    37     }
    38   }
    39 
    40   def node_name(buffer: Buffer): Document.Node.Name =
    41     node_name(JEdit_Lib.buffer_name(buffer))
    42 
    43   def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
    44   {
    45     val name = node_name(buffer)
    46     if (name.is_theory) Some(name) else None
    47   }
    48 
    49   override def append(dir: String, source_path: Path): String =
    50   {
    51     val path = source_path.expand
    52     if (dir == "" || path.is_absolute)
    53       MiscUtilities.resolveSymlinks(File.platform_path(path))
    54     else if (path.is_current) dir
    55     else {
    56       val vfs = VFSManager.getVFSForPath(dir)
    57       if (vfs.isInstanceOf[FileVFS])
    58         MiscUtilities.resolveSymlinks(
    59           vfs.constructPath(dir, File.platform_path(path)))
    60       else vfs.constructPath(dir, File.standard_path(path))
    61     }
    62   }
    63 
    64 
    65   /* file content */
    66 
    67   def read_file_content(node_name: Document.Node.Name): Option[String] =
    68   {
    69     val name = node_name.node
    70     try {
    71       val text =
    72         if (Url.is_wellformed(name)) Url.read(Url(name))
    73         else File.read(new JFile(name))
    74       Some(Symbol.decode(Line.normalize(text)))
    75     }
    76     catch { case ERROR(_) => None }
    77   }
    78 
    79   def get_file_content(node_name: Document.Node.Name): Option[String] =
    80     Document_Model.get(node_name) match {
    81       case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
    82       case Some(model: File_Model) => Some(model.content.text)
    83       case None => read_file_content(node_name)
    84     }
    85 
    86   override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    87   {
    88     GUI_Thread.now {
    89       Document_Model.get(name) match {
    90         case Some(model: Buffer_Model) =>
    91           JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
    92         case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
    93         case None => None
    94       }
    95     } getOrElse {
    96       if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
    97       else {
    98         val file = new JFile(name.node)
    99         if (file.isFile) f(Scan.byte_reader(file))
   100         else error("No such file: " + quote(file.toString))
   101       }
   102     }
   103   }
   104 
   105 
   106   private class File_Content_Output(buffer: Buffer) extends
   107     ByteArrayOutputStream(buffer.getLength + 1)
   108   {
   109     def content(): Bytes = Bytes(this.buf, 0, this.count)
   110   }
   111 
   112   private class File_Content(buffer: Buffer) extends
   113     BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
   114   {
   115     def _run() { }
   116     def content(): Bytes =
   117     {
   118       val out = new File_Content_Output(buffer)
   119       write(buffer, out)
   120       out.content()
   121     }
   122   }
   123 
   124   def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
   125 
   126 
   127   /* theory text edits */
   128 
   129   override def commit(change: Session.Change)
   130   {
   131     if (change.syntax_changed.nonEmpty)
   132       GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
   133     if (change.deps_changed ||
   134         PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
   135       PIDE.plugin.deps_changed()
   136   }
   137 }