src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Fri, 02 May 2014 13:52:45 +0200
changeset 56823 37be55461dbe
parent 56801 8dd9df88f647
child 57612 990ffb84489b
permissions -rw-r--r--
more frugal access to theory text via Reader, reduced costs for I/O text decoding; prefer non-strict Symbol.decode, since Reader[Char] may present symbols in either way;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55134
diff changeset
     1
/*  Title:      Tools/jEdit/src/jedit_resources.scala
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     3
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55134
diff changeset
     4
Resources for theories and auxiliary files, based on jEdit buffer
06cc31dff138 clarifed module name;
wenzelm
parents: 55134
diff changeset
     5
content and virtual file-systems.
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     6
*/
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     7
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     8
package isabelle.jedit
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
     9
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    10
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    11
import isabelle._
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    12
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    13
import java.io.{File => JFile, ByteArrayOutputStream}
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    14
import javax.swing.text.Segment
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    15
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    16
import scala.util.parsing.input.Reader
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    17
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    18
import org.gjt.sp.jedit.io.{VFS, FileVFS, VFSManager}
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    19
import org.gjt.sp.jedit.MiscUtilities
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
    20
import org.gjt.sp.jedit.{jEdit, View, Buffer}
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    21
import org.gjt.sp.jedit.bufferio.BufferIORequest
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    22
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55134
diff changeset
    23
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56666
diff changeset
    24
class JEdit_Resources(
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56666
diff changeset
    25
    loaded_theories: Set[String],
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56666
diff changeset
    26
    known_theories: Map[String, Document.Node.Name],
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56666
diff changeset
    27
    base_syntax: Outer_Syntax)
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56666
diff changeset
    28
  extends Resources(loaded_theories, known_theories, base_syntax)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    29
{
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    30
  /* document node names */
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    31
54509
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    32
  def node_name(buffer: Buffer): Document.Node.Name =
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    33
  {
54515
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54511
diff changeset
    34
    val node = JEdit_Lib.buffer_name(buffer)
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54511
diff changeset
    35
    val theory = Thy_Header.thy_name(node).getOrElse("")
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54511
diff changeset
    36
    val master_dir = if (theory == "") "" else buffer.getDirectory
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54511
diff changeset
    37
    Document.Node.Name(node, master_dir, theory)
54509
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    38
  }
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    39
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    40
  def theory_node_name(buffer: Buffer): Option[Document.Node.Name] =
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    41
  {
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    42
    val name = node_name(buffer)
1f77110c94ef maintain document model for all files, with document view for theory only, and special blob for non-theory files;
wenzelm
parents: 50566
diff changeset
    43
    if (name.is_theory) Some(name) else None
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    44
  }
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    45
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    46
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    47
  /* file-system operations */
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    48
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    49
  override def append(dir: String, source_path: Path): String =
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    50
  {
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    51
    val path = source_path.expand
54517
044bee8c5e69 proper Thy_Load.append of auxiliary file names;
wenzelm
parents: 54515
diff changeset
    52
    if (dir == "" || path.is_absolute) Isabelle_System.platform_path(path)
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    53
    else if (path.is_current) dir
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    54
    else {
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    55
      val vfs = VFSManager.getVFSForPath(dir)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    56
      if (vfs.isInstanceOf[FileVFS])
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    57
        MiscUtilities.resolveSymlinks(
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    58
          vfs.constructPath(dir, Isabelle_System.platform_path(path)))
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    59
      else vfs.constructPath(dir, Isabelle_System.standard_path(path))
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    60
    }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    61
  }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    62
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    63
  override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A =
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    64
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    65
    Swing_Thread.now {
56457
eea4bbe15745 tuned signature -- prefer static type Document.Node.Name;
wenzelm
parents: 56316
diff changeset
    66
      JEdit_Lib.jedit_buffer(name) match {
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    67
        case Some(buffer) =>
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    68
          JEdit_Lib.buffer_lock(buffer) { Some(f(JEdit_Lib.buffer_reader(buffer))) }
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    69
        case None => None
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    70
      }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    71
    } getOrElse {
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    72
      if (Url.is_wellformed(name.node)) f(Scan.byte_reader(Url(name.node)))
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    73
      else {
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    74
        val file = new JFile(name.node)
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    75
        if (file.isFile) f(Scan.byte_reader(file))
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    76
        else error("No such file: " + quote(file.toString))
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    77
      }
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    78
    }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    79
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    80
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    81
  def check_file(view: View, file: String): Boolean =
44963
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    82
    try {
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    83
      if (Url.is_wellformed(file)) Url.is_readable(file)
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    84
      else (new JFile(file)).isFile
44963
4662dddc2fd8 explicit check_file wrt. jEdit VFS, to avoid slightly confusing empty buffer after IO error;
wenzelm
parents: 44953
diff changeset
    85
    }
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    86
    catch { case ERROR(_) => false }
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    87
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    88
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    89
  /* file content */
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    90
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    91
  private class File_Content_Output(buffer: Buffer) extends
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    92
    ByteArrayOutputStream(buffer.getLength + 1)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    93
  {
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    94
    def content(): Bytes = Bytes(this.buf, 0, this.count)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    95
  }
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    96
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    97
  private class File_Content(buffer: Buffer) extends
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
    98
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
    99
  {
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   100
    def _run() { }
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   101
    def content(): Bytes =
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   102
    {
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   103
      val out = new File_Content_Output(buffer)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   104
      write(buffer, out)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   105
      out.content()
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   106
    }
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   107
  }
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   108
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   109
  def file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   110
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   111
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   112
  /* theory text edits */
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   113
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   114
  override def commit(change: Session.Change)
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   115
  {
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   116
    if (change.syntax_changed) Swing_Thread.later { jEdit.propertiesChanged() }
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   117
  }
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
   118
}
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
   119