src/Tools/jEdit/src/jedit_resources.scala
author wenzelm
Thu, 31 Aug 2017 16:35:09 +0200
changeset 66572 1e5ae735e026
parent 66195 bb886f13623a
child 66963 1c3d0c12bb51
permissions -rw-r--r--
tolerate errors in session structure, although this may lead to confusion about theory imports later on;
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
66572
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    24
object JEdit_Resources
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    25
{
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    26
  def apply(options: Options): JEdit_Resources =
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    27
  {
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    28
    val (errs, base) = JEdit_Sessions.session_base(options)
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    29
    new JEdit_Resources(errs, base)
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    30
  }
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    31
}
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    32
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    33
class JEdit_Resources private(val session_errors: List[String], session_base: Sessions.Base)
1e5ae735e026 tolerate errors in session structure, although this may lead to confusion about theory imports later on;
wenzelm
parents: 66195
diff changeset
    34
  extends Resources(session_base)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    35
{
64834
wenzelm
parents: 64813
diff changeset
    36
  /* document node name */
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    37
65501
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    38
  def known_file(path: String): Option[Document.Node.Name] =
66195
bb886f13623a proper bootstrap_name (amending b42743f5b595);
wenzelm
parents: 65532
diff changeset
    39
    JEdit_Lib.check_file(path).flatMap(session_base.known.get_file(_, bootstrap = true))
65501
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    40
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    41
  def node_name(path: String): Document.Node.Name =
65501
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    42
    known_file(path) getOrElse {
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    43
      val vfs = VFSManager.getVFSForPath(path)
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    44
      val node = if (vfs.isInstanceOf[FileVFS]) MiscUtilities.resolveSymlinks(path) else path
65532
febfd9f78bd4 eliminated default_qualifier: just a constant;
wenzelm
parents: 65524
diff changeset
    45
      theory_name(Sessions.DRAFT, Thy_Header.theory_name(node)) match {
65501
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    46
        case (true, theory) => Document.Node.Name.loaded_theory(theory)
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    47
        case (false, theory) =>
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    48
          val master_dir = if (theory == "") "" else vfs.getParentOfPath(path)
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    49
          Document.Node.Name(node, master_dir, theory)
b42743f5b595 prefer formal name from session context, for proper qualified theory name;
wenzelm
parents: 65476
diff changeset
    50
      }
65472
wenzelm
parents: 65471
diff changeset
    51
    }
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    52
65469
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 65452
diff changeset
    53
  def node_name(buffer: Buffer): Document.Node.Name =
78ace4a14975 more explicit jEdit file operations;
wenzelm
parents: 65452
diff changeset
    54
    node_name(JEdit_Lib.buffer_name(buffer))
64858
e31cf6eaecb8 update File_Model based on file-system events;
wenzelm
parents: 64856
diff changeset
    55
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
    56
  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
    57
  {
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
    58
    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
    59
    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
    60
  }
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 49406
diff changeset
    61
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    62
  override def append(dir: String, source_path: Path): String =
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    63
  {
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    64
    val path = source_path.expand
57841
e212e2001b2a always resolve symlinks for local files, e.g. relevant for ML_file to load proper source via editor instead of stored file via prover;
wenzelm
parents: 57622
diff changeset
    65
    if (dir == "" || path.is_absolute)
60988
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 60917
diff changeset
    66
      MiscUtilities.resolveSymlinks(File.platform_path(path))
56502
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
    67
    else if (path.is_current) dir
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    68
    else {
44615
a4ff8a787202 more abstract Document.Node.Name;
wenzelm
parents: 44580
diff changeset
    69
      val vfs = VFSManager.getVFSForPath(dir)
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    70
      if (vfs.isInstanceOf[FileVFS])
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    71
        MiscUtilities.resolveSymlinks(
60988
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 60917
diff changeset
    72
          vfs.constructPath(dir, File.platform_path(path)))
1d7a7e33fd67 tuned signature, according to ML version;
wenzelm
parents: 60917
diff changeset
    73
      else vfs.constructPath(dir, File.standard_path(path))
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    74
    }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    75
  }
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
    76
64834
wenzelm
parents: 64813
diff changeset
    77
wenzelm
parents: 64813
diff changeset
    78
  /* file content */
wenzelm
parents: 64813
diff changeset
    79
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    80
  def read_file_content(node_name: Document.Node.Name): Option[String] =
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    81
  {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    82
    val name = node_name.node
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    83
    try {
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    84
      val text =
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    85
        if (Url.is_wellformed(name)) Url.read(Url(name))
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    86
        else File.read(new JFile(name))
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    87
      Some(Symbol.decode(Line.normalize(text)))
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    88
    }
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    89
    catch { case ERROR(_) => None }
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    90
  }
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
    91
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    92
  def get_file_content(node_name: Document.Node.Name): Option[String] =
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    93
    Document_Model.get(node_name) match {
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    94
      case Some(model: Buffer_Model) => Some(JEdit_Lib.buffer_text(model.buffer))
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    95
      case Some(model: File_Model) => Some(model.content.text)
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    96
      case None => read_file_content(node_name)
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    97
    }
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
    98
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    99
  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
   100
  {
57612
990ffb84489b clarified module name: facilitate alternative GUI frameworks;
wenzelm
parents: 56823
diff changeset
   101
    GUI_Thread.now {
64837
4efe34df9136 refer to internal File_Model instead of external file;
wenzelm
parents: 64836
diff changeset
   102
      Document_Model.get(name) match {
64841
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
   103
        case Some(model: Buffer_Model) =>
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
   104
          JEdit_Lib.buffer_lock(model.buffer) { Some(f(JEdit_Lib.buffer_reader(model.buffer))) }
d53696aed874 added node_name(String): imitate jEdit buffer operations;
wenzelm
parents: 64837
diff changeset
   105
        case Some(model: File_Model) => Some(f(Scan.char_reader(model.content.text)))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   106
        case None => None
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   107
      }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   108
    } getOrElse {
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
   109
      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
   110
      else {
db2836f65d42 improved support for external URLs, based on standard Java network operations;
wenzelm
parents: 56460
diff changeset
   111
        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
   112
        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
   113
        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
   114
      }
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   115
    }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   116
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   117
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   118
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   119
  private class File_Content_Output(buffer: Buffer) extends
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   120
    ByteArrayOutputStream(buffer.getLength + 1)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   121
  {
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   122
    def content(): Bytes = Bytes(this.buf, 0, this.count)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   123
  }
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   124
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   125
  private class File_Content(buffer: Buffer) extends
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   126
    BufferIORequest(null, buffer, null, VFSManager.getVFSForPath(buffer.getPath), buffer.getPath)
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   127
  {
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   128
    def _run() { }
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   129
    def content(): Bytes =
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   130
    {
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   131
      val out = new File_Content_Output(buffer)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   132
      write(buffer, out)
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   133
      out.content()
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   134
    }
54511
1fd24c96ce9b caching of blob;
wenzelm
parents: 54509
diff changeset
   135
  }
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   136
64835
fd1efd6dd385 resolve dependencies implicitly via File_Model, without jEdit Buffer_Model;
wenzelm
parents: 64834
diff changeset
   137
  def make_file_content(buffer: Buffer): Bytes = (new File_Content(buffer)).content()
56666
229309cbc508 avoid accidental use of scala.language.reflectiveCalls;
wenzelm
parents: 56502
diff changeset
   138
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   139
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   140
  /* theory text edits */
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54517
diff changeset
   141
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   142
  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
   143
  {
59319
wenzelm
parents: 59078
diff changeset
   144
    if (change.syntax_changed.nonEmpty)
64836
wenzelm
parents: 64835
diff changeset
   145
      GUI_Thread.later { Document_Model.syntax_changed(change.syntax_changed) }
60917
0607869c2ff3 more thorough reload;
wenzelm
parents: 60916
diff changeset
   146
    if (change.deps_changed ||
61023
46df28442a80 clarified undefined_blobs: already loaded theories are suppressed;
wenzelm
parents: 60988
diff changeset
   147
        PIDE.options.bool("jedit_auto_resolve") && undefined_blobs(change.version.nodes).nonEmpty)
65243
ba5ce07e06a7 proper plugin access;
wenzelm
parents: 64858
diff changeset
   148
      PIDE.plugin.deps_changed()
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56208
diff changeset
   149
  }
44577
96b6388d06c4 separate module for jEdit primitives for loading theory files;
wenzelm
parents:
diff changeset
   150
}