src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Mon Apr 07 21:23:02 2014 +0200 (2014-04-07)
changeset 56457 eea4bbe15745
parent 56316 b1cf8ddc2e04
child 56460 af28fdd50690
permissions -rw-r--r--
tuned signature -- prefer static type Document.Node.Name;
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@54511
    13
import java.io.{File => JFile, IOException, ByteArrayOutputStream}
wenzelm@44577
    14
import javax.swing.text.Segment
wenzelm@44577
    15
wenzelm@44963
    16
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSFile, VFSManager}
wenzelm@44577
    17
import org.gjt.sp.jedit.MiscUtilities
wenzelm@55134
    18
import org.gjt.sp.jedit.{jEdit, View, Buffer}
wenzelm@54511
    19
import org.gjt.sp.jedit.bufferio.BufferIORequest
wenzelm@44577
    20
wenzelm@56208
    21
wenzelm@56208
    22
class JEdit_Resources(loaded_theories: Set[String] = Set.empty, base_syntax: Outer_Syntax)
wenzelm@56208
    23
  extends Resources(loaded_theories, base_syntax)
wenzelm@44577
    24
{
wenzelm@50566
    25
  /* document node names */
wenzelm@50566
    26
wenzelm@54509
    27
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    28
  {
wenzelm@54515
    29
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@54515
    30
    val theory = Thy_Header.thy_name(node).getOrElse("")
wenzelm@54515
    31
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    32
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    33
  }
wenzelm@54509
    34
wenzelm@54509
    35
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    36
  {
wenzelm@54509
    37
    val name = node_name(buffer)
wenzelm@54509
    38
    if (name.is_theory) Some(name) else None
wenzelm@50566
    39
  }
wenzelm@50566
    40
wenzelm@50566
    41
wenzelm@50566
    42
  /* file-system operations */
wenzelm@50566
    43
wenzelm@44615
    44
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    45
  {
wenzelm@44577
    46
    val path = source_path.expand
wenzelm@54517
    47
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
wenzelm@44577
    48
    else {
wenzelm@44615
    49
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    50
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    51
        MiscUtilities.resolveSymlinks(
wenzelm@44615
    52
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
wenzelm@44615
    53
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
wenzelm@44577
    54
    }
wenzelm@44577
    55
  }
wenzelm@44577
    56
wenzelm@48885
    57
  override def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
wenzelm@48885
    58
  {
wenzelm@48885
    59
    Swing_Thread.now {
wenzelm@56457
    60
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    61
        case Some(buffer) =>
wenzelm@49406
    62
          JEdit_Lib.buffer_lock(buffer) {
wenzelm@48885
    63
            Some(f(buffer.getSegment(0, buffer.getLength)))
wenzelm@48885
    64
          }
wenzelm@48885
    65
        case None => None
wenzelm@48885
    66
      }
wenzelm@48885
    67
    } getOrElse {
wenzelm@48885
    68
      val file = new JFile(name.node)  // FIXME load URL via jEdit VFS (!?)
wenzelm@48885
    69
      if (!file.exists || !file.isFile) error("No such file: " + quote(file.toString))
wenzelm@48885
    70
      f(File.read(file))
wenzelm@48885
    71
    }
wenzelm@48885
    72
  }
wenzelm@48885
    73
wenzelm@44963
    74
  def check_file(view: View, path: String): Boolean =
wenzelm@44963
    75
  {
wenzelm@44963
    76
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@44963
    77
    val session = vfs.createVFSSession(path, view)
wenzelm@44963
    78
wenzelm@44963
    79
    try {
wenzelm@44963
    80
      session != null && {
wenzelm@44963
    81
        try {
wenzelm@44963
    82
          val file = vfs._getFile(session, path, view)
wenzelm@44963
    83
          file != null && file.isReadable && file.getType == VFSFile.FILE
wenzelm@44963
    84
        }
wenzelm@44963
    85
        catch { case _: IOException => false }
wenzelm@44963
    86
      }
wenzelm@44963
    87
    }
wenzelm@44963
    88
    finally {
wenzelm@44963
    89
      try { vfs._endVFSSession(session, view) }
wenzelm@44963
    90
      catch { case _: IOException => }
wenzelm@44963
    91
    }
wenzelm@44963
    92
  }
wenzelm@54511
    93
wenzelm@54511
    94
wenzelm@54511
    95
  /* file content */
wenzelm@54511
    96
wenzelm@54511
    97
  def file_content(buffer: Buffer): Bytes =
wenzelm@54511
    98
  {
wenzelm@54511
    99
    val path = buffer.getPath
wenzelm@54511
   100
    val vfs = VFSManager.getVFSForPath(path)
wenzelm@54511
   101
    val content =
wenzelm@54511
   102
      new BufferIORequest(null, buffer, null, vfs, path) {
wenzelm@54511
   103
        def _run() { }
wenzelm@54511
   104
        def apply(): Bytes =
wenzelm@54511
   105
        {
wenzelm@54511
   106
          val out =
wenzelm@54511
   107
            new ByteArrayOutputStream(buffer.getLength + 1) {
wenzelm@54511
   108
              def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@54511
   109
            }
wenzelm@54511
   110
          write(buffer, out)
wenzelm@54511
   111
          out.content()
wenzelm@54511
   112
        }
wenzelm@54511
   113
      }
wenzelm@54511
   114
    content()
wenzelm@54511
   115
  }
wenzelm@55134
   116
wenzelm@55134
   117
wenzelm@55134
   118
  /* theory text edits */
wenzelm@55134
   119
wenzelm@56316
   120
  override def commit(change: Session.Change)
wenzelm@56316
   121
  {
wenzelm@56316
   122
    if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() }
wenzelm@56316
   123
    if (change.deps_changed) PIDE.deps_changed()
wenzelm@56316
   124
  }
wenzelm@44577
   125
}
wenzelm@44577
   126