src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Sun Jan 08 16:47:53 2017 +0100 (2017-01-08 ago)
changeset 64835 fd1efd6dd385
parent 64834 0a7179ad430d
child 64836 3611f759f896
permissions -rw-r--r--
resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
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@64834
    36
  /* document node name */
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@44615
    52
  override def append(dir: String, source_path: Path): String =
wenzelm@44577
    53
  {
wenzelm@44577
    54
    val path = source_path.expand
wenzelm@57841
    55
    if (dir == "" || path.is_absolute)
wenzelm@60988
    56
      MiscUtilities.resolveSymlinks(File.platform_path(path))
wenzelm@56502
    57
    else if (path.is_current) dir
wenzelm@44577
    58
    else {
wenzelm@44615
    59
      val vfs = VFSManager.getVFSForPath(dir)
wenzelm@44577
    60
      if (vfs.isInstanceOf[FileVFS])
wenzelm@44577
    61
        MiscUtilities.resolveSymlinks(
wenzelm@60988
    62
          vfs.constructPath(dir, File.platform_path(path)))
wenzelm@60988
    63
      else vfs.constructPath(dir, File.standard_path(path))
wenzelm@44577
    64
    }
wenzelm@44577
    65
  }
wenzelm@44577
    66
wenzelm@64834
    67
wenzelm@64834
    68
  /* file content */
wenzelm@64834
    69
wenzelm@64835
    70
  def read_file_content(node_name: Document.Node.Name): Option[String] =
wenzelm@64835
    71
  {
wenzelm@64835
    72
    val name = node_name.node
wenzelm@64835
    73
    try {
wenzelm@64835
    74
      val text =
wenzelm@64835
    75
        if (Url.is_wellformed(name)) Url.read(Url(name))
wenzelm@64835
    76
        else File.read(new JFile(name))
wenzelm@64835
    77
      Some(Symbol.decode(Line.normalize(text)))
wenzelm@64835
    78
    }
wenzelm@64835
    79
    catch { case ERROR(_) => None }
wenzelm@64835
    80
  }
wenzelm@64835
    81
wenzelm@56823
    82
  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
wenzelm@48885
    83
  {
wenzelm@57612
    84
    GUI_Thread.now {
wenzelm@56457
    85
      JEdit_Lib.jedit_buffer(name) match {
wenzelm@48885
    86
        case Some(buffer) =>
wenzelm@56823
    87
          JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
wenzelm@48885
    88
        case None => None
wenzelm@48885
    89
      }
wenzelm@48885
    90
    } getOrElse {
wenzelm@56823
    91
      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
wenzelm@56502
    92
      else {
wenzelm@56502
    93
        val file = new JFile(name.node)
wenzelm@56823
    94
        if (file.isFile) f(Scan.byte_reader(file))
wenzelm@56502
    95
        else error("No such file: " + quote(file.toString))
wenzelm@56502
    96
      }
wenzelm@48885
    97
    }
wenzelm@48885
    98
  }
wenzelm@48885
    99
wenzelm@54511
   100
wenzelm@56666
   101
  private class File_Content_Output(buffer: Buffer) extends
wenzelm@56666
   102
    ByteArrayOutputStream(buffer.getLength + 1)
wenzelm@56666
   103
  {
wenzelm@56666
   104
    def content(): Bytes = Bytes(this.buf, 0, this.count)
wenzelm@56666
   105
  }
wenzelm@56666
   106
wenzelm@56666
   107
  private class File_Content(buffer: Buffer) extends
wenzelm@56666
   108
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
wenzelm@54511
   109
  {
wenzelm@56666
   110
    def _run() { }
wenzelm@56666
   111
    def content(): Bytes =
wenzelm@56666
   112
    {
wenzelm@56666
   113
      val out = new File_Content_Output(buffer)
wenzelm@56666
   114
      write(buffer, out)
wenzelm@56666
   115
      out.content()
wenzelm@56666
   116
    }
wenzelm@54511
   117
  }
wenzelm@55134
   118
wenzelm@64835
   119
  def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
wenzelm@56666
   120
wenzelm@55134
   121
wenzelm@55134
   122
  /* theory text edits */
wenzelm@55134
   123
wenzelm@56316
   124
  override def commit(change: Session.Change)
wenzelm@56316
   125
  {
wenzelm@59319
   126
    if (change.syntax_changed.nonEmpty)
wenzelm@59078
   127
      GUI_Thread.later {
wenzelm@59078
   128
        val changed = change.syntax_changed.toSet
wenzelm@59078
   129
        for {
wenzelm@59078
   130
          buffer <- JEdit_Lib.jedit_buffers()
wenzelm@64813
   131
          model <- Document_Model.get(buffer)
wenzelm@59078
   132
          if changed(model.node_name)
wenzelm@59078
   133
        } model.syntax_changed()
wenzelm@59078
   134
      }
wenzelm@60917
   135
    if (change.deps_changed ||
wenzelm@61023
   136
        PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
wenzelm@60916
   137
      PIDE.deps_changed()
wenzelm@56316
   138
  }
wenzelm@44577
   139
}