| author | wenzelm | 
| Sat, 08 Jul 2023 19:28:26 +0200 | |
| changeset 78271 | c0dc000d2a40 | 
| parent 77301 | c6d724692603 | 
| child 78592 | fdfe9b91d96e | 
| permissions | -rw-r--r-- | 
| 36676 | 1 | /* Title: Pure/PIDE/document.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 4 | Document as collection of named nodes, each consisting of an editable | 
| 38418 
9a7af64d71bb
more explicit / functional ML version of document model;
 wenzelm parents: 
38417diff
changeset | 5 | list of commands, associated with asynchronous execution process. | 
| 36676 | 6 | */ | 
| 34407 | 7 | |
| 34871 
e596a0b71f3c
incorporate "proofdocument" part into main Isabelle/Pure.jar -- except for html_panel.scala, which depends on external library (Lobo/Cobra browser);
 wenzelm parents: 
34868diff
changeset | 8 | package isabelle | 
| 34318 
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
 wenzelm parents: diff
changeset | 9 | |
| 34760 | 10 | |
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 11 | import scala.collection.mutable | 
| 37073 | 12 | |
| 13 | ||
| 75393 | 14 | object Document {
 | 
| 38424 | 15 | /** document structure **/ | 
| 16 | ||
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 17 | /* overlays -- print functions with arguments */ | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 18 | |
| 75393 | 19 |   object Overlays {
 | 
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 20 | val empty = new Overlays(Map.empty) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 21 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 22 | |
| 75393 | 23 |   final class Overlays private(rep: Map[Node.Name, Node.Overlays]) {
 | 
| 64799 | 24 | def apply(name: Node.Name): Node.Overlays = | 
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 25 | rep.getOrElse(name, Node.Overlays.empty) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 26 | |
| 75393 | 27 |     private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays = {
 | 
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 28 | val node_overlays = f(apply(name)) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 29 | new Overlays(if (node_overlays.is_empty) rep - name else rep + (name -> node_overlays)) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 30 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 31 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 32 | def insert(command: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 33 | update(command.node_name, _.insert(command, fn, args)) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 34 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 35 | def remove(command: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 36 | update(command.node_name, _.remove(command, fn, args)) | 
| 55800 | 37 | |
| 38 |     override def toString: String = rep.mkString("Overlays(", ",", ")")
 | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 39 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 40 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 41 | |
| 55783 | 42 | /* document blobs: auxiliary files */ | 
| 43 | ||
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 44 |   object Blobs {
 | 
| 76916 | 45 | sealed case class Item( | 
| 46 | bytes: Bytes, | |
| 47 | source: String, | |
| 48 | chunk: Symbol.Text_Chunk, | |
| 49 | changed: Boolean | |
| 50 |     ) {
 | |
| 76924 | 51 | def source_wellformed: Boolean = bytes.wellformed_text.nonEmpty | 
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 52 | def unchanged: Item = copy(changed = false) | 
| 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 53 | } | 
| 55783 | 54 | |
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 55 | def apply(blobs: Map[Node.Name, Item]): Blobs = new Blobs(blobs) | 
| 55783 | 56 | val empty: Blobs = apply(Map.empty) | 
| 76913 | 57 | |
| 58 | def make(blobs: List[(Command.Blob, Item)]): Blobs = | |
| 59 | if (blobs.isEmpty) empty | |
| 60 | else apply((for ((a, b) <- blobs.iterator) yield a.name -> b).toMap) | |
| 55783 | 61 | } | 
| 62 | ||
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 63 |   final class Blobs private(blobs: Map[Node.Name, Blobs.Item]) {
 | 
| 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 64 | def get(name: Node.Name): Option[Blobs.Item] = blobs.get(name) | 
| 55783 | 65 | |
| 66 | def changed(name: Node.Name): Boolean = | |
| 67 |       get(name) match {
 | |
| 68 | case Some(blob) => blob.changed | |
| 69 | case None => false | |
| 70 | } | |
| 71 | ||
| 55800 | 72 |     override def toString: String = blobs.mkString("Blobs(", ",", ")")
 | 
| 55783 | 73 | } | 
| 74 | ||
| 75 | ||
| 76 | /* document nodes: theories and auxiliary files */ | |
| 38424 | 77 | |
| 44615 | 78 | type Edit[A, B] = (Node.Name, Node.Edit[A, B]) | 
| 44384 | 79 | type Edit_Text = Edit[Text.Edit, Text.Perspective] | 
| 52849 | 80 | type Edit_Command = Edit[Command.Edit, Command.Perspective] | 
| 38151 | 81 | |
| 75393 | 82 |   object Node {
 | 
| 52887 | 83 | /* header and name */ | 
| 84 | ||
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 85 | sealed case class Header( | 
| 70638 
f164cec7ac22
clarified signature: prefer operations without position;
 wenzelm parents: 
70636diff
changeset | 86 | imports_pos: List[(Name, Position.T)] = Nil, | 
| 63020 | 87 | keywords: Thy_Header.Keywords = Nil, | 
| 63579 | 88 | abbrevs: Thy_Header.Abbrevs = Nil, | 
| 75393 | 89 | errors: List[String] = Nil | 
| 90 |     ) {
 | |
| 70638 
f164cec7ac22
clarified signature: prefer operations without position;
 wenzelm parents: 
70636diff
changeset | 91 | def imports: List[Name] = imports_pos.map(_._1) | 
| 
f164cec7ac22
clarified signature: prefer operations without position;
 wenzelm parents: 
70636diff
changeset | 92 | |
| 66773 | 93 | def append_errors(msgs: List[String]): Header = | 
| 94 | copy(errors = errors ::: msgs) | |
| 54549 
2a3053472ec3
actually expose errors of cumulative theory dependencies;
 wenzelm parents: 
54530diff
changeset | 95 | |
| 
2a3053472ec3
actually expose errors of cumulative theory dependencies;
 wenzelm parents: 
54530diff
changeset | 96 | def cat_errors(msg2: String): Header = | 
| 62492 | 97 | copy(errors = errors.map(msg1 => Exn.cat_message(msg1, msg2))) | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 98 | } | 
| 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 99 | |
| 71601 | 100 | val no_header: Header = Header() | 
| 63020 | 101 | def bad_header(msg: String): Header = Header(errors = List(msg)) | 
| 46737 | 102 | |
| 75393 | 103 |     object Name {
 | 
| 76860 
f95ed5a0600c
clarified signature: uniform master_dir instead of separate field;
 wenzelm parents: 
76848diff
changeset | 104 | def apply(node: String, theory: String = ""): Name = new Name(node, theory) | 
| 76671 
254964ca1b98
clarified signature: avoid case class with redefined equality;
 wenzelm parents: 
76481diff
changeset | 105 | |
| 76905 | 106 | def loaded_theory(theory: String): Name = Name(theory, theory = theory) | 
| 46723 | 107 | |
| 76848 | 108 |       val empty: Name = Name("")
 | 
| 76845 | 109 | |
| 75393 | 110 |       object Ordering extends scala.math.Ordering[Name] {
 | 
| 46723 | 111 | def compare(name1: Name, name2: Name): Int = name1.node compare name2.node | 
| 112 | } | |
| 70636 | 113 | |
| 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: 
70650diff
changeset | 114 | type Graph[A] = isabelle.Graph[Node.Name, A] | 
| 
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: 
70650diff
changeset | 115 | |
| 70692 | 116 | def make_graph[A](entries: List[((Name, A), List[Name])]): Graph[A] = | 
| 76840 | 117 | Graph.make(entries, converse = true)(Ordering) | 
| 44957 
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
 wenzelm parents: 
44676diff
changeset | 118 | } | 
| 52887 | 119 | |
| 76860 
f95ed5a0600c
clarified signature: uniform master_dir instead of separate field;
 wenzelm parents: 
76848diff
changeset | 120 |     final class Name private(val node: String, val theory: String) {
 | 
| 44615 | 121 | override def hashCode: Int = node.hashCode | 
| 122 | override def equals(that: Any): Boolean = | |
| 123 |         that match {
 | |
| 124 | case other: Name => node == other.node | |
| 125 | case _ => false | |
| 126 | } | |
| 54509 
1f77110c94ef
maintain document model for all files, with document view for theory only, and special blob for non-theory files;
 wenzelm parents: 
54462diff
changeset | 127 | |
| 76829 
f2a8ba0b8c96
more robust: avoid detour via somewhat fragile Node.Name.path;
 wenzelm parents: 
76792diff
changeset | 128 |       def file_name: String = Url.get_base_name(node).getOrElse("")
 | 
| 
f2a8ba0b8c96
more robust: avoid detour via somewhat fragile Node.Name.path;
 wenzelm parents: 
76792diff
changeset | 129 | |
| 66849 | 130 | def path: Path = Path.explode(File.standard_path(node)) | 
| 76860 
f95ed5a0600c
clarified signature: uniform master_dir instead of separate field;
 wenzelm parents: 
76848diff
changeset | 131 | |
| 
f95ed5a0600c
clarified signature: uniform master_dir instead of separate field;
 wenzelm parents: 
76848diff
changeset | 132 |       def master_dir: String = Url.strip_base_name(node).getOrElse("")
 | 
| 66716 | 133 | |
| 59319 | 134 | def is_theory: Boolean = theory.nonEmpty | 
| 65409 | 135 | |
| 65439 | 136 | def theory_base_name: String = Long_Name.base_name(theory) | 
| 137 | ||
| 54510 | 138 | override def toString: String = if (is_theory) theory else node | 
| 56801 
8dd9df88f647
some support for session-qualified theories: allow to refer to resources via qualified name instead of odd file-system path;
 wenzelm parents: 
56746diff
changeset | 139 | |
| 67943 | 140 | def json: JSON.Object.T = | 
| 141 |         JSON.Object("node_name" -> node, "theory_name" -> theory)
 | |
| 44615 | 142 | } | 
| 143 | ||
| 75393 | 144 |     sealed case class Entry(name: Node.Name, header: Node.Header) {
 | 
| 66714 | 145 | override def toString: String = name.toString | 
| 146 | } | |
| 147 | ||
| 52887 | 148 | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 149 | /* node overlays */ | 
| 52887 | 150 | |
| 75393 | 151 |     object Overlays {
 | 
| 52976 | 152 | val empty = new Overlays(Multi_Map.empty) | 
| 52887 | 153 | } | 
| 154 | ||
| 75393 | 155 |     final class Overlays private(rep: Multi_Map[Command, (String, List[String])]) {
 | 
| 52887 | 156 | def commands: Set[Command] = rep.keySet | 
| 157 | def is_empty: Boolean = rep.isEmpty | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 158 | def dest: List[(Command, (String, List[String]))] = rep.iterator.toList | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 159 | def insert(cmd: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 160 | new Overlays(rep.insert(cmd, (fn, args))) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 161 | def remove(cmd: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 162 | new Overlays(rep.remove(cmd, (fn, args))) | 
| 55800 | 163 | |
| 164 |       override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
 | |
| 52887 | 165 | } | 
| 166 | ||
| 167 | ||
| 168 | /* edits */ | |
| 169 | ||
| 75393 | 170 |     sealed abstract class Edit[A, B] {
 | 
| 171 |       def foreach(f: A => Unit): Unit = {
 | |
| 44156 | 172 |         this match {
 | 
| 44383 | 173 | case Edits(es) => es.foreach(f) | 
| 174 | case _ => | |
| 44156 | 175 | } | 
| 44383 | 176 | } | 
| 57621 | 177 | |
| 178 | def is_void: Boolean = | |
| 179 |         this match {
 | |
| 180 | case Edits(Nil) => true | |
| 181 | case _ => false | |
| 182 | } | |
| 44156 | 183 | } | 
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 184 | case class Blob[A, B](blob: Blobs.Item) extends Edit[A, B] | 
| 54562 
301a721af68b
clarified node edits sent to prover -- Clear/Blob only required for text edits within editor;
 wenzelm parents: 
54549diff
changeset | 185 | |
| 44384 | 186 | case class Edits[A, B](edits: List[A]) extends Edit[A, B] | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 187 | case class Deps[A, B](header: Header) extends Edit[A, B] | 
| 52849 | 188 | case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B] | 
| 57610 | 189 | |
| 190 | ||
| 191 | /* perspective */ | |
| 192 | ||
| 76702 | 193 |     object Perspective_Text {
 | 
| 194 | type T = Perspective[Text.Edit, Text.Perspective] | |
| 195 | val empty: T = Perspective(false, Text.Perspective.empty, Overlays.empty) | |
| 196 | def is_empty(perspective: T): Boolean = | |
| 197 | !perspective.required && | |
| 198 | perspective.visible.is_empty && | |
| 199 | perspective.overlays.is_empty | |
| 200 | } | |
| 57614 | 201 | |
| 76702 | 202 |     object Perspective_Command {
 | 
| 203 | type T = Perspective[Command.Edit, Command.Perspective] | |
| 204 | val empty: T = Perspective(false, Command.Perspective.empty, Overlays.empty) | |
| 205 | def is_empty(perspective: T): Boolean = | |
| 206 | !perspective.required && | |
| 207 | perspective.visible.is_empty && | |
| 208 | perspective.overlays.is_empty | |
| 209 | } | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 210 | |
| 52887 | 211 | |
| 212 | /* commands */ | |
| 213 | ||
| 75393 | 214 |     object Commands {
 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 215 | def apply(commands: Linear_Set[Command]): Commands = new Commands(commands) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 216 | val empty: Commands = apply(Linear_Set.empty) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 217 | |
| 75393 | 218 | def starts( | 
| 219 | commands: Iterator[Command], | |
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 220 | init: Int = 0, | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 221 | count: Command => Int = _.length | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 222 |       ) : Iterator[(Command, Int)] = {
 | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 223 | var i = init | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 224 |         for (command <- commands) yield {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 225 | val start = i | 
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 226 | i += count(command) | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 227 | (command, start) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 228 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 229 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 230 | |
| 75393 | 231 | def starts_pos( | 
| 232 | commands: Iterator[Command], | |
| 233 | pos: Token.Pos = Token.Pos.start | |
| 234 |       ) : Iterator[(Command, Token.Pos)] = {
 | |
| 67895 | 235 | var p = pos | 
| 236 |         for (command <- commands) yield {
 | |
| 237 | val start = p | |
| 73359 | 238 | p = command.span.content.foldLeft(p)(_.advance(_)) | 
| 67895 | 239 | (command, start) | 
| 240 | } | |
| 241 | } | |
| 242 | ||
| 56398 
15d0821c8667
afford larger full_index, to save a few milliseconds during rendering (notably text_overview);
 wenzelm parents: 
56394diff
changeset | 243 | private val block_size = 256 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 244 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 245 | |
| 75393 | 246 |     final class Commands private(val commands: Linear_Set[Command]) {
 | 
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 247 | lazy val start_lines: Map[Document_ID.Command, Int] = | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 248 |         (for {
 | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 249 | (command, line) <- | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 250 | Node.Commands.starts(commands.iterator, init = 1, | 
| 77007 | 251 | count = cmd => Library.count_newlines(cmd.source)) | 
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 252 | } yield command.id -> line).toMap | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 253 | |
| 56314 | 254 | lazy val load_commands: List[Command] = | 
| 59319 | 255 | commands.iterator.filter(cmd => cmd.blobs.nonEmpty).toList | 
| 54462 | 256 | |
| 75393 | 257 |       private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) = {
 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 258 | val blocks = new mutable.ListBuffer[(Command, Text.Offset)] | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 259 | var next_block = 0 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 260 | var last_stop = 0 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 261 |         for ((command, start) <- Commands.starts(commands.iterator)) {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 262 | last_stop = start + command.length | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 263 |           while (last_stop + 1 > next_block) {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 264 | blocks += (command -> start) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 265 | next_block += Commands.block_size | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 266 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 267 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 268 | (blocks.toArray, Text.Range(0, last_stop)) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 269 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 270 | |
| 66043 | 271 | private def full_range: Text.Range = full_index._2 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 272 | |
| 75393 | 273 |       def iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] = {
 | 
| 59319 | 274 |         if (commands.nonEmpty && full_range.contains(i)) {
 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 275 | val (cmd0, start0) = full_index._1(i / Commands.block_size) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 276 |           Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 277 | case (cmd, start) => start + cmd.length <= i } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 278 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 279 | else Iterator.empty | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 280 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 281 | } | 
| 57614 | 282 | |
| 283 | val empty: Node = new Node() | |
| 76903 | 284 | |
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 285 | def init_blob(blob: Blobs.Item): Node = | 
| 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 286 | new Node(get_blob = Some(blob.unchanged)) | 
| 38151 | 287 | } | 
| 288 | ||
| 46712 | 289 | final class Node private( | 
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 290 | val get_blob: Option[Blobs.Item] = None, | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 291 | val header: Node.Header = Node.no_header, | 
| 63584 
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
 wenzelm parents: 
63579diff
changeset | 292 | val syntax: Option[Outer_Syntax] = None, | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 293 | val text_perspective: Text.Perspective = Text.Perspective.empty, | 
| 76702 | 294 | val perspective: Node.Perspective_Command.T = Node.Perspective_Command.empty, | 
| 75393 | 295 | _commands: Node.Commands = Node.Commands.empty | 
| 296 |   ) {
 | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 297 | def is_empty: Boolean = | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 298 | get_blob.isEmpty && | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 299 | header == Node.no_header && | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 300 | text_perspective.is_empty && | 
| 76702 | 301 | Node.Perspective_Command.is_empty(perspective) && | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 302 | commands.isEmpty | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 303 | |
| 59702 
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
 wenzelm parents: 
59695diff
changeset | 304 | def has_header: Boolean = header != Node.no_header | 
| 
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
 wenzelm parents: 
59695diff
changeset | 305 | |
| 69556 | 306 | override def toString: String = | 
| 307 | if (is_empty) "empty" | |
| 308 | else if (get_blob.isDefined) "blob" | |
| 309 | else "node" | |
| 310 | ||
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 311 | def commands: Linear_Set[Command] = _commands.commands | 
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 312 | def command_start_line(command: Command): Option[Int] = | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 313 | _commands.start_lines.get(command.id) | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 314 | def load_commands: List[Command] = _commands.load_commands | 
| 64799 | 315 | def load_commands_changed(doc_blobs: Blobs): Boolean = | 
| 316 | load_commands.exists(_.blobs_changed(doc_blobs)) | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 317 | |
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 318 | def update_header(new_header: Node.Header): Node = | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 319 | new Node(get_blob, new_header, syntax, text_perspective, perspective, _commands) | 
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 320 | |
| 63584 
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
 wenzelm parents: 
63579diff
changeset | 321 | def update_syntax(new_syntax: Option[Outer_Syntax]): Node = | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 322 | new Node(get_blob, header, new_syntax, text_perspective, perspective, _commands) | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 323 | |
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 324 | def update_perspective( | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 325 | new_text_perspective: Text.Perspective, | 
| 76702 | 326 | new_perspective: Node.Perspective_Command.T): Node = | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 327 | new Node(get_blob, header, syntax, new_text_perspective, new_perspective, _commands) | 
| 46680 | 328 | |
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 329 | def edit_perspective: Node.Edit[Text.Edit, Text.Perspective] = | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 330 | Node.Perspective(perspective.required, text_perspective, perspective.overlays) | 
| 46680 | 331 | |
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 332 | def same_perspective( | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 333 | other_text_perspective: Text.Perspective, | 
| 76702 | 334 | other_perspective: Node.Perspective_Command.T): Boolean = | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 335 | text_perspective == other_text_perspective && | 
| 52849 | 336 | perspective.required == other_perspective.required && | 
| 337 | perspective.visible.same(other_perspective.visible) && | |
| 338 | perspective.overlays == other_perspective.overlays | |
| 52808 
143f225e50f5
allow explicit indication of required node: full eval, no prints;
 wenzelm parents: 
52568diff
changeset | 339 | |
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 340 | def update_commands(new_commands: Linear_Set[Command]): Node = | 
| 52918 
038458a4d11b
proper low-level comparison -- heed warning by Scala compiler;
 wenzelm parents: 
52901diff
changeset | 341 | if (new_commands eq _commands.commands) this | 
| 76759 | 342 |       else {
 | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 343 | new Node(get_blob, header, syntax, text_perspective, perspective, | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 344 | Node.Commands(new_commands)) | 
| 76759 | 345 | } | 
| 38151 | 346 | |
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 347 | def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] = | 
| 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 348 | _commands.iterator(i) | 
| 38151 | 349 | |
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 350 | def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] = | 
| 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 351 |       command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
 | 
| 38151 | 352 | |
| 38879 
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
 wenzelm parents: 
38872diff
changeset | 353 | def command_start(cmd: Command): Option[Text.Offset] = | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 354 | Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2) | 
| 67247 | 355 | |
| 76924 | 356 | lazy val source_wellformed: Boolean = | 
| 76916 | 357 |       get_blob match {
 | 
| 76924 | 358 | case Some(blob) => blob.source_wellformed | 
| 76916 | 359 | case None => true | 
| 360 | } | |
| 361 | ||
| 76204 
b80b2fbc46c3
clarified signature: persistent Node.source / Snapshot.source;
 wenzelm parents: 
75914diff
changeset | 362 | lazy val source: String = | 
| 67247 | 363 |       get_blob match {
 | 
| 67265 
f32287c95432
store full blob source for the sake of markup_to_XML;
 wenzelm parents: 
67264diff
changeset | 364 | case Some(blob) => blob.source | 
| 75819 | 365 |         case None => command_iterator().map({ case (cmd, _) => cmd.source }).mkString
 | 
| 67247 | 366 | } | 
| 38151 | 367 | } | 
| 368 | ||
| 369 | ||
| 46723 | 370 | /* development graph */ | 
| 371 | ||
| 75393 | 372 |   object Nodes {
 | 
| 46723 | 373 | val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering)) | 
| 77299 | 374 | |
| 375 | private def init(graph: Graph[Node.Name, Node], name: Node.Name): Graph[Node.Name, Node] = | |
| 376 | graph.default_node(name, Node.empty) | |
| 46723 | 377 | } | 
| 378 | ||
| 75393 | 379 |   final class Nodes private(graph: Graph[Node.Name, Node]) {
 | 
| 77299 | 380 | def apply(name: Node.Name): Node = Nodes.init(graph, name).get_node(name) | 
| 46723 | 381 | |
| 75393 | 382 |     def is_suppressed(name: Node.Name): Boolean = {
 | 
| 77299 | 383 | val graph1 = Nodes.init(graph, name) | 
| 57619 | 384 | graph1.is_maximal(name) && graph1.get_node(name).is_empty | 
| 385 | } | |
| 57617 | 386 | |
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 387 | def purge_suppressed: Option[Nodes] = | 
| 71601 | 388 |       graph.keys_iterator.filter(is_suppressed).toList match {
 | 
| 67112 | 389 | case Nil => None | 
| 73359 | 390 | case del => Some(new Nodes(del.foldLeft(graph)(_.del_node(_)))) | 
| 67112 | 391 | } | 
| 67110 
3156faac30a7
purge hidden nodes more thoroughly: is_hidden may become true only later;
 wenzelm parents: 
67014diff
changeset | 392 | |
| 75393 | 393 |     def + (entry: (Node.Name, Node)): Nodes = {
 | 
| 46723 | 394 | val (name, node) = entry | 
| 70638 
f164cec7ac22
clarified signature: prefer operations without position;
 wenzelm parents: 
70636diff
changeset | 395 | val imports = node.header.imports | 
| 77299 | 396 | val graph1 = (name :: imports).foldLeft(graph)(Nodes.init) | 
| 73359 | 397 | val graph2 = | 
| 398 |         graph1.imm_preds(name).foldLeft(graph1) { case (g, dep) => g.del_edge(dep, name) }
 | |
| 399 |       val graph3 = imports.foldLeft(graph2) { case (g, dep) => g.add_edge(dep, name) }
 | |
| 67110 
3156faac30a7
purge hidden nodes more thoroughly: is_hidden may become true only later;
 wenzelm parents: 
67014diff
changeset | 400 | new Nodes(graph3.map_node(name, _ => node)) | 
| 46723 | 401 | } | 
| 402 | ||
| 68321 | 403 | def domain: Set[Node.Name] = graph.domain | 
| 404 | ||
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 405 | def iterator: Iterator[(Node.Name, Node)] = | 
| 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 406 |       graph.iterator.map({ case (name, (node, _)) => (name, node) })
 | 
| 46723 | 407 | |
| 70539 
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
 wenzelm parents: 
70284diff
changeset | 408 | def theory_name(theory: String): Option[Node.Name] = | 
| 
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
 wenzelm parents: 
70284diff
changeset | 409 | graph.keys_iterator.find(name => name.theory == theory) | 
| 
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
 wenzelm parents: 
70284diff
changeset | 410 | |
| 64799 | 411 | def commands_loading(file_name: Node.Name): List[Command] = | 
| 54528 | 412 |       (for {
 | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 413 | (_, node) <- iterator | 
| 56314 | 414 | cmd <- node.load_commands.iterator | 
| 54530 
2c1440f70028
ranges of thy_load commands count as visible within perspective;
 wenzelm parents: 
54528diff
changeset | 415 | name <- cmd.blobs_names.iterator | 
| 54528 | 416 | if name == file_name | 
| 417 | } yield cmd).toList | |
| 418 | ||
| 77301 | 419 | def descendants(names: List[Node.Name]): List[Node.Name] = | 
| 420 | names.foldLeft(graph)(Nodes.init).all_succs(names) | |
| 46723 | 421 | def topological_order: List[Node.Name] = graph.topological_order | 
| 56337 | 422 | |
| 423 |     override def toString: String = topological_order.mkString("Nodes(", ",", ")")
 | |
| 46723 | 424 | } | 
| 425 | ||
| 426 | ||
| 38424 | 427 | |
| 428 | /** versioning **/ | |
| 429 | ||
| 430 | /* particular document versions */ | |
| 34485 | 431 | |
| 75393 | 432 |   object Version {
 | 
| 46681 | 433 | val init: Version = new Version() | 
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 434 | def make(nodes: Nodes): Version = new Version(Document_ID.make(), nodes) | 
| 67112 | 435 | |
| 75393 | 436 | def purge_future( | 
| 437 | versions: Map[Document_ID.Version, Version], | |
| 438 | future: Future[Version] | |
| 439 |     ) : Future[Version] = {
 | |
| 68066 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 440 |       if (future.is_finished) {
 | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 441 | val version = future.join | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 442 |         versions.get(version.id) match {
 | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 443 | case Some(version1) if !(version eq version1) => Future.value(version1) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 444 | case _ => future | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 445 | } | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 446 | } | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 447 | else future | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 448 | } | 
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 449 | |
| 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 450 | def purge_suppressed( | 
| 75393 | 451 | versions: Map[Document_ID.Version, Version] | 
| 452 |     ): Map[Document_ID.Version, Version] = {
 | |
| 73359 | 453 | (for ((id, v) <- versions.iterator; v1 <- v.purge_suppressed) yield (id, v1)). | 
| 454 | foldLeft(versions)(_ + _) | |
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 455 | } | 
| 38417 | 456 | } | 
| 457 | ||
| 46712 | 458 | final class Version private( | 
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 459 | val id: Document_ID.Version = Document_ID.none, | 
| 75393 | 460 | val nodes: Nodes = Nodes.empty | 
| 461 |   ) {
 | |
| 55777 | 462 |     override def toString: String = "Version(" + id + ")"
 | 
| 67110 
3156faac30a7
purge hidden nodes more thoroughly: is_hidden may become true only later;
 wenzelm parents: 
67014diff
changeset | 463 | |
| 68299 
0b5a23477911
clarified signature -- avoid confusion with Resources.is_hidden;
 wenzelm parents: 
68114diff
changeset | 464 | def purge_suppressed: Option[Version] = | 
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 465 | nodes.purge_suppressed.map(new Version(id, _)) | 
| 46941 | 466 | } | 
| 34660 | 467 | |
| 34859 | 468 | |
| 38424 | 469 | /* changes of plain text, eventually resulting in document edits */ | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 470 | |
| 75393 | 471 |   object Change {
 | 
| 46678 | 472 | val init: Change = new Change() | 
| 473 | ||
| 474 | def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change = | |
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 475 | new Change(Some(previous), edits.reverse, version) | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 476 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 477 | |
| 46712 | 478 | final class Change private( | 
| 46677 | 479 | val previous: Option[Future[Version]] = Some(Future.value(Version.init)), | 
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 480 | val rev_edits: List[Edit_Text] = Nil, | 
| 75393 | 481 | val version: Future[Version] = Future.value(Version.init) | 
| 482 |   ) {
 | |
| 44672 | 483 | def is_finished: Boolean = | 
| 484 |       (previous match { case None => true case Some(future) => future.is_finished }) &&
 | |
| 485 | version.is_finished | |
| 486 | ||
| 46678 | 487 | def truncate: Change = new Change(None, Nil, version) | 
| 68066 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 488 | |
| 75393 | 489 |     def purge(versions: Map[Document_ID.Version, Version]): Option[Change] = {
 | 
| 68066 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 490 | val previous1 = previous.map(Version.purge_future(versions, _)) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 491 | val version1 = Version.purge_future(versions, version) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 492 | if ((previous eq previous1) && (version eq version1)) None | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 493 | else Some(new Change(previous1, rev_edits, version1)) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 494 | } | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 495 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 496 | |
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 497 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 498 | /* history navigation */ | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 499 | |
| 75393 | 500 |   object History {
 | 
| 46679 | 501 | val init: History = new History() | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 502 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 503 | |
| 46712 | 504 | final class History private( | 
| 75393 | 505 | val undo_list: List[Change] = List(Change.init) // non-empty list | 
| 506 |   ) {
 | |
| 67114 | 507 |     override def toString: String = "History(" + undo_list.length + ")"
 | 
| 508 | ||
| 46679 | 509 | def tip: Change = undo_list.head | 
| 510 | def + (change: Change): History = new History(change :: undo_list) | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 511 | |
| 75393 | 512 |     def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] = {
 | 
| 46679 | 513 | val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1 | 
| 514 | val (retained, dropped) = undo_list.splitAt(n max retain) | |
| 515 | ||
| 516 |       retained.splitAt(retained.length - 1) match {
 | |
| 517 | case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate))) | |
| 518 | case _ => None | |
| 519 | } | |
| 520 | } | |
| 68066 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 521 | |
| 75393 | 522 |     def purge(versions: Map[Document_ID.Version, Version]): History = {
 | 
| 68066 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 523 | val undo_list1 = undo_list.map(_.purge(versions)) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 524 | if (undo_list1.forall(_.isEmpty)) this | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 525 | else new History(for ((a, b) <- undo_list1 zip undo_list) yield a.getOrElse(b)) | 
| 
63f03ee4057e
purge history more thoroughly (see also 3156faac30a7);
 wenzelm parents: 
67943diff
changeset | 526 | } | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 527 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 528 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 529 | |
| 72818 | 530 | /* snapshot: persistent user-view of document state */ | 
| 38424 | 531 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 532 |   object Pending_Edits {
 | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 533 | val empty: Pending_Edits = make(Nil) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 534 | |
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 535 | def make(models: Iterable[Model]): Pending_Edits = | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 536 | new Pending_Edits( | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 537 |         (for {
 | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 538 | model <- models.iterator | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 539 | edits = model.pending_edits if edits.nonEmpty | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 540 | } yield model.node_name -> edits).toMap) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 541 | } | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 542 | |
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 543 |   final class Pending_Edits(pending_edits: Map[Node.Name, List[Text.Edit]]) {
 | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 544 | def is_stable: Boolean = pending_edits.isEmpty | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 545 | |
| 76905 | 546 |     def + (entry: (Node.Name, List[Text.Edit])): Pending_Edits = {
 | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 547 | val (name, es) = entry | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 548 | if (es.isEmpty) this | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 549 | else new Pending_Edits(pending_edits + (name -> (es ::: edits(name)))) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 550 | } | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 551 | |
| 76905 | 552 | def edits(name: Node.Name): List[Text.Edit] = | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 553 | pending_edits.getOrElse(name, Nil) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 554 | |
| 76905 | 555 | def reverse_edits(name: Node.Name): List[Text.Edit] = | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 556 | reverse_pending_edits.getOrElse(name, Nil) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 557 | |
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 558 | private lazy val reverse_pending_edits = | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 559 | (for ((name, es) <- pending_edits.iterator) yield (name, es.reverse)).toMap | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 560 | } | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 561 | |
| 75393 | 562 |   object Snapshot {
 | 
| 71601 | 563 | val init: Snapshot = State.init.snapshot() | 
| 52972 | 564 | } | 
| 565 | ||
| 72821 | 566 | class Snapshot private[Document]( | 
| 72818 | 567 | val state: State, | 
| 568 | val version: Version, | |
| 569 | val node_name: Node.Name, | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 570 | pending_edits: Pending_Edits, | 
| 75393 | 571 | val snippet_command: Option[Command] | 
| 572 |   ) {
 | |
| 72821 | 573 | override def toString: String = | 
| 574 | "Snapshot(node = " + node_name.node + ", version = " + version.id + | |
| 575 | (if (is_outdated) ", outdated" else "") + ")" | |
| 576 | ||
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 577 | def switch(name: Node.Name): Snapshot = | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 578 | if (name == node_name) this | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 579 | else new Snapshot(state, version, name, pending_edits, None) | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 580 | |
| 72821 | 581 | |
| 72822 | 582 | /* nodes */ | 
| 583 | ||
| 72823 | 584 | def get_node(name: Node.Name): Node = version.nodes(name) | 
| 585 | ||
| 586 | val node: Node = get_node(node_name) | |
| 72822 | 587 | |
| 588 | def node_files: List[Node.Name] = | |
| 72829 
a28a4105883f
avoid duplicate entries: snippet_command is defined within node;
 wenzelm parents: 
72823diff
changeset | 589 | node_name :: node.load_commands.flatMap(_.blobs_names) | 
| 72822 | 590 | |
| 77161 
913c781ff6ba
support document preparation from already loaded theories;
 wenzelm parents: 
77147diff
changeset | 591 | def node_consolidated(name: Node.Name): Boolean = | 
| 77147 | 592 | state.node_consolidated(version, name) | 
| 593 | ||
| 77197 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 594 | def theory_consolidated(theory: String): Boolean = | 
| 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 595 |       version.nodes.theory_name(theory) match {
 | 
| 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 596 | case Some(name) => node_consolidated(name) | 
| 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 597 | case None => false | 
| 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 598 | } | 
| 
a541da01ba67
clarified signature selection: SortedSet[String], which fits better to stored json and works properly on Windows (NB: document theories have an authentic session-theory name);
 wenzelm parents: 
77161diff
changeset | 599 | |
| 72822 | 600 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 601 | /* pending edits */ | 
| 72820 | 602 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 603 | def is_outdated: Boolean = !pending_edits.is_stable | 
| 72820 | 604 | |
| 605 | def convert(offset: Text.Offset): Text.Offset = | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 606 |       pending_edits.edits(node_name).foldLeft(offset) { case (i, edit) => edit.convert(i) }
 | 
| 72820 | 607 | def revert(offset: Text.Offset): Text.Offset = | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 608 |       pending_edits.reverse_edits(node_name).foldLeft(offset) { case (i, edit) => edit.revert(i) }
 | 
| 72820 | 609 | |
| 610 | def convert(range: Text.Range): Text.Range = range.map(convert) | |
| 611 | def revert(range: Text.Range): Text.Range = range.map(revert) | |
| 612 | ||
| 52972 | 613 | |
| 72821 | 614 | /* theory load commands */ | 
| 615 | ||
| 616 | val commands_loading: List[Command] = | |
| 617 | if (node_name.is_theory) Nil | |
| 618 | else version.nodes.commands_loading(node_name) | |
| 619 | ||
| 620 | def commands_loading_ranges(pred: Node.Name => Boolean): List[Text.Range] = | |
| 621 |       (for {
 | |
| 622 | cmd <- node.load_commands.iterator | |
| 623 | blob_name <- cmd.blobs_names.iterator | |
| 624 | if pred(blob_name) | |
| 625 | start <- node.command_start(cmd) | |
| 626 | } yield convert(cmd.core_range + start)).toList | |
| 627 | ||
| 628 | ||
| 629 | /* command as add-on snippet */ | |
| 630 | ||
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 631 |     def snippet(command: Command, doc_blobs: Blobs): Snapshot = {
 | 
| 72718 | 632 | val node_name = command.node_name | 
| 633 | ||
| 76915 
e5f67cfedecd
proper Node.init_blobs, not just edits (amending ca872f20cf5b);
 wenzelm parents: 
76913diff
changeset | 634 | val blobs = for (a <- command.blobs_names; b <- doc_blobs.get(a)) yield a -> b | 
| 
e5f67cfedecd
proper Node.init_blobs, not just edits (amending ca872f20cf5b);
 wenzelm parents: 
76913diff
changeset | 635 | |
| 72718 | 636 | val nodes0 = version.nodes | 
| 637 | val nodes1 = nodes0 + (node_name -> nodes0(node_name).update_commands(Linear_Set(command))) | |
| 76915 
e5f67cfedecd
proper Node.init_blobs, not just edits (amending ca872f20cf5b);
 wenzelm parents: 
76913diff
changeset | 638 |       val nodes2 = blobs.foldLeft(nodes1) { case (ns, (a, b)) => ns + (a -> Node.init_blob(b)) }
 | 
| 
e5f67cfedecd
proper Node.init_blobs, not just edits (amending ca872f20cf5b);
 wenzelm parents: 
76913diff
changeset | 639 | val version1 = Version.make(nodes2) | 
| 72718 | 640 | |
| 641 | val edits: List[Edit_Text] = | |
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 642 | List(node_name -> Node.Edits(List(Text.Edit.insert(0, command.source)))) ::: | 
| 76915 
e5f67cfedecd
proper Node.init_blobs, not just edits (amending ca872f20cf5b);
 wenzelm parents: 
76913diff
changeset | 643 |         blobs.map({ case (a, b) => a -> Node.Blob(b) })
 | 
| 72718 | 644 | |
| 645 | val state0 = state.define_command(command) | |
| 646 | val state1 = | |
| 647 | state0.continue_history(Future.value(version), edits, Future.value(version1)) | |
| 648 | .define_version(version1, state0.the_assignment(version)) | |
| 649 | .assign(version1.id, Nil, List(command.id -> List(Document_ID.make())))._2 | |
| 650 | ||
| 72818 | 651 | state1.snapshot(node_name = node_name, snippet_command = Some(command)) | 
| 72718 | 652 | } | 
| 653 | ||
| 72821 | 654 | |
| 76932 | 655 | /* markup and messages */ | 
| 72821 | 656 | |
| 72723 | 657 | def xml_markup( | 
| 72821 | 658 | range: Text.Range = Text.Range.full, | 
| 659 | elements: Markup.Elements = Markup.Elements.full): XML.Body = | |
| 660 | state.xml_markup(version, node_name, range = range, elements = elements) | |
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 661 | |
| 72869 | 662 | lazy val messages: List[(XML.Elem, Position.T)] = | 
| 72821 | 663 |       (for {
 | 
| 664 | (command, start) <- | |
| 76905 | 665 | Node.Commands.starts_pos(node.commands.iterator, Token.Pos.file(node_name.node)) | 
| 72821 | 666 | pos = command.span.keyword_pos(start).position(command.span.name) | 
| 72869 | 667 | (_, elem) <- state.command_results(version, command).iterator | 
| 668 | } yield (elem, pos)).toList | |
| 72821 | 669 | |
| 670 | ||
| 671 | /* exports */ | |
| 672 | ||
| 673 | lazy val exports: List[Export.Entry] = | |
| 674 | state.node_exports(version, node_name).iterator.map(_._2).toList | |
| 675 | ||
| 75770 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 676 | lazy val all_exports: Map[Export.Entry_Name, Export.Entry] = | 
| 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 677 |       (for {
 | 
| 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 678 | (name, _) <- version.nodes.iterator | 
| 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 679 | (_, entry) <- state.node_exports(version, name).iterator | 
| 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 680 | if entry.entry_name.session == Sessions.DRAFT | 
| 
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
 wenzelm parents: 
75394diff
changeset | 681 | } yield entry.entry_name -> entry).toMap | 
| 72821 | 682 | |
| 683 | ||
| 684 | /* find command */ | |
| 685 | ||
| 686 | def find_command(id: Document_ID.Generic): Option[(Node, Command)] = | |
| 687 |       state.lookup_id(id) match {
 | |
| 688 | case None => None | |
| 689 | case Some(st) => | |
| 690 | val command = st.command | |
| 72823 | 691 | val command_node = get_node(command.node_name) | 
| 692 | if (command_node.commands.contains(command)) Some((command_node, command)) else None | |
| 72821 | 693 | } | 
| 66040 | 694 | |
| 77004 | 695 | def find_command_position( | 
| 696 | id: Document_ID.Generic, | |
| 697 | offset: Symbol.Offset | |
| 698 |     ): Option[Line.Node_Position] = {
 | |
| 72821 | 699 | for ((node, command) <- find_command(id)) | 
| 700 |       yield {
 | |
| 701 | val name = command.node_name.node | |
| 702 | val sources_iterator = | |
| 703 | node.commands.iterator.takeWhile(_ != command).map(_.source) ++ | |
| 704 | (if (offset == 0) Iterator.empty | |
| 705 | else Iterator.single(command.source(Text.Range(0, command.chunk.decode(offset))))) | |
| 73359 | 706 | val pos = sources_iterator.foldLeft(Line.Position.zero)(_.advance(_)) | 
| 72821 | 707 | Line.Node_Position(name, pos) | 
| 708 | } | |
| 77004 | 709 | } | 
| 72821 | 710 | |
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 711 | def find_command_line(id: Document_ID.Generic, offset: Symbol.Offset): Option[Int] = | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 712 |       for {
 | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 713 | (node, command) <- find_command(id) | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 714 | range = Text.Range(0, command.chunk.decode(offset)) | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 715 | text <- range.try_substring(command.source) | 
| 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 716 | line <- node.command_start_line(command) | 
| 77007 | 717 | } yield line + Library.count_newlines(text) | 
| 77006 
d9a4b3a73d8c
more efficient, thanks to persistent lazy data in Document.Node;
 wenzelm parents: 
77004diff
changeset | 718 | |
| 72821 | 719 | def current_command(other_node_name: Node.Name, offset: Text.Offset): Option[Command] = | 
| 720 |       if (other_node_name.is_theory) {
 | |
| 72823 | 721 | val other_node = get_node(other_node_name) | 
| 72821 | 722 | val iterator = other_node.command_iterator(revert(offset) max 0) | 
| 723 |         if (iterator.hasNext) {
 | |
| 73344 | 724 | val (command0, _) = iterator.next() | 
| 72821 | 725 | other_node.commands.reverse.iterator(command0).find(command => !command.is_ignored) | 
| 726 | } | |
| 727 | else other_node.commands.reverse.iterator.find(command => !command.is_ignored) | |
| 728 | } | |
| 729 | else version.nodes.commands_loading(other_node_name).headOption | |
| 730 | ||
| 731 | ||
| 732 | /* command results */ | |
| 733 | ||
| 734 | def command_results(range: Text.Range): Command.Results = | |
| 735 | Command.State.merge_results( | |
| 75393 | 736 | select[List[Command.State]](range, Markup.Elements.full, | 
| 75394 | 737 | command_states => _ => Some(command_states)).flatMap(_.info)) | 
| 72821 | 738 | |
| 739 | def command_results(command: Command): Command.Results = | |
| 740 | state.command_results(version, command) | |
| 741 | ||
| 742 | ||
| 743 | /* cumulate markup */ | |
| 64665 | 744 | |
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 745 | def cumulate[A]( | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 746 | range: Text.Range, | 
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 747 | info: A, | 
| 56743 | 748 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 749 | result: List[Command.State] => (A, Text.Markup) => Option[A], | 
| 75393 | 750 | status: Boolean = false | 
| 751 |     ): List[Text.Info[A]] = {
 | |
| 72821 | 752 | val former_range = revert(range).inflate_singularity | 
| 753 | val (chunk_name, command_iterator) = | |
| 754 |         commands_loading.headOption match {
 | |
| 755 | case None => (Symbol.Text_Chunk.Default, node.command_iterator(former_range)) | |
| 756 | case Some(command) => (Symbol.Text_Chunk.File(node_name.node), Iterator((command, 0))) | |
| 757 | } | |
| 758 | val markup_index = Command.Markup_Index(status, chunk_name) | |
| 759 |       (for {
 | |
| 760 | (command, command_start) <- command_iterator | |
| 761 | chunk <- command.chunks.get(chunk_name).iterator | |
| 762 | states = state.command_states(version, command) | |
| 763 | res = result(states) | |
| 764 | markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator | |
| 765 | markup = Command.State.merge_markup(states, markup_index, markup_range, elements) | |
| 766 | Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements, | |
| 767 |           {
 | |
| 768 | case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b)) | |
| 769 | }).iterator | |
| 770 | r1 <- convert(r0 + command_start).try_restrict(range).iterator | |
| 771 | } yield Text.Info(r1, a)).toList | |
| 772 | } | |
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 773 | |
| 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 774 | def select[A]( | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 775 | range: Text.Range, | 
| 56743 | 776 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 777 | result: List[Command.State] => Text.Markup => Option[A], | 
| 75393 | 778 | status: Boolean = false | 
| 779 |     ): List[Text.Info[A]] = {
 | |
| 780 |       def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] = {
 | |
| 72821 | 781 | val res = result(states) | 
| 782 | (_: Option[A], x: Text.Markup) => | |
| 783 |           res(x) match {
 | |
| 784 | case None => None | |
| 785 | case some => Some(some) | |
| 786 | } | |
| 787 | } | |
| 788 | for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1, status)) | |
| 789 | yield Text.Info(r, x) | |
| 790 | } | |
| 38424 | 791 | } | 
| 792 | ||
| 55820 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 793 | |
| 64814 | 794 | /* model */ | 
| 795 | ||
| 75393 | 796 |   trait Session {
 | 
| 65221 | 797 | def resources: Resources | 
| 798 | } | |
| 799 | ||
| 75393 | 800 |   trait Model {
 | 
| 64814 | 801 | def session: Session | 
| 64827 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 802 | def is_stable: Boolean | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 803 | def pending_edits: List[Text.Edit] | 
| 64814 | 804 | |
| 65356 | 805 | def node_name: Node.Name | 
| 64814 | 806 | def is_theory: Boolean = node_name.is_theory | 
| 807 | override def toString: String = node_name.toString | |
| 808 | ||
| 67014 | 809 | def get_text(range: Text.Range): Option[String] | 
| 66114 | 810 | |
| 76703 
8fac11f7f0f4
clarified state: node_required is guarded by PIDE.editor.document_active (e.g. open panel);
 wenzelm parents: 
76702diff
changeset | 811 | def node_required: Boolean | 
| 76481 
a9d52d02bd83
clarified node_required status: distinguish theory_required vs. document_required;
 wenzelm parents: 
76235diff
changeset | 812 | |
| 76904 
e27d097d7d15
tuned signature: avoid confusion with Document.Node.Blob and Command.Blob;
 wenzelm parents: 
76903diff
changeset | 813 | def get_blob: Option[Blobs.Item] | 
| 76792 
23f433294173
support for generic File_Format.parse_data, with persistent result in document model;
 wenzelm parents: 
76776diff
changeset | 814 | |
| 
23f433294173
support for generic File_Format.parse_data, with persistent result in document model;
 wenzelm parents: 
76776diff
changeset | 815 | def untyped_data: AnyRef | 
| 
23f433294173
support for generic File_Format.parse_data, with persistent result in document model;
 wenzelm parents: 
76776diff
changeset | 816 | def get_data[C](c: Class[C]): Option[C] = Library.as_subclass(c)(untyped_data) | 
| 64815 | 817 | |
| 64827 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 818 | def node_edits( | 
| 64867 | 819 | node_header: Node.Header, | 
| 820 | text_edits: List[Text.Edit], | |
| 76702 | 821 | perspective: Node.Perspective_Text.T | 
| 75393 | 822 |     ): List[Edit_Text] = {
 | 
| 64827 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 823 | val edits: List[Node.Edit[Text.Edit, Text.Perspective]] = | 
| 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 824 |         get_blob match {
 | 
| 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 825 | case None => | 
| 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 826 | List( | 
| 65356 | 827 | Node.Deps( | 
| 66773 | 828 |                 if (session.resources.session_base.loaded_theory(node_name)) {
 | 
| 829 | node_header.append_errors( | |
| 830 |                     List("Cannot update finished theory " + quote(node_name.theory)))
 | |
| 831 | } | |
| 64827 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 832 | else node_header), | 
| 65356 | 833 | Node.Edits(text_edits), perspective) | 
| 834 | case Some(blob) => List(Node.Blob(blob), Node.Edits(text_edits)) | |
| 64827 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 835 | } | 
| 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 836 | edits.flatMap(edit => if (edit.is_void) None else Some(node_name -> edit)) | 
| 
4ef1eb75f1cd
uniform Document.Model.node_edits (without void edits);
 wenzelm parents: 
64819diff
changeset | 837 | } | 
| 64814 | 838 | } | 
| 839 | ||
| 840 | ||
| 55820 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 841 | /** global state -- document structure, execution process, editing history **/ | 
| 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 842 | |
| 52563 | 843 | type Assign_Update = | 
| 844 | List[(Document_ID.Command, List[Document_ID.Exec])] // update of exec state assignment | |
| 44476 
e8a87398f35d
propagate information about last command with exec state assignment through document model;
 wenzelm parents: 
44475diff
changeset | 845 | |
| 75393 | 846 |   object State {
 | 
| 38373 | 847 | class Fail(state: State) extends Exception | 
| 848 | ||
| 75393 | 849 |     object Assignment {
 | 
| 46683 | 850 | val init: Assignment = new Assignment() | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 851 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 852 | |
| 46712 | 853 | final class Assignment private( | 
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 854 | val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty, | 
| 75393 | 855 | val is_finished: Boolean = false | 
| 856 |     ) {
 | |
| 67114 | 857 |       override def toString: String = "Assignment(" + command_execs.size + "," + is_finished + ")"
 | 
| 858 | ||
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 859 |       def check_finished: Assignment = { require(is_finished, "assignment not finished"); this }
 | 
| 47388 
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
 wenzelm parents: 
46997diff
changeset | 860 | def unfinished: Assignment = new Assignment(command_execs, false) | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 861 | |
| 75393 | 862 |       def assign(update: Assign_Update): Assignment = {
 | 
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 863 | require(!is_finished, "assignment already finished") | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 864 | val command_execs1 = | 
| 73359 | 865 |           update.foldLeft(command_execs) {
 | 
| 52563 | 866 | case (res, (command_id, exec_ids)) => | 
| 867 | if (exec_ids.isEmpty) res - command_id | |
| 868 | else res + (command_id -> exec_ids) | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 869 | } | 
| 47388 
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
 wenzelm parents: 
46997diff
changeset | 870 | new Assignment(command_execs1, true) | 
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 871 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 872 | } | 
| 46682 | 873 | |
| 874 | val init: State = | |
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 875 | State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil, Nil)._2 | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 876 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 877 | |
| 46712 | 878 | final case class State private( | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 879 | /*reachable versions*/ | 
| 60215 | 880 | versions: Map[Document_ID.Version, Version] = Map.empty, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 881 | /*inlined auxiliary files*/ | 
| 60215 | 882 | blobs: Set[SHA1.Digest] = Set.empty, | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 883 | /*loaded theories in batch builds*/ | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 884 | theories: Map[Document_ID.Exec, Command.State] = Map.empty, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 885 | /*static markup from define_command*/ | 
| 60215 | 886 | commands: Map[Document_ID.Command, Command.State] = Map.empty, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 887 | /*dynamic markup from execution*/ | 
| 60215 | 888 | execs: Map[Document_ID.Exec, Command.State] = Map.empty, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 889 | /*command-exec assignment for each version*/ | 
| 60215 | 890 | assignments: Map[Document_ID.Version, State.Assignment] = Map.empty, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 891 | /*commands with markup produced by other commands (imm_succs)*/ | 
| 60215 | 892 | commands_redirection: Graph[Document_ID.Command, Unit] = Graph.long, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 893 | /*explicit (linear) history*/ | 
| 60215 | 894 | history: History = History.init, | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 895 | /*intermediate state between remove_versions/removed_versions*/ | 
| 75393 | 896 | removing_versions: Boolean = false | 
| 897 |   ) {
 | |
| 68496 | 898 | override def toString: String = | 
| 899 | "State(versions = " + versions.size + | |
| 900 | ", blobs = " + blobs.size + | |
| 901 | ", commands = " + commands.size + | |
| 902 | ", execs = " + execs.size + | |
| 903 | ", assignments = " + assignments.size + | |
| 904 | ", commands_redirection = " + commands_redirection.size + | |
| 905 | ", history = " + history.undo_list.size + | |
| 906 | ", removing_versions = " + removing_versions + ")" | |
| 907 | ||
| 38373 | 908 | private def fail[A]: A = throw new State.Fail(this) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 909 | |
| 75393 | 910 |     def define_version(version: Version, assignment: State.Assignment): State = {
 | 
| 38417 | 911 | val id = version.id | 
| 912 | copy(versions = versions + (id -> version), | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 913 | assignments = assignments + (id -> assignment.unfinished)) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 914 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 915 | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 916 | def define_blob(digest: SHA1.Digest): State = copy(blobs = blobs + digest) | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 917 | def defined_blob(digest: SHA1.Digest): Boolean = blobs.contains(digest) | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 918 | |
| 75393 | 919 |     def define_command(command: Command): State = {
 | 
| 38373 | 920 | val id = command.id | 
| 72719 | 921 | if (commands.isDefinedAt(id)) fail | 
| 922 | else copy(commands = commands + (id -> command.init_state)) | |
| 38373 | 923 | } | 
| 924 | ||
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 925 | def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id) | 
| 44582 | 926 | |
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 927 | def the_version(id: Document_ID.Version): Version = versions.getOrElse(id, fail) | 
| 52564 
4e855c120f6a
tuned signature -- NB: Command.read is actually part of Command.eval;
 wenzelm parents: 
52563diff
changeset | 928 | def the_static_state(id: Document_ID.Command): Command.State = commands.getOrElse(id, fail) | 
| 
4e855c120f6a
tuned signature -- NB: Command.read is actually part of Command.eval;
 wenzelm parents: 
52563diff
changeset | 929 | def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail) | 
| 52563 | 930 | def the_assignment(version: Version): State.Assignment = assignments.getOrElse(version.id, fail) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 931 | |
| 68857 | 932 | def lookup_id(id: Document_ID.Generic): Option[Command.State] = | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 933 | theories.get(id) orElse commands.get(id) orElse execs.get(id) | 
| 68857 | 934 | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 935 | private def self_id(st: Command.State)(id: Document_ID.Generic): Boolean = | 
| 56295 
a40e67ce4f84
clarified valid_id: always standardize towards static command.id;
 wenzelm parents: 
56176diff
changeset | 936 | id == st.command.id || | 
| 
a40e67ce4f84
clarified valid_id: always standardize towards static command.id;
 wenzelm parents: 
56176diff
changeset | 937 |       (execs.get(id) match { case Some(st1) => st1.command.id == st.command.id case None => false })
 | 
| 
a40e67ce4f84
clarified valid_id: always standardize towards static command.id;
 wenzelm parents: 
56176diff
changeset | 938 | |
| 75393 | 939 | private def other_id( | 
| 940 | node_name: Node.Name, | |
| 941 | id: Document_ID.Generic | |
| 942 |     ) : Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)] = {
 | |
| 72780 | 943 |       for {
 | 
| 944 | st <- lookup_id(id) | |
| 945 | if st.command.node_name == node_name | |
| 946 | } yield (Symbol.Text_Chunk.Id(st.command.id), st.command.chunk) | |
| 947 | } | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 948 | |
| 56475 | 949 | private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] = | 
| 73359 | 950 |       st.markups.redirection_iterator.foldLeft(commands_redirection) {
 | 
| 951 | case (graph, id) => | |
| 952 | graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.command.id) | |
| 953 | } | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 954 | |
| 75393 | 955 | def accumulate( | 
| 956 | id: Document_ID.Generic, | |
| 957 | message: XML.Elem, | |
| 958 | cache: XML.Cache | |
| 959 |     ) : (Command.State, State) = {
 | |
| 960 |       def update(st: Command.State): (Command.State, State) = {
 | |
| 73031 
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
 wenzelm parents: 
72962diff
changeset | 961 | val st1 = st.accumulate(self_id(st), other_id, message, cache) | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 962 | (st1, copy(commands_redirection = redirection(st1))) | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 963 | } | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 964 |       execs.get(id).map(update) match {
 | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 965 | case Some((st1, state1)) => (st1, state1.copy(execs = execs + (id -> st1))) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 966 | case None => | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 967 |           commands.get(id).map(update) match {
 | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 968 | case Some((st1, state1)) => (st1, state1.copy(commands = commands + (id -> st1))) | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 969 | case None => | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 970 |               theories.get(id).map(update) match {
 | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 971 | case Some((st1, state1)) => (st1, state1.copy(theories = theories + (id -> st1))) | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 972 | case None => fail | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 973 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 974 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 975 | } | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 976 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 977 | |
| 75393 | 978 | def add_export( | 
| 979 | id: Document_ID.Generic, | |
| 980 | entry: Command.Exports.Entry | |
| 981 |     ): (Command.State, State) = {
 | |
| 68101 | 982 |       execs.get(id) match {
 | 
| 983 | case Some(st) => | |
| 68114 | 984 |           st.add_export(entry) match {
 | 
| 985 | case Some(new_st) => (new_st, copy(execs = execs + (id -> new_st))) | |
| 986 | case None => fail | |
| 987 | } | |
| 68101 | 988 | case None => | 
| 989 |           commands.get(id) match {
 | |
| 990 | case Some(st) => | |
| 68114 | 991 |               st.add_export(entry) match {
 | 
| 992 | case Some(new_st) => (new_st, copy(commands = commands + (id -> new_st))) | |
| 993 | case None => fail | |
| 994 | } | |
| 68101 | 995 | case None => fail | 
| 996 | } | |
| 997 | } | |
| 998 | } | |
| 999 | ||
| 69633 | 1000 | def node_exports(version: Version, node_name: Node.Name): Command.Exports = | 
| 1001 | Command.Exports.merge( | |
| 1002 |         for {
 | |
| 1003 | command <- version.nodes(node_name).commands.iterator | |
| 1004 | st <- command_states(version, command).iterator | |
| 1005 | } yield st.exports) | |
| 1006 | ||
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1007 | def begin_theory( | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1008 | node_name: Node.Name, | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1009 | id: Document_ID.Exec, | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1010 | source: String, | 
| 75393 | 1011 | blobs_info: Command.Blobs_Info | 
| 1012 |     ): State = {
 | |
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1013 | if (theories.isDefinedAt(id)) fail | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1014 |       else {
 | 
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1015 | val command = | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1016 | Command.unparsed(source, theory = true, id = id, node_name = node_name, | 
| 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1017 | blobs_info = blobs_info) | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1018 | copy(theories = theories + (id -> command.empty_state)) | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1019 | } | 
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1020 | } | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1021 | |
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 1022 | def end_theory(id: Document_ID.Exec, document_blobs: Node.Name => Blobs): (Snapshot, State) = | 
| 72861 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72860diff
changeset | 1023 |       theories.get(id) match {
 | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1024 | case None => fail | 
| 72721 
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
 wenzelm parents: 
72719diff
changeset | 1025 | case Some(st) => | 
| 
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
 wenzelm parents: 
72719diff
changeset | 1026 | val command = st.command | 
| 
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
 wenzelm parents: 
72719diff
changeset | 1027 | val node_name = command.node_name | 
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 1028 | val doc_blobs = document_blobs(node_name) | 
| 72721 
79f5e843e5ec
clarified signature: prefer high-level Snapshot over low-level Command.State;
 wenzelm parents: 
72719diff
changeset | 1029 | val command1 = | 
| 72861 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72860diff
changeset | 1030 | Command.unparsed(command.source, theory = true, id = id, node_name = node_name, | 
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1031 | blobs_info = command.blobs_info, results = st.results, markups = st.markups) | 
| 72861 
3f5e6da08687
clarified protocol: support "isabelle log" on failed theories as well;
 wenzelm parents: 
72860diff
changeset | 1032 | val state1 = copy(theories = theories - id) | 
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 1033 | (state1.snippet(command1, doc_blobs), state1) | 
| 72692 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1034 | } | 
| 
22aeec526ffd
support for PIDE markup in batch build (inactive due to pide_reports=false);
 wenzelm parents: 
72669diff
changeset | 1035 | |
| 75393 | 1036 | def assign( | 
| 1037 | id: Document_ID.Version, | |
| 1038 | edited: List[String], | |
| 1039 | update: Assign_Update | |
| 1040 |     ) : ((List[Node.Name], List[Command]), State) = {
 | |
| 38417 | 1041 | val version = the_version(id) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1042 | |
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 1043 | val edited_set = edited.toSet | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 1044 | val edited_nodes = | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 1045 |         (for { (name, _) <- version.nodes.iterator if edited_set(name.node) } yield name).toList
 | 
| 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 1046 | |
| 76759 | 1047 | def upd( | 
| 1048 | exec_id: Document_ID.Exec, | |
| 1049 | st: Command.State | |
| 1050 |       ): Option[(Document_ID.Exec, Command.State)] = {
 | |
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1051 | if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st) | 
| 76759 | 1052 | } | 
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1053 | |
| 44543 | 1054 | val (changed_commands, new_execs) = | 
| 73361 | 1055 |         update.foldLeft((List.empty[Command], execs)) {
 | 
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1056 | case ((commands1, execs1), (command_id, exec)) => | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1057 | val st = the_static_state(command_id) | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1058 | val command = st.command | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1059 | val commands2 = command :: commands1 | 
| 44543 | 1060 | val execs2 = | 
| 1061 |               exec match {
 | |
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1062 | case Nil => execs1 | 
| 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1063 | case eval_id :: print_ids => | 
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1064 | execs1 ++ upd(eval_id, st) ++ | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 1065 | (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up) | 
| 44543 | 1066 | } | 
| 1067 | (commands2, execs2) | |
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1068 | } | 
| 52563 | 1069 | val new_assignment = the_assignment(version).assign(update) | 
| 43722 | 1070 | val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs) | 
| 44476 
e8a87398f35d
propagate information about last command with exec state assignment through document model;
 wenzelm parents: 
44475diff
changeset | 1071 | |
| 70284 
3e17c3a5fd39
more thorough assignment, e.g. when "purge" removes commands that were not assigned;
 wenzelm parents: 
69633diff
changeset | 1072 | ((edited_nodes, changed_commands), new_state) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1073 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1074 | |
| 38417 | 1075 | def is_assigned(version: Version): Boolean = | 
| 43722 | 1076 |       assignments.get(version.id) match {
 | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1077 | case Some(assgn) => assgn.is_finished | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1078 | case None => false | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1079 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 1080 | |
| 44436 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 1081 | def is_stable(change: Change): Boolean = | 
| 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 1082 | change.is_finished && is_assigned(change.version.get_finished) | 
| 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 1083 | |
| 46944 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 1084 | def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail | 
| 44672 | 1085 | def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail | 
| 60933 
6d03e05ef041
more robust access to stable tip version: take all pending edits into account, don't assume model for current buffer;
 wenzelm parents: 
60916diff
changeset | 1086 | def stable_tip_version: Option[Version] = | 
| 
6d03e05ef041
more robust access to stable tip version: take all pending edits into account, don't assume model for current buffer;
 wenzelm parents: 
60916diff
changeset | 1087 | if (is_stable(history.tip)) Some(history.tip.version.get_finished) else None | 
| 44436 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 1088 | |
| 44446 | 1089 | def continue_history( | 
| 56711 | 1090 | previous: Future[Version], | 
| 1091 | edits: List[Edit_Text], | |
| 75393 | 1092 | version: Future[Version] | 
| 1093 |     ): State = {
 | |
| 46678 | 1094 | val change = Change.make(previous, edits, version) | 
| 56711 | 1095 | copy(history = history + change) | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 1096 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 1097 | |
| 75393 | 1098 |     def remove_versions(retain: Int = 0): (List[Version], State) = {
 | 
| 46679 | 1099 |       history.prune(is_stable, retain) match {
 | 
| 1100 | case Some((dropped, history1)) => | |
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 1101 | val old_versions = dropped.map(change => change.version.get_finished) | 
| 59319 | 1102 | val removing = old_versions.nonEmpty | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 1103 | val state1 = copy(history = history1, removing_versions = removing) | 
| 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 1104 | (old_versions, state1) | 
| 46679 | 1105 | case None => fail | 
| 44672 | 1106 | } | 
| 1107 | } | |
| 1108 | ||
| 75393 | 1109 |     def removed_versions(removed: List[Document_ID.Version]): State = {
 | 
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 1110 | val versions1 = Version.purge_suppressed(versions -- removed) | 
| 67110 
3156faac30a7
purge hidden nodes more thoroughly: is_hidden may become true only later;
 wenzelm parents: 
67014diff
changeset | 1111 | |
| 44676 | 1112 | val assignments1 = assignments -- removed | 
| 67112 | 1113 | var blobs1_names = Set.empty[Node.Name] | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 1114 | var blobs1 = Set.empty[SHA1.Digest] | 
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 1115 | var commands1 = Map.empty[Document_ID.Command, Command.State] | 
| 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 1116 | var execs1 = Map.empty[Document_ID.Exec, Command.State] | 
| 44676 | 1117 |       for {
 | 
| 67112 | 1118 | (version_id, version) <- versions1.iterator | 
| 46997 | 1119 | command_execs = assignments1(version_id).command_execs | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 1120 | (_, node) <- version.nodes.iterator | 
| 44676 | 1121 | command <- node.commands.iterator | 
| 1122 |       } {
 | |
| 67112 | 1123 |         for ((name, digest) <- command.blobs_defined) {
 | 
| 1124 | blobs1_names += name | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 1125 | blobs1 += digest | 
| 67112 | 1126 | } | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 1127 | |
| 76759 | 1128 |         if (!commands1.isDefinedAt(command.id)) {
 | 
| 52568 | 1129 | commands.get(command.id).foreach(st => commands1 += (command.id -> st)) | 
| 76759 | 1130 | } | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 1131 | |
| 76760 | 1132 |         for {
 | 
| 1133 | exec_id <- command_execs.getOrElse(command.id, Nil) | |
| 1134 | if !execs1.isDefinedAt(exec_id) | |
| 1135 | st <- execs.get(exec_id) | |
| 1136 | } execs1 += (exec_id -> st) | |
| 44676 | 1137 | } | 
| 67112 | 1138 | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1139 | copy( | 
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 1140 | versions = versions1, | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1141 | blobs = blobs1, | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1142 | commands = commands1, | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1143 | execs = execs1, | 
| 70699 | 1144 | commands_redirection = commands_redirection.restrict(commands1.keySet), | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 1145 | assignments = assignments1, | 
| 68300 
cd8ab1a7a286
retain isolated blob nodes (amending deb2fcbda16e): avoid failure of Session.handle_change with "Missing blob", when opening theory with load command later;
 wenzelm parents: 
68299diff
changeset | 1146 | history = history.purge(versions1), | 
| 57976 
bf99106b6672
postpone changes in intermediate state between remove_versions/removed_versions, which is important for handle_change to refer to defined items on prover side;
 wenzelm parents: 
57842diff
changeset | 1147 | removing_versions = false) | 
| 44676 | 1148 | } | 
| 1149 | ||
| 75393 | 1150 |     def command_maybe_consolidated(version: Version, command: Command): Boolean = {
 | 
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 1151 | require(is_assigned(version), "version not assigned (command_maybe_consolidated)") | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1152 |       try {
 | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1153 |         the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil) match {
 | 
| 70780 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1154 | case eval_id :: print_ids => | 
| 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1155 | the_dynamic_state(eval_id).maybe_consolidated && | 
| 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1156 | !print_ids.exists(print_id => the_dynamic_state(print_id).consolidating) | 
| 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1157 | case Nil => false | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1158 | } | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1159 | } | 
| 70780 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1160 |       catch { case _: State.Fail => false }
 | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1161 | } | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1162 | |
| 75393 | 1163 | private def command_states_self( | 
| 1164 | version: Version, | |
| 1165 | command: Command | |
| 1166 |     ) : List[(Document_ID.Generic, Command.State)] = {
 | |
| 73120 
c3589f2dff31
more informative errors: simplify diagnosis of spurious failures reported by users;
 wenzelm parents: 
73031diff
changeset | 1167 | require(is_assigned(version), "version not assigned (command_states_self)") | 
| 44613 | 1168 |       try {
 | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1169 | the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil) | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1170 |           .map(id => id -> the_dynamic_state(id)) match {
 | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1171 | case Nil => fail | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1172 | case res => res | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 1173 | } | 
| 44613 | 1174 | } | 
| 47395 
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
 wenzelm parents: 
47388diff
changeset | 1175 |       catch {
 | 
| 
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
 wenzelm parents: 
47388diff
changeset | 1176 | case _: State.Fail => | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1177 |           try { List(command.id -> the_static_state(command.id)) }
 | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1178 |           catch { case _: State.Fail => List(command.id -> command.init_state) }
 | 
| 47395 
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
 wenzelm parents: 
47388diff
changeset | 1179 | } | 
| 44613 | 1180 | } | 
| 1181 | ||
| 75393 | 1182 |     def command_states(version: Version, command: Command): List[Command.State] = {
 | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1183 | val self = command_states_self(version, command) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1184 | val others = | 
| 56475 | 1185 |         if (commands_redirection.defined(command.id)) {
 | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1186 |           (for {
 | 
| 56475 | 1187 | command_id <- commands_redirection.imm_succs(command.id).iterator | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1188 | (id, st) <- command_states_self(version, the_static_state(command_id).command) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1189 | if !self.exists(_._1 == id) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1190 | } yield (id, st)).toMap.valuesIterator.toList | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1191 | } | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1192 | else Nil | 
| 56489 | 1193 | self.map(_._2) ::: others.flatMap(_.redirect(command)) | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1194 | } | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 1195 | |
| 56299 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 1196 | def command_results(version: Version, command: Command): Command.Results = | 
| 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 1197 | Command.State.merge_results(command_states(version, command)) | 
| 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 1198 | |
| 56301 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 1199 | def command_markup(version: Version, command: Command, index: Command.Markup_Index, | 
| 56743 | 1200 | range: Text.Range, elements: Markup.Elements): Markup_Tree = | 
| 56301 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 1201 | Command.State.merge_markup(command_states(version, command), index, range, elements) | 
| 56299 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 1202 | |
| 72723 | 1203 | def xml_markup( | 
| 66041 | 1204 | version: Version, | 
| 67264 | 1205 | node_name: Node.Name, | 
| 72723 | 1206 | range: Text.Range = Text.Range.full, | 
| 75393 | 1207 | elements: Markup.Elements = Markup.Elements.full | 
| 1208 |     ): XML.Body = {
 | |
| 67264 | 1209 | val node = version.nodes(node_name) | 
| 1210 |       if (node_name.is_theory) {
 | |
| 1211 | val markup_index = Command.Markup_Index.markup | |
| 1212 |         (for {
 | |
| 1213 | command <- node.commands.iterator | |
| 1214 | command_range <- command.range.try_restrict(range).iterator | |
| 1215 | markup = command_markup(version, command, markup_index, command_range, elements) | |
| 1216 | tree <- markup.to_XML(command_range, command.source, elements).iterator | |
| 1217 | } yield tree).toList | |
| 1218 | } | |
| 76924 | 1219 |       else if (node.source_wellformed) {
 | 
| 76235 | 1220 |         Text.Range.length(node.source).try_restrict(range) match {
 | 
| 67264 | 1221 | case Some(node_range) => | 
| 1222 | val markup = | |
| 1223 |               version.nodes.commands_loading(node_name).headOption match {
 | |
| 1224 | case None => Markup_Tree.empty | |
| 1225 | case Some(command) => | |
| 1226 | val chunk_name = Symbol.Text_Chunk.File(node_name.node) | |
| 1227 | val markup_index = Command.Markup_Index(false, chunk_name) | |
| 1228 | command_markup(version, command, markup_index, node_range, elements) | |
| 1229 | } | |
| 76204 
b80b2fbc46c3
clarified signature: persistent Node.source / Snapshot.source;
 wenzelm parents: 
75914diff
changeset | 1230 | markup.to_XML(node_range, node.source, elements) | 
| 76917 | 1231 | case None => Nil | 
| 67264 | 1232 | } | 
| 1233 | } | |
| 76917 | 1234 | else Nil | 
| 66041 | 1235 | } | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 1236 | |
| 68323 | 1237 | def node_initialized(version: Version, name: Node.Name): Boolean = | 
| 1238 | name.is_theory && | |
| 1239 |       (version.nodes(name).commands.iterator.find(_.potentially_initialized) match {
 | |
| 1240 | case None => false | |
| 1241 | case Some(command) => command_states(version, command).headOption.exists(_.initialized) | |
| 1242 | }) | |
| 1243 | ||
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1244 | def node_maybe_consolidated(version: Version, name: Node.Name): Boolean = | 
| 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1245 | name.is_theory && | 
| 70780 
034742453594
more robust: avoid update/interrupt of long-running print_consolidation;
 wenzelm parents: 
70716diff
changeset | 1246 | version.nodes(name).commands.reverse.iterator.forall(command_maybe_consolidated(version, _)) | 
| 68381 
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
 wenzelm parents: 
68365diff
changeset | 1247 | |
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66195diff
changeset | 1248 | def node_consolidated(version: Version, name: Node.Name): Boolean = | 
| 75393 | 1249 |       !name.is_theory || {
 | 
| 68335 | 1250 | val it = version.nodes(name).commands.reverse.iterator | 
| 73344 | 1251 | it.hasNext && command_states(version, it.next()).exists(_.consolidated) | 
| 68335 | 1252 | } | 
| 66379 
6392766f3c25
maintain "consolidated" status of theory nodes, which means all evals are finished (but not necessarily prints nor imports);
 wenzelm parents: 
66195diff
changeset | 1253 | |
| 72816 
ea4f86914cb2
support for PIDE markup for auxiliary files ("blobs");
 wenzelm parents: 
72780diff
changeset | 1254 | def snapshot( | 
| 72818 | 1255 | node_name: Node.Name = Node.Name.empty, | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1256 | pending_edits: Pending_Edits = Pending_Edits.empty, | 
| 75393 | 1257 | snippet_command: Option[Command] = None | 
| 1258 |     ): Snapshot = {
 | |
| 72819 | 1259 | val stable = recent_stable | 
| 72820 | 1260 | val version = stable.version.get_finished | 
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 1261 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1262 | val pending_edits1 = | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1263 |         (for {
 | 
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 1264 | change <- history.undo_list.takeWhile(_ != stable) | 
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1265 | (name, Node.Edits(es)) <- change.rev_edits | 
| 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1266 | } yield (name -> es)).foldLeft(pending_edits)(_ + _) | 
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 1267 | |
| 76765 
c654103e9c9d
more robust Document.Pending_Edits: cover all nodes simulataneously, and thus support proper Snapshot.switch;
 wenzelm parents: 
76760diff
changeset | 1268 | new Snapshot(this, version, node_name, pending_edits1, snippet_command) | 
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 1269 | } | 
| 72859 | 1270 | |
| 76912 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 1271 | def snippet(command: Command, doc_blobs: Blobs): Snapshot = | 
| 
ca872f20cf5b
clarified session sources: theory and blobs are read from database, instead of physical file-system;
 wenzelm parents: 
76905diff
changeset | 1272 | snapshot().snippet(command, doc_blobs) | 
| 34485 | 1273 | } | 
| 34840 
6c5560d48561
more precise treatment of document/state assigment;
 wenzelm parents: 
34838diff
changeset | 1274 | } |