src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Wed Apr 12 19:56:47 2017 +0200 (2017-04-12 ago)
changeset 65469 78ace4a14975
parent 65452 9e9750a7932c
child 65471 05e5bffcf1d8
permissions -rw-r--r--
more explicit jEdit file operations;
less redundant JEdit_Resources.node_name();
     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     val theory = Thy_Header.theory_name(node)
    33     val master_dir = if (theory == "") "" else vfs.getParentOfPath(path)
    34     Document.Node.Name(node, master_dir, theory)
    35   }
    36 
    37   def node_name(buffer: Buffer): Document.Node.Name =
    38     node_name(JEdit_Lib.buffer_name(buffer))
    39 
    40   def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
    41   {
    42     val name = node_name(buffer)
    43     if (name.is_theory) Some(name) else None
    44   }
    45 
    46   override def append(dir: String, source_path: Path): String =
    47   {
    48     val path = source_path.expand
    49     if (dir == "" || path.is_absolute)
    50       MiscUtilities.resolveSymlinks(File.platform_path(path))
    51     else if (path.is_current) dir
    52     else {
    53       val vfs = VFSManager.getVFSForPath(dir)
    54       if (vfs.isInstanceOf[FileVFS])
    55         MiscUtilities.resolveSymlinks(
    56           vfs.constructPath(dir, File.platform_path(path)))
    57       else vfs.constructPath(dir, File.standard_path(path))
    58     }
    59   }
    60 
    61 
    62   /* file content */
    63 
    64   def read_file_content(node_name: Document.Node.Name): Option[String] =
    65   {
    66     val name = node_name.node
    67     try {
    68       val text =
    69         if (Url.is_wellformed(name)) Url.read(Url(name))
    70         else File.read(new JFile(name))
    71       Some(Symbol.decode(Line.normalize(text)))
    72     }
    73     catch { case ERROR(_) => None }
    74   }
    75 
    76   def get_file_content(node_name: Document.Node.Name): Option[String] =
    77     Document_Model.get(node_name) match {
    78       case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
    79       case Some(model: File_Model) => Some(model.content.text)
    80       case None => read_file_content(node_name)
    81     }
    82 
    83   override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    84   {
    85     GUI_Thread.now {
    86       Document_Model.get(name) match {
    87         case Some(model: Buffer_Model) =>
    88           JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
    89         case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
    90         case None => None
    91       }
    92     } getOrElse {
    93       if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
    94       else {
    95         val file = new JFile(name.node)
    96         if (file.isFile) f(Scan.byte_reader(file))
    97         else error("No such file: " + quote(file.toString))
    98       }
    99     }
   100   }
   101 
   102 
   103   private class File_Content_Output(buffer: Buffer) extends
   104     ByteArrayOutputStream(buffer.getLength + 1)
   105   {
   106     def content(): Bytes = Bytes(this.buf, 0, this.count)
   107   }
   108 
   109   private class File_Content(buffer: Buffer) extends
   110     BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
   111   {
   112     def _run() { }
   113     def content(): Bytes =
   114     {
   115       val out = new File_Content_Output(buffer)
   116       write(buffer, out)
   117       out.content()
   118     }
   119   }
   120 
   121   def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
   122 
   123 
   124   /* theory text edits */
   125 
   126   override def commit(change: Session.Change)
   127   {
   128     if (change.syntax_changed.nonEmpty)
   129       GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
   130     if (change.deps_changed ||
   131         PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
   132       PIDE.plugin.deps_changed()
   133   }
   134 }