src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Wed Apr 12 22:32:55 2017 +0200 (2017-04-12 ago)
changeset 65471 05e5bffcf1d8
parent 65469 78ace4a14975
child 65472 f83081bcdd0e
permissions -rw-r--r--
clarified loaded_theories: map to qualified theory name;
proper theory_name for PIDE editors;
wenzelm@56208
     1
/*  Title:      Tools/jEdit/src/jedit_resources.scala
wenzelm@44577
     2
    Author:     Makarius
wenzelm@44577
     3
wenzelm@56208
     4
Resources for theories and auxiliary files, based on jEdit buffer
wenzelm@56208
     5
content and virtual file-systems.
wenzelm@44577
     6
*/
wenzelm@44577
     7
wenzelm@44577
     8
package isabelle.jedit
wenzelm@44577
     9
wenzelm@44577
    10
wenzelm@44577
    11
import isabelle._
wenzelm@44577
    12
wenzelm@56502
    13
import java.io.{File => JFile, ByteArrayOutputStream}
wenzelm@44577
    14
import javax.swing.text.Segment
wenzelm@44577
    15
wenzelm@56823
    16
import scala.util.parsing.input.Reader
wenzelm@56823
    17
wenzelm@56502
    18
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
wenzelm@44577
    19
import org.gjt.sp.jedit.MiscUtilities
wenzelm@55134
    20
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@54511
    21
import org.gjt.sp.jedit.bufferio.BufferIORequest
wenzelm@44577
    22
wenzelm@56208
    23
wenzelm@65441
    24
class JEdit_Resources(session_base: Sessions.Base) extends Resources(session_base)
wenzelm@44577
    25
{
wenzelm@64834
    26
  /* document node name */
wenzelm@50566
    27
wenzelm@64841
    28
  def node_name(path: String): Document.Node.Name =
wenzelm@64841
    29
  {
wenzelm@64841
    30
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@64841
    31
    val node = if (vfs.isInstanceOf[FileVFS]) MiscUtilities.resolveSymlinks(path) else path
wenzelm@65471
    32
    val (loaded, theory) = theory_name(default_qualifier, Thy_Header.theory_name(node))
wenzelm@65471
    33
    if (loaded) Document.Node.Name.loaded_theory(theory)
wenzelm@65471
    34
    else Document.Node.Name(node, if (theory == "") "" else vfs.getParentOfPath(path), theory)
wenzelm@64841
    35
  }
wenzelm@64841
    36
wenzelm@65469
    37
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@65469
    38
    node_name(JEdit_Lib.buffer_name(buffer))
wenzelm@64858
    39
wenzelm@54509
    40
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    41
  {
wenzelm@54509
    42
    val name = node_name(buffer)
wenzelm@54509
    43
    if (name.is_theory) Some(name) else None
wenzelm@50566
    44
  }
wenzelm@50566
    45
wenzelm@44615
    46
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    47
  {
wenzelm@44577
    48
    val path = source_path.expand
wenzelm@57841
    49
    if (dir == "" || path.is_absolute)
wenzelm@60988
    50
      MiscUtilities.resolveSymlinks(File.platform_path(path))
wenzelm@56502
    51
    else if (path.is_current) dir
wenzelm@44577
    52
    else {
wenzelm@44615
    53
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    54
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    55
        MiscUtilities.resolveSymlinks(
wenzelm@60988
    56
          vfs.constructPath(dir, File.platform_path(path)))
wenzelm@60988
    57
      else vfs.constructPath(dir, File.standard_path(path))
wenzelm@44577
    58
    }
wenzelm@44577
    59
  }
wenzelm@44577
    60
wenzelm@64834
    61
wenzelm@64834
    62
  /* file content */
wenzelm@64834
    63
wenzelm@64835
    64
  def read_file_content(node_name: Document.Node.Name): Option[String] =
wenzelm@64835
    65
  {
wenzelm@64835
    66
    val name = node_name.node
wenzelm@64835
    67
    try {
wenzelm@64835
    68
      val text =
wenzelm@64835
    69
        if (Url.is_wellformed(name)) Url.read(Url(name))
wenzelm@64835
    70
        else File.read(new JFile(name))
wenzelm@64835
    71
      Some(Symbol.decode(Line.normalize(text)))
wenzelm@64835
    72
    }
wenzelm@64835
    73
    catch { case ERROR(_) => None }
wenzelm@64835
    74
  }
wenzelm@64835
    75
wenzelm@64841
    76
  def get_file_content(node_name: Document.Node.Name): Option[String] =
wenzelm@64841
    77
    Document_Model.get(node_name) match {
wenzelm@64841
    78
      case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
wenzelm@64841
    79
      case Some(model: File_Model) => Some(model.content.text)
wenzelm@64841
    80
      case None => read_file_content(node_name)
wenzelm@64841
    81
    }
wenzelm@64841
    82
wenzelm@56823
    83
  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    84
  {
wenzelm@57612
    85
    GUI_Thread.now {
wenzelm@64837
    86
      Document_Model.get(name) match {
wenzelm@64841
    87
        case Some(model: Buffer_Model) =>
wenzelm@64841
    88
          JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
wenzelm@64841
    89
        case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
wenzelm@48885
    90
        case None => None
wenzelm@48885
    91
      }
wenzelm@48885
    92
    } getOrElse {
wenzelm@56823
    93
      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
wenzelm@56502
    94
      else {
wenzelm@56502
    95
        val file = new JFile(name.node)
wenzelm@56823
    96
        if (file.isFile) f(Scan.byte_reader(file))
wenzelm@56502
    97
        else error("No such file: " + quote(file.toString))
wenzelm@56502
    98
      }
wenzelm@48885
    99
    }
wenzelm@48885
   100
  }
wenzelm@48885
   101
wenzelm@54511
   102
wenzelm@56666
   103
  private class File_Content_Output(buffer: Buffer) extends
wenzelm@56666
   104
    ByteArrayOutputStream(buffer.getLength + 1)
wenzelm@56666
   105
  {
wenzelm@56666
   106
    def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@56666
   107
  }
wenzelm@56666
   108
wenzelm@56666
   109
  private class File_Content(buffer: Buffer) extends
wenzelm@56666
   110
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
wenzelm@54511
   111
  {
wenzelm@56666
   112
    def _run() { }
wenzelm@56666
   113
    def content(): Bytes =
wenzelm@56666
   114
    {
wenzelm@56666
   115
      val out = new File_Content_Output(buffer)
wenzelm@56666
   116
      write(buffer, out)
wenzelm@56666
   117
      out.content()
wenzelm@56666
   118
    }
wenzelm@54511
   119
  }
wenzelm@55134
   120
wenzelm@64835
   121
  def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
wenzelm@56666
   122
wenzelm@55134
   123
wenzelm@55134
   124
  /* theory text edits */
wenzelm@55134
   125
wenzelm@56316
   126
  override def commit(change: Session.Change)
wenzelm@56316
   127
  {
wenzelm@59319
   128
    if (change.syntax_changed.nonEmpty)
wenzelm@64836
   129
      GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
wenzelm@60917
   130
    if (change.deps_changed ||
wenzelm@61023
   131
        PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
wenzelm@65243
   132
      PIDE.plugin.deps_changed()
wenzelm@56316
   133
  }
wenzelm@44577
   134
}