src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Sun Jan 08 19:08:26 2017 +0100 (2017-01-08 ago)
changeset 64841 d53696aed874
parent 64837 4efe34df9136
child 64854 f5aa712e6250
permissions -rw-r--r--
added node_name(String): imitate jEdit buffer operations;
more uniform get_file_content for external source file references;
     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 object JEdit_Resources
    25 {
    26   val empty: JEdit_Resources =
    27     new JEdit_Resources(Set.empty, Map.empty, Outer_Syntax.empty)
    28 }
    29 
    30 class JEdit_Resources(
    31     loaded_theories: Set[String],
    32     known_theories: Map[String, Document.Node.Name],
    33     base_syntax: Outer_Syntax)
    34   extends Resources(loaded_theories, known_theories, base_syntax)
    35 {
    36   /* document node name */
    37 
    38   def node_name(buffer: Buffer): Document.Node.Name =
    39   {
    40     val node = JEdit_Lib.buffer_name(buffer)
    41     val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("")
    42     val master_dir = if (theory == "") "" else buffer.getDirectory
    43     Document.Node.Name(node, master_dir, theory)
    44   }
    45 
    46   def node_name(path: String): Document.Node.Name =
    47   {
    48     val vfs = VFSManager.getVFSForPath(path)
    49     val node = if (vfs.isInstanceOf[FileVFS]) MiscUtilities.resolveSymlinks(path) else path
    50     val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("")
    51     val master_dir = if (theory == "") "" else vfs.getParentOfPath(path)
    52     Document.Node.Name(node, master_dir, theory)
    53   }
    54 
    55   def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
    56   {
    57     val name = node_name(buffer)
    58     if (name.is_theory) Some(name) else None
    59   }
    60 
    61   override def append(dir: String, source_path: Path): String =
    62   {
    63     val path = source_path.expand
    64     if (dir == "" || path.is_absolute)
    65       MiscUtilities.resolveSymlinks(File.platform_path(path))
    66     else if (path.is_current) dir
    67     else {
    68       val vfs = VFSManager.getVFSForPath(dir)
    69       if (vfs.isInstanceOf[FileVFS])
    70         MiscUtilities.resolveSymlinks(
    71           vfs.constructPath(dir, File.platform_path(path)))
    72       else vfs.constructPath(dir, File.standard_path(path))
    73     }
    74   }
    75 
    76 
    77   /* file content */
    78 
    79   def read_file_content(node_name: Document.Node.Name): Option[String] =
    80   {
    81     val name = node_name.node
    82     try {
    83       val text =
    84         if (Url.is_wellformed(name)) Url.read(Url(name))
    85         else File.read(new JFile(name))
    86       Some(Symbol.decode(Line.normalize(text)))
    87     }
    88     catch { case ERROR(_) => None }
    89   }
    90 
    91   def get_file_content(node_name: Document.Node.Name): Option[String] =
    92     Document_Model.get(node_name) match {
    93       case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
    94       case Some(model: File_Model) => Some(model.content.text)
    95       case None => read_file_content(node_name)
    96     }
    97 
    98   override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    99   {
   100     GUI_Thread.now {
   101       Document_Model.get(name) match {
   102         case Some(model: Buffer_Model) =>
   103           JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
   104         case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
   105         case None => None
   106       }
   107     } getOrElse {
   108       if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
   109       else {
   110         val file = new JFile(name.node)
   111         if (file.isFile) f(Scan.byte_reader(file))
   112         else error("No such file: " + quote(file.toString))
   113       }
   114     }
   115   }
   116 
   117 
   118   private class File_Content_Output(buffer: Buffer) extends
   119     ByteArrayOutputStream(buffer.getLength + 1)
   120   {
   121     def content(): Bytes = Bytes(this.buf, 0, this.count)
   122   }
   123 
   124   private class File_Content(buffer: Buffer) extends
   125     BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
   126   {
   127     def _run() { }
   128     def content(): Bytes =
   129     {
   130       val out = new File_Content_Output(buffer)
   131       write(buffer, out)
   132       out.content()
   133     }
   134   }
   135 
   136   def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
   137 
   138 
   139   /* theory text edits */
   140 
   141   override def commit(change: Session.Change)
   142   {
   143     if (change.syntax_changed.nonEmpty)
   144       GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
   145     if (change.deps_changed ||
   146         PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
   147       PIDE.deps_changed()
   148   }
   149 }