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