src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Thu Jan 05 10:10:51 2017 +0100 (2017-01-05 ago)
changeset 64797 891a25a87ea1
parent 63022 785a59235a15
child 64813 7283f41d05ab
permissions -rw-r--r--
tuned;
     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 =
    27     new JEdit_Resources(Set.empty, Map.empty, Outer_Syntax.empty)
    28 }
    29 
    30 class JEdit_Resources(
    31     loaded_theories: Set[String],
    32     known_theories: Map[String, Document.Node.Name],
    33     base_syntax: Outer_Syntax)
    34   extends Resources(loaded_theories, known_theories, base_syntax)
    35 {
    36   /* document node names */
    37 
    38   def node_name(buffer: Buffer): Document.Node.Name =
    39   {
    40     val node = JEdit_Lib.buffer_name(buffer)
    41     val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("")
    42     val master_dir = if (theory == "") "" else buffer.getDirectory
    43     Document.Node.Name(node, master_dir, theory)
    44   }
    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 
    53   /* file-system operations */
    54 
    55   override def append(dir: String, source_path: Path): String =
    56   {
    57     val path = source_path.expand
    58     if (dir == "" || path.is_absolute)
    59       MiscUtilities.resolveSymlinks(File.platform_path(path))
    60     else if (path.is_current) dir
    61     else {
    62       val vfs = VFSManager.getVFSForPath(dir)
    63       if (vfs.isInstanceOf[FileVFS])
    64         MiscUtilities.resolveSymlinks(
    65           vfs.constructPath(dir, File.platform_path(path)))
    66       else vfs.constructPath(dir, File.standard_path(path))
    67     }
    68   }
    69 
    70   override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
    71   {
    72     GUI_Thread.now {
    73       JEdit_Lib.jedit_buffer(name) match {
    74         case Some(buffer) =>
    75           JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
    76         case None => None
    77       }
    78     } getOrElse {
    79       if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
    80       else {
    81         val file = new JFile(name.node)
    82         if (file.isFile) f(Scan.byte_reader(file))
    83         else error("No such file: " + quote(file.toString))
    84       }
    85     }
    86   }
    87 
    88 
    89   /* file content */
    90 
    91   private class File_Content_Output(buffer: Buffer) extends
    92     ByteArrayOutputStream(buffer.getLength + 1)
    93   {
    94     def content(): Bytes = Bytes(this.buf, 0, this.count)
    95   }
    96 
    97   private class File_Content(buffer: Buffer) extends
    98     BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
    99   {
   100     def _run() { }
   101     def content(): Bytes =
   102     {
   103       val out = new File_Content_Output(buffer)
   104       write(buffer, out)
   105       out.content()
   106     }
   107   }
   108 
   109   def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
   110 
   111 
   112   /* theory text edits */
   113 
   114   override def commit(change: Session.Change)
   115   {
   116     if (change.syntax_changed.nonEmpty)
   117       GUI_Thread.later {
   118         val changed = change.syntax_changed.toSet
   119         for {
   120           buffer <- JEdit_Lib.jedit_buffers()
   121           model <- PIDE.document_model(buffer)
   122           if changed(model.node_name)
   123         } model.syntax_changed()
   124       }
   125     if (change.deps_changed ||
   126         PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
   127       PIDE.deps_changed()
   128   }
   129 }