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