src/Pure/PIDE/resources.scala
author wenzelm
Wed, 21 Dec 2016 23:54:21 +0100
changeset 64654 31b681e38c70
parent 63584 68751fe1c036
child 64656 65c8a7780538
permissions -rw-r--r--
clarified signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
     1
/*  Title:      Pure/PIDE/resources.scala
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     3
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
     4
Resources for theories and auxiliary files.
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     5
*/
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     6
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     7
package isabelle
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
     8
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
     9
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    10
import scala.annotation.tailrec
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    11
import scala.util.parsing.input.Reader
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    12
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    13
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    14
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    15
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
    16
object Resources
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    17
{
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    18
  def thy_path(path: Path): Path = path.ext("thy")
60835
6512bb0b1ff4 clarified management of (single) session;
wenzelm
parents: 59718
diff changeset
    19
6512bb0b1ff4 clarified management of (single) session;
wenzelm
parents: 59718
diff changeset
    20
  val empty: Resources = new Resources(Set.empty, Map.empty, Outer_Syntax.empty)
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    21
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    22
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    23
class Resources(
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    24
  val 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: 56393
diff changeset
    25
  val known_theories: Map[String, Document.Node.Name],
63584
68751fe1c036 tuned signature -- prover-independence is presently theoretical;
wenzelm
parents: 63579
diff changeset
    26
  val base_syntax: Outer_Syntax)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    27
{
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    28
  /* document node names */
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    29
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    30
  def node_name(qualifier: String, raw_path: Path): Document.Node.Name =
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    31
  {
56913
df4cd6e1fdfa no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents: 56823
diff changeset
    32
    val no_qualifier = "" // FIXME
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    33
    val path = raw_path.expand
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    34
    val node = path.implode
56913
df4cd6e1fdfa no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents: 56823
diff changeset
    35
    val theory = Long_Name.qualify(no_qualifier, Thy_Header.thy_name(node).getOrElse(""))
54515
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    36
    val master_dir = if (theory == "") "" else path.dir.implode
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    37
    Document.Node.Name(node, master_dir, theory)
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    38
  }
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    39
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    40
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    41
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    42
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    43
  def append(dir: String, source_path: Path): String =
48711
8d381fdef898 need to expand path in order to resolve imports like "~~/src/Tools/Code_Generator";
wenzelm
parents: 48710
diff changeset
    44
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    45
64654
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    46
  def append_file(dir: String, raw_name: String): String =
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    47
    if (Path.is_valid(raw_name)) append(dir, Path.explode(raw_name))
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    48
    else raw_name
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    49
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    50
  def append_file_url(dir: String, raw_name: String): String =
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    51
    if (Path.is_valid(raw_name)) "file://" + append(dir, Path.explode(raw_name))
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    52
    else raw_name
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    53
31b681e38c70 clarified signature;
wenzelm
parents: 63584
diff changeset
    54
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    55
  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
    56
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    57
    val path = Path.explode(name.node)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    58
    if (!path.is_file) error("No such file: " + path.toString)
50291
674893679352 prefer Symbol.decode_strict in batch mode, to avoid files with spurious Unicode symbols that clash with Isabelle symbol interpretation;
wenzelm
parents: 50204
diff changeset
    59
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    60
    val reader = Scan.byte_reader(path.file)
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    61
    try { f(reader) } finally { reader.close }
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    62
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    63
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    64
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    65
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    66
63584
68751fe1c036 tuned signature -- prover-independence is presently theoretical;
wenzelm
parents: 63579
diff changeset
    67
  def loaded_files(syntax: Outer_Syntax, text: String): List[String] =
56393
22f533e6a049 more abstract Prover.Syntax, as proposed by Carst Tankink;
wenzelm
parents: 56392
diff changeset
    68
    if (syntax.load_commands_in(text)) {
57906
020df63dd0a9 tuned signature;
wenzelm
parents: 57905
diff changeset
    69
      val spans = syntax.parse_spans(text)
59689
7968c57ea240 simplified Command.resolve_files in ML, using blobs_index from Scala;
wenzelm
parents: 59683
diff changeset
    70
      spans.iterator.map(Command.span_files(syntax, _)._1).flatten.toList
56392
bc118a32a870 tuned signature (see also 0850d43cb355);
wenzelm
parents: 56387
diff changeset
    71
    }
bc118a32a870 tuned signature (see also 0850d43cb355);
wenzelm
parents: 56387
diff changeset
    72
    else Nil
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    73
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    74
  private def dummy_name(theory: 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: 56393
diff changeset
    75
    Document.Node.Name(theory + ".thy", "", theory)
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    76
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    77
  def import_name(qualifier: String, master: Document.Node.Name, s: String): Document.Node.Name =
51634
553953ad8165 more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents: 51293
diff changeset
    78
  {
56913
df4cd6e1fdfa no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents: 56823
diff changeset
    79
    val no_qualifier = "" // FIXME
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    80
    val thy1 = Thy_Header.base_name(s)
56913
df4cd6e1fdfa no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents: 56823
diff changeset
    81
    val thy2 = if (Long_Name.is_qualified(thy1)) thy1 else Long_Name.qualify(no_qualifier, thy1)
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    82
    (known_theories.get(thy1) orElse
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    83
     known_theories.get(thy2) orElse
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    84
     known_theories.get(Long_Name.base_name(thy1))) match {
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    85
      case Some(name) if loaded_theories(name.theory) => dummy_name(name.theory)
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    86
      case Some(name) => name
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    87
      case None =>
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    88
        val path = Path.explode(s)
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    89
        val theory = path.base.implode
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    90
        if (Long_Name.is_qualified(theory)) dummy_name(theory)
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    91
        else {
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    92
          val node = append(master.master_dir, Resources.thy_path(path))
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    93
          val master_dir = append(master.master_dir, path.dir)
56913
df4cd6e1fdfa no qualifier for now, to avoid confusion concerning loaded_theories in PIDE interaction;
wenzelm
parents: 56823
diff changeset
    94
          Document.Node.Name(node, master_dir, Long_Name.qualify(no_qualifier, theory))
56801
8dd9df88f647 some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
wenzelm
parents: 56393
diff changeset
    95
        }
51634
553953ad8165 more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents: 51293
diff changeset
    96
    }
553953ad8165 more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents: 51293
diff changeset
    97
  }
553953ad8165 more general Thy_Load.import_name, e.g. relevant for Isabelle/eclipse -- NB: Thy_Load serves as main hub for funny overriding to adapt to provers and editors;
wenzelm
parents: 51293
diff changeset
    98
59705
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
    99
  def check_thy_reader(qualifier: String, node_name: Document.Node.Name,
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
   100
    reader: Reader[Char], start: Token.Pos): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   101
  {
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   102
    if (reader.source.length > 0) {
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   103
      try {
59705
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
   104
        val header = Thy_Header.read(reader, start).decode_symbols
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   105
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   106
        val base_name = Long_Name.base_name(node_name.theory)
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   107
        val (name, pos) = header.name
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   108
        if (base_name != name)
59718
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   109
          error("Bad theory name " + quote(name) +
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   110
            " for file " + Resources.thy_path(Path.basic(base_name)) + Position.here(pos) +
5d0c539537c9 tuned message -- include completion;
wenzelm
parents: 59705
diff changeset
   111
            Completion.report_names(pos, 1, List((base_name, ("theory", base_name)))))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
   112
59694
d2bb4b5ed862 misc tuning -- more uniform ML vs. Scala;
wenzelm
parents: 59691
diff changeset
   113
        val imports =
59695
a03e0561bdbf clarified positions of theory imports;
wenzelm
parents: 59694
diff changeset
   114
          header.imports.map({ case (s, pos) => (import_name(qualifier, node_name, s), pos) })
63579
73939a9b70a3 support 'abbrevs' within theory header;
wenzelm
parents: 63020
diff changeset
   115
        Document.Node.Header(imports, header.keywords, header.abbrevs)
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   116
      }
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   117
      catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
   118
    }
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   119
    else Document.Node.no_header
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   120
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   121
59705
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
   122
  def check_thy(qualifier: String, name: Document.Node.Name, start: Token.Pos)
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
   123
    : Document.Node.Header =
740a0ca7e09b clarified span position;
wenzelm
parents: 59695
diff changeset
   124
    with_thy_reader(name, check_thy_reader(qualifier, name, _, start))
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   125
59691
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   126
  def check_file(file: String): Boolean =
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   127
    try {
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   128
      if (Url.is_wellformed(file)) Url.is_readable(file)
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   129
      else (new JFile(file)).isFile
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   130
    }
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   131
    catch { case ERROR(_) => false }
f6ff19188842 tuned signature;
wenzelm
parents: 59689
diff changeset
   132
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   133
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   134
  /* document changes */
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   135
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   136
  def parse_change(
56315
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   137
      reparse_limit: Int,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   138
      previous: Document.Version,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   139
      doc_blobs: Document.Blobs,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   140
      edits: List[Document.Edit_Text]): Session.Change =
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   141
    Thy_Syntax.parse_change(this, reparse_limit, previous, doc_blobs, edits)
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54521
diff changeset
   142
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   143
  def commit(change: Session.Change) { }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   144
}