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