src/Pure/PIDE/resources.scala
author wenzelm
Sat, 03 Sep 2022 17:20:35 +0200
changeset 76046 507c65cc4332
parent 75941 4bbbbaa656f1
child 76049 d6c6e787cd86
permissions -rw-r--r--
back to more traditional import_name (reverting cceb10dcc9f9), e.g. relevant for "isabelle jedit -l CTT src/CTT/ex/Elimination.thy" to produce proper error "Cannot update finished theory CTT.Elimination";
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
56823
37be55461dbe more frugal access to theory text via Reader, reduced costs for I/O text decoding;
wenzelm
parents: 56801
diff changeset
    10
import scala.util.parsing.input.Reader
48885
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
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    15
object Resources {
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72957
diff changeset
    16
  def empty: Resources =
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72957
diff changeset
    17
    new Resources(Sessions.Structure.empty, Sessions.Structure.empty.bootstrap)
74755
510296c0d8d1 clarified signature: avoid potential misunderstanding of Resources.empty as proper Resources;
wenzelm
parents: 74696
diff changeset
    18
74756
a6c7a257b713 clarified signature: prefer static operations;
wenzelm
parents: 74755
diff changeset
    19
  def hidden_node(name: Document.Node.Name): Boolean =
a6c7a257b713 clarified signature: prefer static operations;
wenzelm
parents: 74755
diff changeset
    20
    !name.is_theory || name.theory == Sessions.root_name || File_Format.registry.is_theory(name)
a6c7a257b713 clarified signature: prefer static operations;
wenzelm
parents: 74755
diff changeset
    21
75941
4bbbbaa656f1 clarified modules;
wenzelm
parents: 75922
diff changeset
    22
  def html_document(snapshot: Document.Snapshot): Option[Browser_Info.HTML_Document] =
74756
a6c7a257b713 clarified signature: prefer static operations;
wenzelm
parents: 74755
diff changeset
    23
    File_Format.registry.get(snapshot.node_name).flatMap(_.html_document(snapshot))
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72957
diff changeset
    24
}
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72957
diff changeset
    25
65359
9ca34f0407a9 provide session qualifier via resources;
wenzelm
parents: 65358
diff changeset
    26
