src/Pure/PIDE/resources.scala
author wenzelm
Sat, 29 Mar 2014 10:17:09 +0100
changeset 56315 c20053f67093
parent 56314 9a513737a0b2
child 56316 b1cf8ddc2e04
permissions -rw-r--r--
tuned 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
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    11
48409
0d2114eb412a more explicit java.io.{File => JFile};
wenzelm
parents: 46938
diff changeset
    12
import java.io.{File => JFile}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    13
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    14
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
    15
object Resources
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    16
{
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    17
  def thy_path(path: Path): Path = path.ext("thy")
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    18
}
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    19
48422
9613780a805b determine source dependencies, relatively to preloaded theories;
wenzelm
parents: 48409
diff changeset
    20
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
    21
class Resources(val loaded_theories: Set[String] = Set.empty, val base_syntax: Outer_Syntax)
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
    22
{
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    23
  /* document node names */
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    24
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    25
  def node_name(raw_path: Path): Document.Node.Name =
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    26
  {
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    27
    val path = raw_path.expand
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    28
    val node = path.implode
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    29
    val theory = Thy_Header.thy_name(node).getOrElse("")
54515
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    30
    val master_dir = if (theory == "") "" else path.dir.implode
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    31
    Document.Node.Name(node, master_dir, theory)
54514
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    32
  }
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    33
6428dfab6520 clarified Thy_Load.node_name;
wenzelm
parents: 54513
diff changeset
    34
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    35
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    36
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    37
  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
    38
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    39
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    40
  def with_thy_text[A](name: Document.Node.Name, f: CharSequence => A): A =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    41
  {
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    42
    val path = Path.explode(name.node)
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    43
    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
    44
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
    45
    val text = File.read(path)
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
    46
    Symbol.decode_strict(text)
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
    47
    f(text)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    48
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    49
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    50
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    51
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    52
50415
0d60de55c58a fork slow part of Thy_Load.body_files only;
wenzelm
parents: 50414
diff changeset
    53
  def body_files_test(syntax: Outer_Syntax, text: String): Boolean =
56314
9a513737a0b2 tuned signature;
wenzelm
parents: 56209
diff changeset
    54
    syntax.load_commands.exists({ case (cmd, _) => text.containsSlice(cmd) })
50415
0d60de55c58a fork slow part of Thy_Load.body_files only;
wenzelm
parents: 50414
diff changeset
    55
0d60de55c58a fork slow part of Thy_Load.body_files only;
wenzelm
parents: 50414
diff changeset
    56
  def body_files(syntax: Outer_Syntax, text: String): List[String] =
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    57
  {
50415
0d60de55c58a fork slow part of Thy_Load.body_files only;
wenzelm
parents: 50414
diff changeset
    58
    val spans = Thy_Syntax.parse_spans(syntax.scan(text))
54513
5545aff878b1 inline blobs into command, via SHA1 digest;
wenzelm
parents: 51634
diff changeset
    59
    spans.iterator.map(Thy_Syntax.span_files(syntax, _)).flatten.toList
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    60
  }
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    61
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
    62
  def import_name(master: Document.Node.Name, s: String): Document.Node.Name =
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
    63
  {
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
    64
    val theory = Thy_Header.base_name(s)
54515
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    65
    if (loaded_theories(theory)) Document.Node.Name(theory + ".thy", "", theory)
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
    66
    else {
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
    67
      val path = Path.explode(s)
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
    68
      val node = append(master.master_dir, Resources.thy_path(path))
54515
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    69
      val master_dir = append(master.master_dir, path.dir)
570ba266f5b5 clarified boundary cases of Document.Node.Name;
wenzelm
parents: 54514
diff changeset
    70
      Document.Node.Name(node, master_dir, theory)
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
    71
    }
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
    72
  }
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
    73
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    74
  def check_thy_text(name: Document.Node.Name, text: CharSequence): Document.Node.Header =
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    75
  {
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    76
    try {
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    77
      val header = Thy_Header.read(text)
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    78
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    79
      val name1 = header.name
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    80
      if (name.theory != name1)
56208
06cc31dff138 clarifed module name;
wenzelm
parents: 55879
diff changeset
    81
        error("Bad file name " + Resources.thy_path(Path.basic(name.theory)) +
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    82
          " for theory " + quote(name1))
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    83
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
    84
      val imports = header.imports.map(import_name(name, _))
51293
05b1bbae748d discontinued obsolete 'uses' within theory header;
wenzelm
parents: 50641
diff changeset
    85
      Document.Node.Header(imports, header.keywords, Nil)
50414
e17a1f179bb0 explore theory_body_files via future, for improved performance;
wenzelm
parents: 50291
diff changeset
    86
    }
50641
wenzelm
parents: 50566
diff changeset
    87
    catch { case exn: Throwable => Document.Node.bad_header(Exn.message(exn)) }
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    88
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
    89
48885
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    90
  def check_thy(name: Document.Node.Name): Document.Node.Header =
d5fdaf7dd1f8 find files via load commands within theory text;
wenzelm
parents: 48883
diff changeset
    91
    with_thy_text(name, check_thy_text(name, _))
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
    92
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
    93
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
    94
  /* theory text edits */
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
    95
56315
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
    96
  def parse_edits(
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
    97
      reparse_limit: Int,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
    98
      previous: Document.Version,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
    99
      doc_blobs: Document.Blobs,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   100
      edits: List[Document.Edit_Text]): Session.Change =
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   101
    Thy_Syntax.parse_edits(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
   102
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54521
diff changeset
   103
  def syntax_changed() { }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   104
}
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   105