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