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