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;
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@60835
    26
  val empty: JEdit_Resources =
wenzelm@60835
    27
    new JEdit_Resources(Set.empty, Map.empty, Outer_Syntax.empty)
wenzelm@60835
    28
}
wenzelm@60835
    29
wenzelm@56801
    30
class JEdit_Resources(
wenzelm@56801
    31
    loaded_theories: Set[String],
wenzelm@56801
    32
    known_theories: Map[String, Document.Node.Name],
wenzelm@56801
    33
    base_syntax: Outer_Syntax)
wenzelm@56801
    34
  extends Resources(loaded_theories, known_theories, base_syntax)
wenzelm@44577
    35
{
wenzelm@50566
    36
  /* document node names */
wenzelm@50566
    37
wenzelm@54509
    38
  def node_name(buffer: Buffer): Document.Node.Name =
wenzelm@50566
    39
  {
wenzelm@54515
    40
    val node = JEdit_Lib.buffer_name(buffer)
wenzelm@63022
    41
    val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("")
wenzelm@54515
    42
    val master_dir = if (theory == "") "" else buffer.getDirectory
wenzelm@54515
    43
    Document.Node.Name(node, master_dir, theory)
wenzelm@54509
    44
  }
wenzelm@54509
    45
wenzelm@54509
    46
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
wenzelm@54509
    47
  {
wenzelm@54509
    48
    val name = node_name(buffer)
wenzelm@54509
    49
    if (name.is_theory) Some(name) else None
wenzelm@50566
    50
  }
wenzelm@50566
    51
wenzelm@50566
    52
wenzelm@50566
    53
  /* file-system operations */
wenzelm@50566
    54
wenzelm@44615
    55
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    56
  {
wenzelm@44577
    57
    val path = source_path.expand
wenzelm@57841
    58
    if (dir == "" || path.is_absolute)
wenzelm@60988
    59
      MiscUtilities.resolveSymlinks(File.platform_path(path))
wenzelm@56502
    60
    else if (path.is_current) dir
wenzelm@44577
    61
    else {
wenzelm@44615
    62
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    63
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    64
        MiscUtilities.resolveSymlinks(
wenzelm@60988
    65
          vfs.constructPath(dir, File.platform_path(path)))
wenzelm@60988
    66
      else vfs.constructPath(dir, File.standard_path(path))
wenzelm@44577
    67
    }
wenzelm@44577
    68
  }
wenzelm@44577
    69
wenzelm@56823
    70
  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    71
  {
wenzelm@57612
    72
    GUI_Thread.now {
wenzelm@56457
    73
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    74
        case Some(buffer) =>
wenzelm@56823
    75
          JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
wenzelm@48885
    76
        case None => None
wenzelm@48885
    77
      }
wenzelm@48885
    78
    } getOrElse {
wenzelm@56823
    79
      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
wenzelm@56502
    80
      else {
wenzelm@56502
    81
        val file = new JFile(name.node)
wenzelm@56823
    82
        if (file.isFile) f(Scan.byte_reader(file))
wenzelm@56502
    83
        else error("No such file: " + quote(file.toString))
wenzelm@56502
    84
      }
wenzelm@48885
    85
    }
wenzelm@48885
    86
  }
wenzelm@48885
    87
wenzelm@54511
    88
wenzelm@54511
    89
  /* file content */
wenzelm@54511
    90
wenzelm@56666
    91
  private class File_Content_Output(buffer: Buffer) extends
wenzelm@56666
    92
    ByteArrayOutputStream(buffer.getLength + 1)
wenzelm@56666
    93
  {
wenzelm@56666
    94
    def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@56666
    95
  }
wenzelm@56666
    96
wenzelm@56666
    97
  private class File_Content(buffer: Buffer) extends
wenzelm@56666
    98
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
wenzelm@54511
    99
  {
wenzelm@56666
   100
    def _run() { }
wenzelm@56666
   101
    def content(): Bytes =
wenzelm@56666
   102
    {
wenzelm@56666
   103
      val out = new File_Content_Output(buffer)
wenzelm@56666
   104
      write(buffer, out)
wenzelm@56666
   105
      out.content()
wenzelm@56666
   106
    }
wenzelm@54511
   107
  }
wenzelm@55134
   108
wenzelm@56666
   109
  def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
wenzelm@56666
   110
wenzelm@55134
   111
wenzelm@55134
   112
  /* theory text edits */
wenzelm@55134
   113
wenzelm@56316
   114
  override def commit(change: Session.Change)
wenzelm@56316
   115
  {
wenzelm@59319
   116
    if (change.syntax_changed.nonEmpty)
wenzelm@59078
   117
      GUI_Thread.later {
wenzelm@59078
   118
        val changed = change.syntax_changed.toSet
wenzelm@59078
   119
        for {
wenzelm@59078
   120
          buffer <- JEdit_Lib.jedit_buffers()
wenzelm@59078
   121
          model <- PIDE.document_model(buffer)
wenzelm@59078
   122
          if changed(model.node_name)
wenzelm@59078
   123
        } model.syntax_changed()
wenzelm@59078
   124
      }
wenzelm@60917
   125
    if (change.deps_changed ||
wenzelm@61023
   126
        PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
wenzelm@60916
   127
      PIDE.deps_changed()
wenzelm@56316
   128
  }
wenzelm@44577
   129
}