src/Pure/PIDE/document_info.scala
author wenzelm
Thu, 06 Jun 2024 11:53:52 +0200
changeset 80266 d52be75ae60b
parent 76656 a8f452f7c503
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/PIDE/document_info.scala
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     3
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     4
Persistent document information --- for presentation purposes.
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     6
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     8
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
     9
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    10
object Document_Info {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    11
  sealed case class Session(
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    12
    name: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    13
    used_theories: List[String],
75971
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
    14
    loaded_theories: Map[String, Theory],
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
    15
    build_uuid: String
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
    16
  ) {
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
    17
    if (build_uuid.isEmpty) error("Missing build_uuid for session " + quote(name))
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
    18
  }
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    19
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    20
  object Theory {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    21
    def apply(
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    22
      name: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    23
      files: List[String],
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    24
      static_session: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    25
      dynamic_session: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    26
      entities: List[Export_Theory.Entity0],
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    27
      others: List[String]
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    28
    ): Theory = {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    29
      val entities1 =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    30
        entities.filter(e => e.file.nonEmpty && Position.Range.unapply(e.pos).isDefined)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    31
      new Theory(name, files, static_session, dynamic_session, entities1, others)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    32
    }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    33
  }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    34
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    35
  class Theory private(
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    36
    val name: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    37
    val files: List[String],
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    38
    val static_session: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    39
    val dynamic_session: String,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    40
    entities: List[Export_Theory.Entity0],
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    41
    others: List[String]
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    42
  ) {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    43
    override def toString: String = name
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    44
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    45
    val (thy_file, blobs_files) =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    46
      files match {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    47
        case Nil => error("Unknown theory file for " + quote(name))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    48
        case a :: bs =>
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    49
          def for_theory: String = " for theory " + quote(name)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    50
          if (!File.is_thy(a)) error("Bad .thy file " + quote(a) + for_theory)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    51
          for (b <- bs if File.is_thy(b)) error("Bad auxiliary file " + quote(b) + for_theory)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    52
          (a, bs)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    53
      }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    54
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    55
    def home_session: Boolean = static_session == dynamic_session
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    56
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    57
    def print_short: String =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    58
      if (home_session) Long_Name.base_name(name) else name
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    59
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    60
    def print_long: String =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    61
      "theory " + quote(name) +
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    62
      (if (home_session) "" else " (session " + quote(dynamic_session) + ")")
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    63
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    64
    private lazy val by_file_range: Map[(String, Symbol.Range), List[Export_Theory.Entity0]] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    65
      entities.groupBy(entity => (entity.file, entity.range))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    66
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    67
    private lazy val by_file_kname: Map[(String, String), Export_Theory.Entity0] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    68
      (for {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    69
        entity <- entities
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    70
        file <- Position.File.unapply(entity.pos)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    71
      } yield (file, entity.kname) -> entity).toMap
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    72
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    73
    def get_defs(file: String, range: Symbol.Range): List[Export_Theory.Entity0] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    74
      by_file_range.getOrElse((file, range), Nil)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    75
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    76
    def get_def(file: String, kind: String, name: String): Option[Export_Theory.Entity0] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    77
      by_file_kname.get((file, Export_Theory.export_kind_name(kind, name)))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    78
75941
4bbbbaa656f1 clarified modules;
wenzelm
parents: 75910
diff changeset
    79
    def elements(elements: Browser_Info.Elements): Browser_Info.Elements =
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    80
      elements.copy(entity = others.foldLeft(elements.entity)(_ + _))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    81
  }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    82
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    83
  val empty: Document_Info = new Document_Info(Map.empty)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    84
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    85
  def read(
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    86
    database_context: Export.Database_Context,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    87
    deps: Sessions.Deps,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    88
    sessions: List[String]
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    89
  ): Document_Info = {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    90
    val sessions_structure = deps.sessions_structure
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    91
    val sessions_requirements = sessions_structure.build_requirements(sessions)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    92
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
    93
    def read_theory(theory_context: Export.Theory_Context): Option[Document_Info.Theory] =
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    94
    {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    95
      val session_name = theory_context.session_context.session_name
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    96
      val theory_name = theory_context.theory
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
    97
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
    98
      theory_context.files0(permissive = true) match {
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
    99
        case Nil => None
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   100
        case files =>
75972
d574b55c4e83 read full sessions_requirements, for more complete entity hyperlinks;
wenzelm
parents: 75971
diff changeset
   101
          val theory_export = Export_Theory.read_theory(theory_context, permissive = true)
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   102
          val theory =
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   103
            Theory(theory_name,
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   104
              static_session = sessions_structure.theory_qualifier(theory_name),
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   105
              dynamic_session = session_name,
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   106
              files = files,
75972
d574b55c4e83 read full sessions_requirements, for more complete entity hyperlinks;
wenzelm
parents: 75971
diff changeset
   107
              entities = theory_export.entity_iterator.toList,
d574b55c4e83 read full sessions_requirements, for more complete entity hyperlinks;
wenzelm
parents: 75971
diff changeset
   108
              others = theory_export.others.keySet.toList)
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   109
          Some(theory)
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   110
      }
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   111
    }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   112
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   113
    def read_session(session_name: String): Document_Info.Session = {
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   114
      val static_theories = deps(session_name).used_theories.map(_._1.theory)
75971
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   115
      val (thys, build_uuid) = {
76656
a8f452f7c503 clarified names;
wenzelm
parents: 75972
diff changeset
   116
        using(database_context.open_session(deps.background(session_name))) { session_context =>
75971
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   117
          val thys =
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   118
            for {
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   119
              theory_name <- static_theories
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   120
              theory <- read_theory(session_context.theory(theory_name))
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   121
            } yield theory_name -> theory
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   122
          val build_uuid =
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   123
            (for {
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   124
              db <- session_context.session_db(session_name)
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   125
              build <- database_context.store.read_build(db, session_name)
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   126
            } yield build.uuid).getOrElse("")
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   127
          (thys, build_uuid)
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   128
        }
75971
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   129
      }
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   130
      val loaded_theories0 = thys.toMap
75910
529e4ac56904 more robust: theories could have been suppressed via option "condition";
wenzelm
parents: 75907
diff changeset
   131
      val used_theories = static_theories.filter(loaded_theories0.keySet)
75971
cec22f403c25 more accurate management of dependencies: change of build_uuid causes output of HTML, but already existing/current HTML is not produced again;
wenzelm
parents: 75941
diff changeset
   132
      Session(session_name, used_theories, loaded_theories0, build_uuid)
75907
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   133
    }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   134
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   135
    val result0 =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   136
      (for (session <- Par_List.map(read_session, sessions_requirements).iterator)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   137
        yield session.name -> session).toMap
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   138
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   139
    val result1 =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   140
      sessions_requirements.foldLeft(Map.empty[String, Session]) {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   141
        case (seen, session_name) =>
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   142
          val session0 = result0(session_name)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   143
          val loaded_theories1 =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   144
            sessions_structure(session_name).parent.map(seen) match {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   145
              case None => session0.loaded_theories
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   146
              case Some(parent_session) =>
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   147
                parent_session.loaded_theories ++ session0.loaded_theories
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   148
            }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   149
          val session1 = session0.copy(loaded_theories = loaded_theories1)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   150
          seen + (session_name -> session1)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   151
      }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   152
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   153
    new Document_Info(result1)
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   154
  }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   155
}
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   156
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   157
class Document_Info private(sessions: Map[String, Document_Info.Session]) {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   158
  override def toString: String =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   159
    sessions.keysIterator.toList.sorted.mkString("Document_Info(", ", ", ")")
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   160
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   161
  def the_session(session: String): Document_Info.Session =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   162
    sessions.getOrElse(session,
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   163
      error("Unknown document information for session: " + quote(session)))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   164
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   165
  def theory_by_name(session: String, theory: String): Option[Document_Info.Theory] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   166
    by_session_and_theory_name.get((session, theory))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   167
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   168
  def theory_by_file(session: String, file: String): Option[Document_Info.Theory] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   169
    by_session_and_theory_file.get((session, file))
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   170
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   171
  private lazy val by_session_and_theory_name: Map[(String, String), Document_Info.Theory] =
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   172
    (for {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   173
      session <- sessions.valuesIterator
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   174
      theory <- session.loaded_theories.valuesIterator
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   175
    } yield (session.name, theory.name) -> theory).toMap
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   176
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   177
  private lazy val by_session_and_theory_file: Map[(String, String), Document_Info.Theory] = {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   178
    (for {
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   179
      session <- sessions.valuesIterator
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   180
      theory <- session.loaded_theories.valuesIterator
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   181
      file <- theory.files.iterator
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   182
    } yield (session.name, file) -> theory).toMap
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   183
  }
091edca12219 clarified modules;
wenzelm
parents:
diff changeset
   184
}