class Resources(
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    27
  val sessions_structure: Sessions.Structure,
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
    28
  val session_base: Sessions.Base,
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72637
diff changeset
    29
  val log: Logger = No_Logger,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    30
  command_timings: List[Properties.T] = Nil
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    31
) {
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
    32
  resources =>
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    33
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    34
75768
be79948f7f23 clarified signature: more operations;
wenzelm
parents: 75752
diff changeset
    35
  override def toString: String = "Resources(" + session_base.toString + ")"
be79948f7f23 clarified signature: more operations;
wenzelm
parents: 75752
diff changeset
    36
be79948f7f23 clarified signature: more operations;
wenzelm
parents: 75752
diff changeset
    37
72637
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    38
  /* init session */
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    39
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    40
  def init_session_yxml: String = {
72637
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    41
    import XML.Encode._
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    42
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    43
    YXML.string_of_body(
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    44
      pair(list(pair(string, properties)),
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    45
      pair(list(pair(string, string)),
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    46
      pair(list(pair(string, list(string))),
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72637
diff changeset
    47
      pair(list(properties),
74671
df12779c3ce8 more PIDE markup;
wenzelm
parents: 73565
diff changeset
    48
      pair(list(pair(string, properties)),
75586
b2b097624e4c clarified types and defaults;
wenzelm
parents: 75406
diff changeset
    49
      pair(list(Scala.encode_fun),
74696
0554a5c4c191 unused (see also 217e6cf61453, 5e7916535860);
wenzelm
parents: 74671
diff changeset
    50
      pair(list(pair(string, string)), list(string))))))))(
72646
wenzelm
parents: 72638
diff changeset
    51
       (sessions_structure.session_positions,
wenzelm
parents: 72638
diff changeset
    52
       (sessions_structure.dest_session_directories,
wenzelm
parents: 72638
diff changeset
    53
       (sessions_structure.bibtex_entries,
72638
2a7fc87495e0 refer to command_timings/last_timing via resources;
wenzelm
parents: 72637
diff changeset
    54
       (command_timings,
74671
df12779c3ce8 more PIDE markup;
wenzelm
parents: 73565
diff changeset
    55
       (Command_Span.load_commands.map(cmd => (cmd.name, cmd.position)),
75586
b2b097624e4c clarified types and defaults;
wenzelm
parents: 75406
diff changeset
    56
       (Scala.functions,
75885
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
    57
       (sessions_structure.global_theories.toList,
74696
0554a5c4c191 unused (see also 217e6cf61453, 5e7916535860);
wenzelm
parents: 74671
diff changeset
    58
        session_base.loaded_theories.keys)))))))))
72637
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    59
  }
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    60
fd68c9c1b90b more uniform Resources.init_session via YXML;
wenzelm
parents: 72072
diff changeset
    61
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    62
  /* file formats */
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    63
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    64
  def make_theory_name(name: Document.Node.Name): Option[Document.Node.Name] =
71733
6c470c918aad more general support for isabelle_scala_service;
wenzelm
parents: 71726
diff changeset
    65
    File_Format.registry.get(name).flatMap(_.make_theory_name(resources, name))
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    66
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    67
  def make_theory_content(thy_name: Document.Node.Name): Option[String] =
71733
6c470c918aad more general support for isabelle_scala_service;
wenzelm
parents: 71726
diff changeset
    68
    File_Format.registry.get_theory(thy_name).flatMap(_.make_theory_content(resources, thy_name))
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69008
diff changeset
    69
64839
842163abfc0d tuned signature;
wenzelm
parents: 64835
diff changeset
    70
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    71
  /* file-system operations */
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    72
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    73
  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
    74
    (Path.explode(dir) + source_path).expand.implode
44953
cdfe42f1267c sane default for class Thy_Load;
wenzelm
parents: 44616
diff changeset
    75
65488
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    76
  def append(node_name: Document.Node.Name, source_path: Path): String =
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    77
    append(node_name.master_dir, source_path)
331f09d9535e tuned signature;
wenzelm
parents: 65487
diff changeset
    78
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    79
  def file_node(file: Path, dir: String = "", theory: String = ""): Document.Node.Name = {
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    80
    val node = append(dir, file)
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    81
    val master_dir = append(dir, file.dir)
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    82
    Document.Node.Name(node, master_dir, theory)
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    83
  }
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
    84
70716
a8afe8eb3529 tuned signature;
wenzelm
parents: 70713
diff changeset
    85
  def loaded_theory_node(theory: String): Document.Node.Name =
a8afe8eb3529 tuned signature;
wenzelm
parents: 70713
diff changeset
    86
    Document.Node.Name(theory, "", theory)
a8afe8eb3529 tuned signature;
wenzelm
parents: 70713
diff changeset
    87
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
    88
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    89
  /* source files of Isabelle/ML bootstrap */
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    90
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
    91
  def source_file(raw_name: String): Option[String] = {
64657
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    92
    if (Path.is_wellformed(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    93
      if (Path.is_valid(raw_name)) {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    94
        def check(p: Path): Option[Path] = if (p.is_file) Some(p) else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    95
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    96
        val path = Path.explode(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    97
        val path1 =
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    98
          if (path.is_absolute || path.is_current) check(path)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
    99
          else {
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   100
            check(Path.explode("~~/src/Pure") + path) orElse
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   101
              (if (Isabelle_System.getenv("ML_SOURCES") == "") None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   102
               else check(Path.explode("$ML_SOURCES") + path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   103
          }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   104
        Some(File.platform_path(path1 getOrElse path))
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   105
      }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   106
      else None
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   107
    }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   108
    else Some(raw_name)
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   109
  }
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   110
6209e0f7a4e8 clarified modules;
wenzelm
parents: 64656
diff changeset
   111
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   112
  /* theory files */
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   113
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   114
  def load_commands(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   115
    syntax: Outer_Syntax,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   116
    name: Document.Node.Name
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   117
  ) : () => List[Command_Span.Span] = {
66918
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66917
diff changeset
   118
    val (is_utf8, raw_text) =
ec2b50aeb0dd more robust treatment of UTF8 in raw byte sources;
wenzelm
parents: 66917
diff changeset
   119
      with_thy_reader(name, reader => (Scan.reader_is_utf8(reader), reader.source.toString))
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   120
    () =>
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   121
      {
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   122
        if (syntax.has_load_commands(raw_text)) {
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   123
          val text = Symbol.decode(Scan.reader_decode_utf8(is_utf8, raw_text))
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   124
          syntax.parse_spans(text).filter(_.is_load_command(syntax))
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   125
        }
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   126
        else Nil
66698
5b9dc3f7bcde prefer sequential file-system access, but parallel parse;
wenzelm
parents: 66697
diff changeset
   127
      }
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   128
  }
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   129
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   130
  def loaded_files(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   131
    syntax: Outer_Syntax,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   132
    name: Document.Node.Name,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   133
    spans: List[Command_Span.Span]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   134
  ) : List[Path] = {
72817
1c378ab75d48 clarified signature, notably access to blob files;
wenzelm
parents: 72816
diff changeset
   135
    val dir = name.master_dir_path
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   136
    for { span <- spans; file <- span.loaded_files(syntax).files }
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   137
      yield (dir + Path.explode(file)).expand
66695
wenzelm
parents: 65532
diff changeset
   138
  }
wenzelm
parents: 65532
diff changeset
   139
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   140
  def pure_files(syntax: Outer_Syntax): List[Path] = {
69560
195371990820 clarified signature;
wenzelm
parents: 69393
diff changeset
   141
    val pure_dir = Path.explode("~~/src/Pure")
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   142
    for {
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   143
      (name, theory) <- Thy_Header.ml_roots
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   144
      path = (pure_dir + Path.explode(name)).expand
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   145
      node_name = Document.Node.Name(path.implode, path.dir.implode, theory)
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   146
      file <- loaded_files(syntax, node_name, load_commands(syntax, node_name)())
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   147
    } yield file
66701
d181f8a0e857 maintain loaded_files for each theory;
wenzelm
parents: 66700
diff changeset
   148
  }
66696
8f863dae78a0 clarified pure_files, based on uniform loaded_files;
wenzelm
parents: 66695
diff changeset
   149
75885
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   150
  def global_theory(theory: String): Boolean =
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   151
    sessions_structure.global_theories.isDefinedAt(theory)
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   152
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   153
  def theory_name(qualifier: String, theory: String): String =
75885
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   154
    if (Long_Name.is_qualified(theory) || global_theory(theory)) theory
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   155
    else Long_Name.qualify(qualifier, theory)
65471
05e5bffcf1d8 clarified loaded_theories: map to qualified theory name;
wenzelm
parents: 65467
diff changeset
   156
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   157
  def find_theory_node(theory: String): Option[Document.Node.Name] = {
72776
27a464537fb0 tuned signature;
wenzelm
parents: 72775
diff changeset
   158
    val thy_file = Path.basic(Long_Name.base_name(theory)).thy
75884
3d8b37b1d798 clarified signature: avoid object-oriented HTML_Context;
wenzelm
parents: 75768
diff changeset
   159
    val session = sessions_structure.theory_qualifier(theory)
70712
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   160
    val dirs =
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   161
      sessions_structure.get(session) match {
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   162
        case Some(info) => info.dirs
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   163
        case None => Nil
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   164
      }
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   165
    dirs.collectFirst({
72857
a9e091ccd450 clarified signature;
wenzelm
parents: 72817
diff changeset
   166
      case dir if (dir + thy_file).is_file => file_node(dir + thy_file, theory = theory) })
70712
a3cfe859d915 find theories via session directories only -- ignore known_theories;
wenzelm
parents: 70683
diff changeset
   167
  }
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
   168
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   169
  def import_name(qualifier: String, dir: String, s: String): Document.Node.Name = {
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   170
    val theory = theory_name(qualifier, Thy_Header.import_name(s))
76046
507c65cc4332 back to more traditional import_name (reverting cceb10dcc9f9), e.g. relevant for "isabelle jedit -l CTT src/CTT/ex/Elimination.thy" to produce proper error "Cannot update finished theory CTT.Elimination";
wenzelm
parents: 75941
diff changeset
   171
    if (session_base.loaded_theory(theory)) loaded_theory_node(theory)
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   172
    else {
70683
8c7706b053c7 find theory files via session structure: much faster Prover IDE startup;
wenzelm
parents: 70682
diff changeset
   173
      find_theory_node(theory) match {
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   174
        case Some(node_name) => node_name
76046
507c65cc4332 back to more traditional import_name (reverting cceb10dcc9f9), e.g. relevant for "isabelle jedit -l CTT src/CTT/ex/Elimination.thy" to produce proper error "Cannot update finished theory CTT.Elimination";
wenzelm
parents: 75941
diff changeset
   175
        case None =>
507c65cc4332 back to more traditional import_name (reverting cceb10dcc9f9), e.g. relevant for "isabelle jedit -l CTT src/CTT/ex/Elimination.thy" to produce proper error "Cannot update finished theory CTT.Elimination";
wenzelm
parents: 75941
diff changeset
   176
          if (Thy_Header.is_base_name(s) && Long_Name.is_qualified(s)) loaded_theory_node(theory)
507c65cc4332 back to more traditional import_name (reverting cceb10dcc9f9), e.g. relevant for "isabelle jedit -l CTT src/CTT/ex/Elimination.thy" to produce proper error "Cannot update finished theory CTT.Elimination";
wenzelm
parents: 75941
diff changeset
   177
          else file_node(Path.explode(s).thy, dir = dir, theory = theory)
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   178
      }
65472
wenzelm
parents: 65471
diff changeset
   179
    }
67104
a2fa0c6a7aff clarified theory_name vs. loaded_theory: proper import_name for already loaded theories from other sessions (amending 4c98c929a12a);
wenzelm
parents: 67060
diff changeset
   180
  }
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
   181
65529
wenzelm
parents: 65503
diff changeset
   182
  def import_name(name: Document.Node.Name, s: String): Document.Node.Name =
75884
3d8b37b1d798 clarified signature: avoid object-oriented HTML_Context;
wenzelm
parents: 75768
diff changeset
   183
    import_name(sessions_structure.theory_qualifier(name), name.master_dir, s)
65489
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   184
70633
b99b925dbd84 tuned signature;
wenzelm
parents: 70625
diff changeset
   185
  def import_name(info: Sessions.Info, s: String): Document.Node.Name =
b99b925dbd84 tuned signature;
wenzelm
parents: 70625
diff changeset
   186
    import_name(info.name, info.dir.implode, s)
b99b925dbd84 tuned signature;
wenzelm
parents: 70625
diff changeset
   187
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   188
  def find_theory(file: JFile): Option[Document.Node.Name] = {
70718
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   189
    for {
75885
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   190
      qualifier <- sessions_structure.session_directories.get(File.canonical(file).getParentFile)
70718
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   191
      theory_base <- proper_string(Thy_Header.theory_name(file.getName))
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   192
      theory = theory_name(qualifier, theory_base)
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   193
      theory_node <- find_theory_node(theory)
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   194
      if File.eq(theory_node.path.file, file)
5bb025e24224 clarified inversion of file name to theory name, notably for Windows;
wenzelm
parents: 70717
diff changeset
   195
    } yield theory_node
70673
b0172698d0d3 theory_name based on session_directories: no need for expensive all_known;
wenzelm
parents: 70652
diff changeset
   196
  }
b0172698d0d3 theory_name based on session_directories: no need for expensive all_known;
wenzelm
parents: 70652
diff changeset
   197
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   198
  def complete_import_name(context_name: Document.Node.Name, s: String): List[String] = {
75884
3d8b37b1d798 clarified signature: avoid object-oriented HTML_Context;
wenzelm
parents: 75768
diff changeset
   199
    val context_session = sessions_structure.theory_qualifier(context_name)
70713
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   200
    val context_dir =
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   201
      try { Some(context_name.master_dir_path) }
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   202
      catch { case ERROR(_) => None }
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   203
    (for {
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   204
      (session, (info, _))  <- sessions_structure.imports_graph.iterator
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   205
      dir <- (if (session == context_session) context_dir.toList else info.dirs).iterator
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   206
      theory <- Thy_Header.try_read_dir(dir).iterator
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   207
      if Completion.completed(s)(theory)
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   208
    } yield {
75885
8342cba8eae8 clarified signature: avoid constants from Sessions.Structure within Session.Base;
wenzelm
parents: 75884
diff changeset
   209
      if (session == context_session || global_theory(theory)) theory
70713
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   210
      else Long_Name.qualify(session, theory)
fd188463066e clarified theory imports completion, based on session directories and current master directory (no support for local session-subdirectories);
wenzelm
parents: 70712
diff changeset
   211
    }).toList.sorted
66767
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   212
  }
294c2e9a689e clarified modules;
wenzelm
parents: 66766
diff changeset
   213
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   214
  def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A = {
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   215
    val path = name.path
69393
ed0824ef337e static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents: 69255
diff changeset
   216
    if (path.is_file) using(Scan.byte_reader(path.file))(f)
66771
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   217
    else if (name.node == name.theory)
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   218
      error("Cannot load theory " + quote(name.theory))
925d10a7a610 clarified error for bad session-qualified imports;
wenzelm
parents: 66767
diff changeset
   219
    else error ("Cannot load theory file " + path)
64656
wenzelm
parents: 64654
diff changeset
   220
  }
wenzelm
parents: 64654
diff changeset
   221
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   222
  def check_thy(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   223
    node_name: Document.Node.Name,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   224
    reader: Reader[Char],
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   225
    command: Boolean = true,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   226
    strict: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   227
  ): Document.Node.Header = {
64826
c97296294f6d clarified check_thy_reader: check node_name here;
wenzelm
parents: 64825
diff changeset
   228
    if (node_name.is_theory && reader.source.length > 0) {
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   229
      try {
72777
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   230
        val header = Thy_Header.read(node_name, reader, command = command, strict = strict)
72778
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   231
        val imports =
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   232
          header.imports.map({ case (s, pos) =>
67296
888aa91f0556 clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents: 67294
diff changeset
   233
            val name = import_name(node_name, s)
75922
b327e5d5d6b4 tuned signature;
wenzelm
parents: 75919
diff changeset
   234
            if (Sessions.illegal_theory(name.theory_base_name)) {
b327e5d5d6b4 tuned signature;
wenzelm
parents: 75919
diff changeset
   235
              error("Illegal theory name " + quote(name.theory_base_name) + Position.here(pos))
75919
wenzelm
parents: 75914
diff changeset
   236
            }
wenzelm
parents: 75914
diff changeset
   237
            else (name, pos)
67296
888aa91f0556 clarified check, notably for bibtex theory (amending 5f082b1fa9fa);
wenzelm
parents: 67294
diff changeset
   238
          })
72778
83e581c9a5f1 tuned signature;
wenzelm
parents: 72777
diff changeset
   239
        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
   240
      }
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   241
      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
   242
    }
57615
df1b3452d71c more explicit discrimination of empty nodes -- suppress from Theories panel;
wenzelm
parents: 56913
diff changeset
   243
    else Document.Node.no_header
46737
09ab89658a5d clarified module Thy_Load;
wenzelm
parents: 44953
diff changeset
   244
  }
46748
8f3ae4d04a2d refined node_header -- more direct buffer access (again);
wenzelm
parents: 46737
diff changeset
   245
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   246
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   247
  /* special header */
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   248
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   249
  def special_header(name: Document.Node.Name): Option[Document.Node.Header] = {
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   250
    val imports =
75406
85e8b4c2b9a9 clarified signature: avoid ambiguity in scala3;
wenzelm
parents: 75393
diff changeset
   251
      if (name.theory == Sessions.root_name) List(import_name(name, Sessions.theory_import))
67215
03d0c958d65a PIDE markup for session ROOT files;
wenzelm
parents: 67104
diff changeset
   252
      else if (Thy_Header.is_ml_root(name.theory)) List(import_name(name, Thy_Header.ML_BOOTSTRAP))
65489
f3076367f4a8 tuned signature;
wenzelm
parents: 65488
diff changeset
   253
      else if (Thy_Header.is_bootstrap(name.theory)) List(import_name(name, Thy_Header.PURE))
65445
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   254
      else Nil
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   255
    if (imports.isEmpty) None
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   256
    else Some(Document.Node.Header(imports.map((_, Position.none))))
e9e7f5f5794c more qualifier treatment, but in the end it is still ignored;
wenzelm
parents: 65441
diff changeset
   257
  }
64673
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   258
b5965890e54d more uniform treatment of file name vs. theory name and special header;
wenzelm
parents: 64657
diff changeset
   259
64797
wenzelm
parents: 64759
diff changeset
   260
  /* blobs */
wenzelm
parents: 64759
diff changeset
   261
wenzelm
parents: 64759
diff changeset
   262
  def undefined_blobs(nodes: Document.Nodes): List[Document.Node.Name] =
wenzelm
parents: 64759
diff changeset
   263
    (for {
wenzelm
parents: 64759
diff changeset
   264
      (node_name, node) <- nodes.iterator
65361
ecefb68dc21d tuned signature;
wenzelm
parents: 65359
diff changeset
   265
      if !session_base.loaded_theory(node_name)
64797
wenzelm
parents: 64759
diff changeset
   266
      cmd <- node.load_commands.iterator
wenzelm
parents: 64759
diff changeset
   267
      name <- cmd.blobs_undefined.iterator
wenzelm
parents: 64759
diff changeset
   268
    } yield name).toList
wenzelm
parents: 64759
diff changeset
   269
wenzelm
parents: 64759
diff changeset
   270
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   271
  /* document changes */
50566
b43c4f660320 tuned signature: use thy_load to adapt to prover/editor specific view on sources;
wenzelm
parents: 50415
diff changeset
   272
56316
b1cf8ddc2e04 propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents: 56315
diff changeset
   273
  def parse_change(
56315
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   274
      reparse_limit: Int,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   275
      previous: Document.Version,
c20053f67093 tuned signature;
wenzelm
parents: 56314
diff changeset
   276
      doc_blobs: Document.Blobs,
68336
09ac56914b29 Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents: 68317
diff changeset
   277
      edits: List[Document.Edit_Text],
70796
2739631ac368 discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
wenzelm
parents: 70740
diff changeset
   278
      consolidate: List[Document.Node.Name]): Session.Change =
2739631ac368 discontinued pointless dump_checkpoint and share_common_data -- superseded by base logic image in Isabelle/MMT;
wenzelm
parents: 70740
diff changeset
   279
    Thy_Syntax.parse_change(resources, reparse_limit, previous, doc_blobs, edits, consolidate)
55134
1b67b17cdad5 propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents: 54521
diff changeset
   280
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 72962
diff changeset
   281
  def commit(change: Session.Change): Unit = {}
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   282
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   283
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   284
  /* theory and file dependencies */
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   285
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   286
  def dependencies(
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   287
      thys: List[(Document.Node.Name, Position.T)],
71726
a5fda30edae2 clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents: 70796
diff changeset
   288
      progress: Progress = new Progress): Dependencies[Unit] =
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   289
    Dependencies.empty[Unit].require_thys((), thys, progress = progress)
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   290
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   291
  def session_dependencies(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   292
    info: Sessions.Info,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   293
    progress: Progress = new Progress
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   294
  ) : Dependencies[Options] = {
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   295
    info.theories.foldLeft(Dependencies.empty[Options]) {
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   296
      case (dependencies, (options, thys)) =>
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   297
        dependencies.require_thys(options,
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   298
          for { (thy, pos) <- thys } yield (import_name(info, thy), pos),
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   299
          progress = progress)
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   300
    }
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   301
  }
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   302
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   303
  object Dependencies {
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   304
    def empty[A]: Dependencies[A] = new Dependencies[A](Nil, Map.empty)
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   305
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   306
    private def show_path(names: List[Document.Node.Name]): String =
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   307
      names.map(name => quote(name.theory)).mkString(" via ")
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   308
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   309
    private def cycle_msg(names: List[Document.Node.Name]): String =
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   310
      "Cyclic dependency of " + show_path(names)
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   311
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   312
    private def required_by(initiators: List[Document.Node.Name]): String =
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   313
      if (initiators.isEmpty) ""
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   314
      else "\n(required by " + show_path(initiators.reverse) + ")"
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   315
  }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   316
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   317
  final class Dependencies[A] private(
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   318
    rev_entries: List[Document.Node.Entry],
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   319
    seen: Map[Document.Node.Name, A]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   320
  ) {
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   321
    private def cons(entry: Document.Node.Entry): Dependencies[A] =
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   322
      new Dependencies[A](entry :: rev_entries, seen)
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   323
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   324
    def require_thy(adjunct: A,
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   325
      thy: (Document.Node.Name, Position.T),
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   326
      initiators: List[Document.Node.Name] = Nil,
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   327
      progress: Progress = new Progress): Dependencies[A] = {
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   328
      val (name, pos) = thy
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   329
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   330
      def message: String =
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   331
        "The error(s) above occurred for theory " + quote(name.theory) +
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   332
          Dependencies.required_by(initiators) + Position.here(pos)
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   333
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   334
      if (seen.isDefinedAt(name)) this
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   335
      else {
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   336
        val dependencies1 = new Dependencies[A](rev_entries, seen + (name -> adjunct))
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   337
        if (session_base.loaded_theory(name)) dependencies1
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   338
        else {
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   339
          try {
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   340
            if (initiators.contains(name)) error(Dependencies.cycle_msg(initiators))
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   341
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   342
            progress.expose_interrupt()
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   343
            val header =
72772
a9ef39041114 clarified signature;
wenzelm
parents: 72765
diff changeset
   344
              try {
72777
164cb0806d0a proper positions for inlined command messages, e.g. for completion within theory header;
wenzelm
parents: 72776
diff changeset
   345
                with_thy_reader(name, check_thy(name, _, command = false)).cat_errors(message)
72772
a9ef39041114 clarified signature;
wenzelm
parents: 72765
diff changeset
   346
              }
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   347
              catch { case ERROR(msg) => cat_error(msg, message) }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   348
            val entry = Document.Node.Entry(name, header)
70638
f164cec7ac22 clarified signature: prefer operations without position;
wenzelm
parents: 70634
diff changeset
   349
            dependencies1.require_thys(adjunct, header.imports_pos,
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   350
              initiators = name :: initiators, progress = progress).cons(entry)
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   351
          }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   352
          catch {
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   353
            case e: Throwable =>
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   354
              dependencies1.cons(Document.Node.Entry(name, Document.Node.bad_header(Exn.message(e))))
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   355
          }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   356
        }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   357
      }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   358
    }
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   359
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   360
    def require_thys(adjunct: A,
69007
2ac19d346b83 clarified signature;
wenzelm
parents: 68762
diff changeset
   361
        thys: List[(Document.Node.Name, Position.T)],
71726
a5fda30edae2 clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents: 70796
diff changeset
   362
        progress: Progress = new Progress,
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   363
        initiators: List[Document.Node.Name] = Nil): Dependencies[A] =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   364
      thys.foldLeft(this)(_.require_thy(adjunct, _, progress = progress, initiators = initiators))
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   365
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   366
    def entries: List[Document.Node.Entry] = rev_entries.reverse
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   367
67059
df7d728103f1 tuned signature;
wenzelm
parents: 67056
diff changeset
   368
    def theories: List[Document.Node.Name] = entries.map(_.name)
72062
d0909b5d88eb tuned signature;
wenzelm
parents: 71733
diff changeset
   369
    def theories_adjunct: List[(Document.Node.Name, A)] = theories.map(name => (name, seen(name)))
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   370
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   371
    def errors: List[String] = entries.flatMap(_.header.errors)
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   372
69008
d55783ea6cf6 more detailed session dependencies, with conditions for theories;
wenzelm
parents: 69007
diff changeset
   373
    def check_errors: Dependencies[A] =
67056
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67053
diff changeset
   374
      errors match {
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67053
diff changeset
   375
        case Nil => this
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67053
diff changeset
   376
        case errs => error(cat_lines(errs))
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67053
diff changeset
   377
      }
e35ae3eeec93 load theories via PIDE document update;
wenzelm
parents: 67053
diff changeset
   378
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   379
    lazy val theory_graph: Document.Node.Name.Graph[Unit] = {
70652
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   380
      val regular = theories.toSet
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   381
      val irregular =
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   382
        (for {
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   383
          entry <- entries.iterator
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   384
          imp <- entry.header.imports
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   385
          if !regular(imp)
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   386
        } yield imp).toSet
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   387
70674
29bb1ebb188f clarified signature: proper Document.Node.Ordering conforming to equality (e.g. required in situations where theory names are ambiguous due to overlapping session directories);
wenzelm
parents: 70673
diff changeset
   388
      Document.Node.Name.make_graph(
70652
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   389
        irregular.toList.map(name => ((name, ()), Nil)) :::
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   390
        entries.map(entry => ((entry.name, ()), entry.header.imports)))
d5e4231caa06 more complete graph;
wenzelm
parents: 70650
diff changeset
   391
    }
70650
fa04b549f652 tuned signature;
wenzelm
parents: 70645
diff changeset
   392
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   393
    lazy val loaded_theories: Graph[String, Outer_Syntax] =
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   394
      entries.foldLeft(session_base.loaded_theories) {
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   395
        case (graph, entry) =>
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   396
          val name = entry.name.theory
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   397
          val imports = entry.header.imports.map(_.theory)
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   398
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   399
          val graph1 = (name :: imports).foldLeft(graph)(_.default_node(_, Outer_Syntax.empty))
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   400
          val graph2 = imports.foldLeft(graph1)(_.add_edge(_, name))
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   401
73359
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   402
          val syntax0 = if (name == Thy_Header.PURE) List(Thy_Header.bootstrap_syntax) else Nil
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   403
          val syntax1 = (name :: graph2.imm_preds(name).toList).map(graph2.get_node)
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   404
          val syntax = Outer_Syntax.merge(syntax0 ::: syntax1) + entry.header
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   405
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   406
          graph2.map_node(name, _ => syntax)
d8a0e996614b tuned --- fewer warnings;
wenzelm
parents: 73340
diff changeset
   407
      }
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   408
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   409
    def get_syntax(name: Document.Node.Name): Outer_Syntax =
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   410
      loaded_theories.get_node(name.theory)
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   411
75745
ebbb7d6eb296 avoid multiple load_commands;
wenzelm
parents: 75586
diff changeset
   412
    lazy val load_commands: List[(Document.Node.Name, List[Command_Span.Span])] =
72799
5dc7165e8a26 clarified signature;
wenzelm
parents: 72778
diff changeset
   413
      theories.zip(
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   414
        Par_List.map((e: () => List[Command_Span.Span]) => e(),
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   415
          theories.map(name => resources.load_commands(get_syntax(name), name))))
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   416
      .filter(p => p._2.nonEmpty)
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   417
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   418
    def loaded_files(
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   419
      name: Document.Node.Name,
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   420
      spans: List[Command_Span.Span]
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   421
    ) : (String, List[Path]) = {
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   422
      val theory = name.theory
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   423
      val syntax = get_syntax(name)
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   424
      val files1 = resources.loaded_files(syntax, name, spans)
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   425
      val files2 = if (theory == Thy_Header.PURE) pure_files(syntax) else Nil
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   426
      (theory, files1 ::: files2)
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   427
    }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   428
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   429
    def loaded_files: List[Path] =
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   430
      for {
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   431
        (name, spans) <- load_commands
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   432
        file <- loaded_files(name, spans)._2
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   433
      } yield file
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   434
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74756
diff changeset
   435
    def imported_files: List[Path] = {
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   436
      val base_theories =
67059
df7d728103f1 tuned signature;
wenzelm
parents: 67056
diff changeset
   437
        loaded_theories.all_preds(theories.map(_.theory)).
72063
25985d757b0a tuned --- based on hints by IntelliJ;
wenzelm
parents: 72062
diff changeset
   438
          filter(session_base.loaded_theories.defined)
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   439
70740
525c18b8ed53 clarified data structures;
wenzelm
parents: 70718
diff changeset
   440
      base_theories.map(theory => session_base.known_theories(theory).name.path) :::
72816
ea4f86914cb2 support for PIDE markup for auxiliary files ("blobs");
wenzelm
parents: 72799
diff changeset
   441
      base_theories.flatMap(session_base.known_loaded_files.withDefaultValue(Nil))
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   442
    }
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   443
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   444
    lazy val overall_syntax: Outer_Syntax =
72063
25985d757b0a tuned --- based on hints by IntelliJ;
wenzelm
parents: 72062
diff changeset
   445
      Outer_Syntax.merge(loaded_theories.maximals.map(loaded_theories.get_node))
66959
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   446
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   447
    override def toString: String = entries.toString
015d47486fc8 clarified modules;
wenzelm
parents: 66918
diff changeset
   448
  }
43651
511df47bcadc some support for theory files within Isabelle/Scala session;
wenzelm
parents:
diff changeset
   449
}