| author | wenzelm | 
| Sun, 14 Feb 2016 13:11:19 +0100 | |
| changeset 62306 | 5c0a5c30cda8 | 
| parent 61197 | b9d69001824e | 
| child 62492 | 0e53fade87fe | 
| 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 | ||
| 34823 | 14 | object Document | 
| 34483 | 15 | {
 | 
| 38424 | 16 | /** document structure **/ | 
| 17 | ||
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 18 | /* overlays -- print functions with arguments */ | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 19 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 20 | object Overlays | 
| 
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 | val empty = new Overlays(Map.empty) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 23 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 24 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 25 | final class Overlays private(rep: Map[Node.Name, Node.Overlays]) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 26 |   {
 | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 27 | def apply(name: Document.Node.Name): Node.Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 28 | rep.getOrElse(name, Node.Overlays.empty) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 29 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 30 | private def update(name: Node.Name, f: Node.Overlays => Node.Overlays): Overlays = | 
| 
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 | val node_overlays = f(apply(name)) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 33 | 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 | 34 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 35 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 36 | def insert(command: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 37 | update(command.node_name, _.insert(command, fn, args)) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 38 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 39 | def remove(command: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 40 | update(command.node_name, _.remove(command, fn, args)) | 
| 55800 | 41 | |
| 42 |     override def toString: String = rep.mkString("Overlays(", ",", ")")
 | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 43 | } | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 44 | |
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 45 | |
| 55783 | 46 | /* document blobs: auxiliary files */ | 
| 47 | ||
| 56746 | 48 | sealed case class Blob(bytes: Bytes, chunk: Symbol.Text_Chunk, changed: Boolean) | 
| 56335 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56314diff
changeset | 49 |   {
 | 
| 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56314diff
changeset | 50 | def unchanged: Blob = copy(changed = false) | 
| 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56314diff
changeset | 51 | } | 
| 55783 | 52 | |
| 53 | object Blobs | |
| 54 |   {
 | |
| 56336 | 55 | def apply(blobs: Map[Node.Name, Blob]): Blobs = new Blobs(blobs) | 
| 55783 | 56 | val empty: Blobs = apply(Map.empty) | 
| 57 | } | |
| 58 | ||
| 56336 | 59 | final class Blobs private(blobs: Map[Node.Name, Blob]) | 
| 55783 | 60 |   {
 | 
| 56336 | 61 | def get(name: Node.Name): Option[Blob] = blobs.get(name) | 
| 55783 | 62 | |
| 63 | def changed(name: Node.Name): Boolean = | |
| 64 |       get(name) match {
 | |
| 65 | case Some(blob) => blob.changed | |
| 66 | case None => false | |
| 67 | } | |
| 68 | ||
| 55800 | 69 |     override def toString: String = blobs.mkString("Blobs(", ",", ")")
 | 
| 55783 | 70 | } | 
| 71 | ||
| 72 | ||
| 73 | /* document nodes: theories and auxiliary files */ | |
| 38424 | 74 | |
| 44615 | 75 | type Edit[A, B] = (Node.Name, Node.Edit[A, B]) | 
| 44384 | 76 | type Edit_Text = Edit[Text.Edit, Text.Perspective] | 
| 52849 | 77 | type Edit_Command = Edit[Command.Edit, Command.Perspective] | 
| 38151 | 78 | |
| 79 | object Node | |
| 80 |   {
 | |
| 52887 | 81 | /* header and name */ | 
| 82 | ||
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 83 | sealed case class Header( | 
| 59695 | 84 | imports: List[(Name, Position.T)], | 
| 48706 | 85 | keywords: Thy_Header.Keywords, | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 86 | errors: List[String]) | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 87 |     {
 | 
| 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 88 | def error(msg: String): Header = copy(errors = errors ::: List(msg)) | 
| 54549 
2a3053472ec3
actually expose errors of cumulative theory dependencies;
 wenzelm parents: 
54530diff
changeset | 89 | |
| 
2a3053472ec3
actually expose errors of cumulative theory dependencies;
 wenzelm parents: 
54530diff
changeset | 90 | def cat_errors(msg2: String): Header = | 
| 
2a3053472ec3
actually expose errors of cumulative theory dependencies;
 wenzelm parents: 
54530diff
changeset | 91 | copy(errors = errors.map(msg1 => Library.cat_message(msg1, msg2))) | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 92 | } | 
| 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 93 | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 94 | val no_header = Header(Nil, Nil, Nil) | 
| 51294 
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
 wenzelm parents: 
51293diff
changeset | 95 | def bad_header(msg: String): Header = Header(Nil, Nil, List(msg)) | 
| 46737 | 96 | |
| 44957 
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
 wenzelm parents: 
44676diff
changeset | 97 | object Name | 
| 
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
 wenzelm parents: 
44676diff
changeset | 98 |     {
 | 
| 54515 | 99 |       val empty = Name("")
 | 
| 46723 | 100 | |
| 101 | object Ordering extends scala.math.Ordering[Name] | |
| 102 |       {
 | |
| 103 | def compare(name1: Name, name2: Name): Int = name1.node compare name2.node | |
| 104 | } | |
| 44957 
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
 wenzelm parents: 
44676diff
changeset | 105 | } | 
| 52887 | 106 | |
| 54515 | 107 | sealed case class Name(node: String, master_dir: String = "", theory: String = "") | 
| 44615 | 108 |     {
 | 
| 109 | override def hashCode: Int = node.hashCode | |
| 110 | override def equals(that: Any): Boolean = | |
| 111 |         that match {
 | |
| 112 | case other: Name => node == other.node | |
| 113 | case _ => false | |
| 114 | } | |
| 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 | 115 | |
| 59319 | 116 | def is_theory: Boolean = theory.nonEmpty | 
| 54510 | 117 | 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 | 118 | |
| 
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 | 119 | def map(f: String => String): Name = copy(f(node), f(master_dir), theory) | 
| 44615 | 120 | } | 
| 121 | ||
| 52887 | 122 | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 123 | /* node overlays */ | 
| 52887 | 124 | |
| 125 | object Overlays | |
| 126 |     {
 | |
| 52976 | 127 | val empty = new Overlays(Multi_Map.empty) | 
| 52887 | 128 | } | 
| 129 | ||
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 130 | final class Overlays private(rep: Multi_Map[Command, (String, List[String])]) | 
| 52887 | 131 |     {
 | 
| 132 | def commands: Set[Command] = rep.keySet | |
| 133 | def is_empty: Boolean = rep.isEmpty | |
| 52977 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 134 | def dest: List[(Command, (String, List[String]))] = rep.iterator.toList | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 135 | def insert(cmd: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 136 | new Overlays(rep.insert(cmd, (fn, args))) | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 137 | def remove(cmd: Command, fn: String, args: List[String]): Overlays = | 
| 
15254e32d299
central management of Document.Overlays, independent of Document_Model;
 wenzelm parents: 
52976diff
changeset | 138 | new Overlays(rep.remove(cmd, (fn, args))) | 
| 55800 | 139 | |
| 140 |       override def toString: String = rep.mkString("Node.Overlays(", ",", ")")
 | |
| 52887 | 141 | } | 
| 142 | ||
| 143 | ||
| 144 | /* edits */ | |
| 145 | ||
| 44384 | 146 | sealed abstract class Edit[A, B] | 
| 44156 | 147 |     {
 | 
| 44383 | 148 | def foreach(f: A => Unit) | 
| 149 |       {
 | |
| 44156 | 150 |         this match {
 | 
| 44383 | 151 | case Edits(es) => es.foreach(f) | 
| 152 | case _ => | |
| 44156 | 153 | } | 
| 44383 | 154 | } | 
| 57621 | 155 | |
| 156 | def is_void: Boolean = | |
| 157 |         this match {
 | |
| 158 | case Edits(Nil) => true | |
| 159 | case _ => false | |
| 160 | } | |
| 44156 | 161 | } | 
| 44384 | 162 | case class Clear[A, B]() extends Edit[A, B] | 
| 56335 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56314diff
changeset | 163 | case class Blob[A, B](blob: Document.Blob) 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 | 164 | |
| 44384 | 165 | case class Edits[A, B](edits: List[A]) extends Edit[A, B] | 
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 166 | case class Deps[A, B](header: Header) extends Edit[A, B] | 
| 52849 | 167 | case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B] | 
| 57610 | 168 | |
| 169 | ||
| 170 | /* perspective */ | |
| 171 | ||
| 52808 
143f225e50f5
allow explicit indication of required node: full eval, no prints;
 wenzelm parents: 
52568diff
changeset | 172 | type Perspective_Text = Perspective[Text.Edit, Text.Perspective] | 
| 52849 | 173 | type Perspective_Command = Perspective[Command.Edit, Command.Perspective] | 
| 44156 | 174 | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 175 | val no_perspective_text: Perspective_Text = | 
| 57610 | 176 | Perspective(false, Text.Perspective.empty, Overlays.empty) | 
| 177 | ||
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 178 | val no_perspective_command: Perspective_Command = | 
| 57614 | 179 | Perspective(false, Command.Perspective.empty, Overlays.empty) | 
| 180 | ||
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 181 | def is_no_perspective_command(perspective: Perspective_Command): Boolean = | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 182 | !perspective.required && | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 183 | perspective.visible.is_empty && | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 184 | perspective.overlays.is_empty | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 185 | |
| 52887 | 186 | |
| 187 | /* commands */ | |
| 188 | ||
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 189 | object Commands | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 190 |     {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 191 | 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 | 192 | val empty: Commands = apply(Linear_Set.empty) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 193 | |
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 194 | def starts(commands: Iterator[Command], offset: Text.Offset = 0) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 195 | : Iterator[(Command, Text.Offset)] = | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 196 |       {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 197 | var i = offset | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 198 |         for (command <- commands) yield {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 199 | val start = i | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 200 | i += command.length | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 201 | (command, start) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 202 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 203 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 204 | |
| 56398 
15d0821c8667
afford larger full_index, to save a few milliseconds during rendering (notably text_overview);
 wenzelm parents: 
56394diff
changeset | 205 | private val block_size = 256 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 206 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 207 | |
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 208 | final class Commands private(val commands: Linear_Set[Command]) | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 209 |     {
 | 
| 56314 | 210 | lazy val load_commands: List[Command] = | 
| 59319 | 211 | commands.iterator.filter(cmd => cmd.blobs.nonEmpty).toList | 
| 54462 | 212 | |
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 213 | private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) = | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 214 |       {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 215 | val blocks = new mutable.ListBuffer[(Command, Text.Offset)] | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 216 | var next_block = 0 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 217 | var last_stop = 0 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 218 |         for ((command, start) <- Commands.starts(commands.iterator)) {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 219 | last_stop = start + command.length | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 220 |           while (last_stop + 1 > next_block) {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 221 | blocks += (command -> start) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 222 | next_block += Commands.block_size | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 223 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 224 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 225 | (blocks.toArray, Text.Range(0, last_stop)) | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 226 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 227 | |
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 228 | private def full_range: Text.Range = full_index._2 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 229 | |
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 230 | def iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] = | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 231 |       {
 | 
| 59319 | 232 |         if (commands.nonEmpty && full_range.contains(i)) {
 | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 233 | 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 | 234 |           Node.Commands.starts(commands.iterator(cmd0), start0) dropWhile {
 | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 235 | case (cmd, start) => start + cmd.length <= i } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 236 | } | 
| 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 237 | else Iterator.empty | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 238 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 239 | } | 
| 57614 | 240 | |
| 241 | val empty: Node = new Node() | |
| 38151 | 242 | } | 
| 243 | ||
| 46712 | 244 | final class Node private( | 
| 56335 
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
 wenzelm parents: 
56314diff
changeset | 245 | val get_blob: Option[Document.Blob] = None, | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 246 | val header: Node.Header = Node.no_header, | 
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 247 | val syntax: Option[Prover.Syntax] = None, | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 248 | val text_perspective: Text.Perspective = Text.Perspective.empty, | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 249 | val perspective: Node.Perspective_Command = Node.no_perspective_command, | 
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 250 | _commands: Node.Commands = Node.Commands.empty) | 
| 38151 | 251 |   {
 | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 252 | def is_empty: Boolean = | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 253 | get_blob.isEmpty && | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 254 | 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 | 255 | text_perspective.is_empty && | 
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 256 | Node.is_no_perspective_command(perspective) && | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 257 | commands.isEmpty | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 258 | |
| 59702 
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
 wenzelm parents: 
59695diff
changeset | 259 | def has_header: Boolean = header != Node.no_header | 
| 
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
 wenzelm parents: 
59695diff
changeset | 260 | |
| 57615 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 261 | def commands: Linear_Set[Command] = _commands.commands | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 262 | def load_commands: List[Command] = _commands.load_commands | 
| 
df1b3452d71c
more explicit discrimination of empty nodes -- suppress from Theories panel;
 wenzelm parents: 
57614diff
changeset | 263 | |
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 264 | def clear: Node = new Node(header = header, syntax = syntax) | 
| 46680 | 265 | |
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 266 | def init_blob(blob: Document.Blob): Node = new Node(get_blob = Some(blob.unchanged)) | 
| 55435 
662e0fd39823
maintain blob edits within history, which is important for Snapshot.convert/revert;
 wenzelm parents: 
55434diff
changeset | 267 | |
| 48707 
ba531af91148
simplified Document.Node.Header -- internalized errors;
 wenzelm parents: 
48706diff
changeset | 268 | 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 | 269 | 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 | 270 | |
| 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 271 | def update_syntax(new_syntax: Option[Prover.Syntax]): Node = | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 272 | 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 | 273 | |
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 274 | def update_perspective( | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 275 | new_text_perspective: Text.Perspective, | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 276 | new_perspective: Node.Perspective_Command): Node = | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 277 | new Node(get_blob, header, syntax, new_text_perspective, new_perspective, _commands) | 
| 46680 | 278 | |
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 279 | 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 | 280 | Node.Perspective(perspective.required, text_perspective, perspective.overlays) | 
| 46680 | 281 | |
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 282 | def same_perspective( | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 283 | other_text_perspective: Text.Perspective, | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 284 | other_perspective: Node.Perspective_Command): Boolean = | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 285 | text_perspective == other_text_perspective && | 
| 52849 | 286 | perspective.required == other_perspective.required && | 
| 287 | perspective.visible.same(other_perspective.visible) && | |
| 288 | perspective.overlays == other_perspective.overlays | |
| 52808 
143f225e50f5
allow explicit indication of required node: full eval, no prints;
 wenzelm parents: 
52568diff
changeset | 289 | |
| 52901 
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
 wenzelm parents: 
52900diff
changeset | 290 | def update_commands(new_commands: Linear_Set[Command]): Node = | 
| 52918 
038458a4d11b
proper low-level comparison -- heed warning by Scala compiler;
 wenzelm parents: 
52901diff
changeset | 291 | if (new_commands eq _commands.commands) this | 
| 59372 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 292 | else | 
| 
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
 wenzelm parents: 
59319diff
changeset | 293 | 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 | 294 | Node.Commands(new_commands)) | 
| 38151 | 295 | |
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 296 | def command_iterator(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] = | 
| 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 297 | _commands.iterator(i) | 
| 38151 | 298 | |
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 299 | def command_iterator(range: Text.Range): Iterator[(Command, Text.Offset)] = | 
| 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 300 |       command_iterator(range.start) takeWhile { case (_, start) => start < range.stop }
 | 
| 38151 | 301 | |
| 38879 
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
 wenzelm parents: 
38872diff
changeset | 302 | 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 | 303 | Node.Commands.starts(commands.iterator).find(_._1 == cmd).map(_._2) | 
| 38151 | 304 | } | 
| 305 | ||
| 306 | ||
| 46723 | 307 | /* development graph */ | 
| 308 | ||
| 309 | object Nodes | |
| 310 |   {
 | |
| 311 | val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering)) | |
| 312 | } | |
| 313 | ||
| 314 | final class Nodes private(graph: Graph[Node.Name, Node]) | |
| 315 |   {
 | |
| 316 | def apply(name: Node.Name): Node = | |
| 317 | graph.default_node(name, Node.empty).get_node(name) | |
| 318 | ||
| 57619 | 319 | def is_hidden(name: Node.Name): Boolean = | 
| 320 |     {
 | |
| 321 | val graph1 = graph.default_node(name, Node.empty) | |
| 322 | graph1.is_maximal(name) && graph1.get_node(name).is_empty | |
| 323 | } | |
| 57617 | 324 | |
| 46723 | 325 | def + (entry: (Node.Name, Node)): Nodes = | 
| 326 |     {
 | |
| 327 | val (name, node) = entry | |
| 59695 | 328 | val imports = node.header.imports.map(_._1) | 
| 46723 | 329 | val graph1 = | 
| 46739 
6024353549ca
clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
 wenzelm parents: 
46737diff
changeset | 330 | (graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty)) | 
| 46723 | 331 | val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name)) | 
| 46739 
6024353549ca
clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
 wenzelm parents: 
46737diff
changeset | 332 | val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name)) | 
| 57616 | 333 | new Nodes( | 
| 334 | if (graph3.is_maximal(name) && node.is_empty) graph3.del_node(name) | |
| 335 | else graph3.map_node(name, _ => node) | |
| 336 | ) | |
| 46723 | 337 | } | 
| 338 | ||
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 339 | def iterator: Iterator[(Node.Name, Node)] = | 
| 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 340 |       graph.iterator.map({ case (name, (node, _)) => (name, node) })
 | 
| 46723 | 341 | |
| 56314 | 342 | def load_commands(file_name: Node.Name): List[Command] = | 
| 54528 | 343 |       (for {
 | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 344 | (_, node) <- iterator | 
| 56314 | 345 | cmd <- node.load_commands.iterator | 
| 54530 
2c1440f70028
ranges of thy_load commands count as visible within perspective;
 wenzelm parents: 
54528diff
changeset | 346 | name <- cmd.blobs_names.iterator | 
| 54528 | 347 | if name == file_name | 
| 348 | } yield cmd).toList | |
| 349 | ||
| 61023 
46df28442a80
clarified undefined_blobs: already loaded theories are suppressed;
 wenzelm parents: 
60933diff
changeset | 350 | def undefined_blobs(pred: Node.Name => Boolean): List[Node.Name] = | 
| 60916 
a6e2a667b0a8
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
 wenzelm parents: 
60215diff
changeset | 351 |       (for {
 | 
| 61023 
46df28442a80
clarified undefined_blobs: already loaded theories are suppressed;
 wenzelm parents: 
60933diff
changeset | 352 | (node_name, node) <- iterator | 
| 
46df28442a80
clarified undefined_blobs: already loaded theories are suppressed;
 wenzelm parents: 
60933diff
changeset | 353 | if pred(node_name) | 
| 60916 
a6e2a667b0a8
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
 wenzelm parents: 
60215diff
changeset | 354 | cmd <- node.load_commands.iterator | 
| 
a6e2a667b0a8
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
 wenzelm parents: 
60215diff
changeset | 355 | name <- cmd.blobs_undefined.iterator | 
| 
a6e2a667b0a8
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
 wenzelm parents: 
60215diff
changeset | 356 | } yield name).toList | 
| 
a6e2a667b0a8
resolve undefined blobs by default, e.g. relevant for ML debugger to avoid reset of breakpoints after reload;
 wenzelm parents: 
60215diff
changeset | 357 | |
| 46942 
f5c2d66faa04
basic support for outer syntax keywords in theory header;
 wenzelm parents: 
46941diff
changeset | 358 | def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names) | 
| 46723 | 359 | def topological_order: List[Node.Name] = graph.topological_order | 
| 56337 | 360 | |
| 361 |     override def toString: String = topological_order.mkString("Nodes(", ",", ")")
 | |
| 46723 | 362 | } | 
| 363 | ||
| 364 | ||
| 38424 | 365 | |
| 366 | /** versioning **/ | |
| 367 | ||
| 368 | /* particular document versions */ | |
| 34485 | 369 | |
| 38417 | 370 | object Version | 
| 371 |   {
 | |
| 46681 | 372 | val init: Version = new Version() | 
| 59077 
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
 wenzelm parents: 
57976diff
changeset | 373 | def make(nodes: Nodes): Version = new Version(Document_ID.make(), nodes) | 
| 38417 | 374 | } | 
| 375 | ||
| 46712 | 376 | final class Version private( | 
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 377 | val id: Document_ID.Version = Document_ID.none, | 
| 46723 | 378 | val nodes: Nodes = Nodes.empty) | 
| 46941 | 379 |   {
 | 
| 55777 | 380 |     override def toString: String = "Version(" + id + ")"
 | 
| 46941 | 381 | } | 
| 34660 | 382 | |
| 34859 | 383 | |
| 38424 | 384 | /* changes of plain text, eventually resulting in document edits */ | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 385 | |
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 386 | object Change | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 387 |   {
 | 
| 46678 | 388 | val init: Change = new Change() | 
| 389 | ||
| 390 | 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 | 391 | new Change(Some(previous), edits.reverse, version) | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 392 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 393 | |
| 46712 | 394 | final class Change private( | 
| 46677 | 395 | 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 | 396 | val rev_edits: List[Edit_Text] = Nil, | 
| 46677 | 397 | val version: Future[Version] = Future.value(Version.init)) | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 398 |   {
 | 
| 44672 | 399 | def is_finished: Boolean = | 
| 400 |       (previous match { case None => true case Some(future) => future.is_finished }) &&
 | |
| 401 | version.is_finished | |
| 402 | ||
| 46678 | 403 | def truncate: Change = new Change(None, Nil, version) | 
| 38227 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 404 | } | 
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 405 | |
| 
6bbb42843b6e
concentrate structural document notions in document.scala;
 wenzelm parents: 
38220diff
changeset | 406 | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 407 | /* history navigation */ | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 408 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 409 | object History | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 410 |   {
 | 
| 46679 | 411 | 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 | 412 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 413 | |
| 46712 | 414 | final class History private( | 
| 46679 | 415 | val undo_list: List[Change] = List(Change.init)) // non-empty list | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 416 |   {
 | 
| 46679 | 417 | def tip: Change = undo_list.head | 
| 418 | 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 | 419 | |
| 46679 | 420 | def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] = | 
| 421 |     {
 | |
| 422 | val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1 | |
| 423 | val (retained, dropped) = undo_list.splitAt(n max retain) | |
| 424 | ||
| 425 |       retained.splitAt(retained.length - 1) match {
 | |
| 426 | case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate))) | |
| 427 | case _ => None | |
| 428 | } | |
| 429 | } | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 430 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 431 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 432 | |
| 55820 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 433 | /* snapshot */ | 
| 38424 | 434 | |
| 52972 | 435 | object Snapshot | 
| 436 |   {
 | |
| 437 | val init = State.init.snapshot() | |
| 438 | } | |
| 439 | ||
| 38424 | 440 | abstract class Snapshot | 
| 441 |   {
 | |
| 44582 | 442 | val state: State | 
| 38424 | 443 | val version: Version | 
| 444 | val is_outdated: Boolean | |
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 445 | |
| 38427 | 446 | def convert(i: Text.Offset): Text.Offset | 
| 52889 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 447 | def revert(i: Text.Offset): Text.Offset | 
| 39177 
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
 wenzelm parents: 
38976diff
changeset | 448 | def convert(range: Text.Range): Text.Range | 
| 
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
 wenzelm parents: 
38976diff
changeset | 449 | def revert(range: Text.Range): Text.Range | 
| 52972 | 450 | |
| 451 | val node_name: Node.Name | |
| 452 | val node: Node | |
| 56314 | 453 | val load_commands: List[Command] | 
| 56176 
0bc9b0ad6287
tuned rendering -- avoid flashing background of aux. files that are disconnected from the document model;
 wenzelm parents: 
55820diff
changeset | 454 | def is_loaded: Boolean | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 455 | |
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 456 | def cumulate[A]( | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 457 | range: Text.Range, | 
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 458 | info: A, | 
| 56743 | 459 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 460 | result: List[Command.State] => (A, Text.Markup) => Option[A], | 
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 461 | status: Boolean = false): List[Text.Info[A]] | 
| 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 462 | |
| 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 463 | def select[A]( | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 464 | range: Text.Range, | 
| 56743 | 465 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 466 | result: List[Command.State] => Text.Markup => Option[A], | 
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 467 | status: Boolean = false): List[Text.Info[A]] | 
| 38424 | 468 | } | 
| 469 | ||
| 55820 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 470 | |
| 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 471 | |
| 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 472 | /** global state -- document structure, execution process, editing history **/ | 
| 
61869776ce1f
tuned signature -- more explicit Document.Elements;
 wenzelm parents: 
55801diff
changeset | 473 | |
| 52563 | 474 | type Assign_Update = | 
| 475 | 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 | 476 | |
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 477 | object State | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 478 |   {
 | 
| 38373 | 479 | class Fail(state: State) extends Exception | 
| 480 | ||
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 481 | object Assignment | 
| 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 482 |     {
 | 
| 46683 | 483 | val init: Assignment = new Assignment() | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 484 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 485 | |
| 46712 | 486 | final class Assignment private( | 
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 487 | val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty, | 
| 46677 | 488 | val is_finished: Boolean = false) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 489 |     {
 | 
| 44477 | 490 |       def check_finished: Assignment = { require(is_finished); this }
 | 
| 47388 
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
 wenzelm parents: 
46997diff
changeset | 491 | def unfinished: Assignment = new Assignment(command_execs, false) | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 492 | |
| 52563 | 493 | def assign(update: Assign_Update): Assignment = | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 494 |       {
 | 
| 38976 
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
 wenzelm parents: 
38885diff
changeset | 495 | require(!is_finished) | 
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 496 | val command_execs1 = | 
| 52563 | 497 |           (command_execs /: update) {
 | 
| 498 | case (res, (command_id, exec_ids)) => | |
| 499 | if (exec_ids.isEmpty) res - command_id | |
| 500 | else res + (command_id -> exec_ids) | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 501 | } | 
| 47388 
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
 wenzelm parents: 
46997diff
changeset | 502 | new Assignment(command_execs1, true) | 
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 503 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 504 | } | 
| 46682 | 505 | |
| 506 | val init: State = | |
| 52563 | 507 | State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2 | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 508 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 509 | |
| 46712 | 510 | 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 | 511 | /*reachable versions*/ | 
| 60215 | 512 | 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 | 513 | /*inlined auxiliary files*/ | 
| 60215 | 514 | blobs: Set[SHA1.Digest] = Set.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 | 515 | /*static markup from define_command*/ | 
| 60215 | 516 | 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 | 517 | /*dynamic markup from execution*/ | 
| 60215 | 518 | 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 | 519 | /*command-exec assignment for each version*/ | 
| 60215 | 520 | 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 | 521 | /*commands with markup produced by other commands (imm_succs)*/ | 
| 60215 | 522 | 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 | 523 | /*explicit (linear) history*/ | 
| 60215 | 524 | 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 | 525 | /*intermediate state between remove_versions/removed_versions*/ | 
| 60215 | 526 | removing_versions: Boolean = false) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 527 |   {
 | 
| 38373 | 528 | 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 | 529 | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 530 | def define_version(version: Version, assignment: State.Assignment): State = | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 531 |     {
 | 
| 38417 | 532 | val id = version.id | 
| 533 | copy(versions = versions + (id -> version), | |
| 44479 
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
 wenzelm parents: 
44477diff
changeset | 534 | assignments = assignments + (id -> assignment.unfinished)) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 535 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 536 | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 537 | 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 | 538 | 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 | 539 | |
| 38373 | 540 | def define_command(command: Command): State = | 
| 541 |     {
 | |
| 542 | val id = command.id | |
| 49414 | 543 | copy(commands = commands + (id -> command.init_state)) | 
| 38373 | 544 | } | 
| 545 | ||
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 546 | def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id) | 
| 44582 | 547 | |
| 52531 | 548 | def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] = | 
| 44582 | 549 |       commands.get(id) orElse execs.get(id) match {
 | 
| 550 | case None => None | |
| 551 | case Some(st) => | |
| 552 | val command = st.command | |
| 46681 | 553 | val node = version.nodes(command.node_name) | 
| 54328 
ffa4e0b1701e
more strict find_command -- avoid invalid hyperlink_command;
 wenzelm parents: 
52978diff
changeset | 554 | if (node.commands.contains(command)) Some((node, command)) else None | 
| 44582 | 555 | } | 
| 38373 | 556 | |
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 557 | 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 | 558 | 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 | 559 | def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail) | 
| 52563 | 560 | 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 | 561 | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 562 | 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 | 563 | id == st.command.id || | 
| 
a40e67ce4f84
clarified valid_id: always standardize towards static command.id;
 wenzelm parents: 
56176diff
changeset | 564 |       (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 | 565 | |
| 56746 | 566 | private def other_id(id: Document_ID.Generic) | 
| 567 | : Option[(Symbol.Text_Chunk.Id, Symbol.Text_Chunk)] = None | |
| 56514 
db929027e701
ignore other_id reports for now (see 8eda56033203): massive amounts of redirections to 'class' etc. makes it difficult to edit main HOL;
 wenzelm parents: 
56489diff
changeset | 568 | /* FIXME | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 569 | (execs.get(id) orElse commands.get(id)).map(st => | 
| 56746 | 570 | ((Symbol.Text_Chunk.Id(st.command.id), st.command.chunk))) | 
| 56514 
db929027e701
ignore other_id reports for now (see 8eda56033203): massive amounts of redirections to 'class' etc. makes it difficult to edit main HOL;
 wenzelm parents: 
56489diff
changeset | 571 | */ | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 572 | |
| 56475 | 573 | private def redirection(st: Command.State): Graph[Document_ID.Command, Unit] = | 
| 574 |       (commands_redirection /: st.markups.redirection_iterator)({ case (graph, 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 | 575 | graph.default_node(id, ()).default_node(st.command.id, ()).add_edge(id, st.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 | 576 | |
| 52531 | 577 | def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, 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 | 578 |     {
 | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 579 |       execs.get(id) match {
 | 
| 44446 | 580 | case Some(st) => | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 581 | val new_st = st.accumulate(self_id(st), other_id _, message) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 582 | val execs1 = execs + (id -> new_st) | 
| 56475 | 583 | (new_st, copy(execs = execs1, commands_redirection = redirection(new_st))) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 584 | case None => | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 585 |           commands.get(id) match {
 | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 586 | case Some(st) => | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 587 | val new_st = st.accumulate(self_id(st), other_id _, message) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 588 | val commands1 = commands + (id -> new_st) | 
| 56475 | 589 | (new_st, copy(commands = commands1, commands_redirection = redirection(new_st))) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 590 | case None => fail | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 591 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 592 | } | 
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 593 | } | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 594 | |
| 52563 | 595 | def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) = | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 596 |     {
 | 
| 38417 | 597 | val version = the_version(id) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 598 | |
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 599 | def upd(exec_id: Document_ID.Exec, st: Command.State) | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 600 | : Option[(Document_ID.Exec, Command.State)] = | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 601 | if (execs.isDefinedAt(exec_id)) None else Some(exec_id -> st) | 
| 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 602 | |
| 44543 | 603 | val (changed_commands, new_execs) = | 
| 52563 | 604 |         ((Nil: List[Command], execs) /: update) {
 | 
| 52566 
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
 wenzelm parents: 
52564diff
changeset | 605 | 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 | 606 | 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 | 607 | val command = st.command | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 608 | val commands2 = command :: commands1 | 
| 44543 | 609 | val execs2 = | 
| 610 |               exec match {
 | |
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 611 | case Nil => execs1 | 
| 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 612 | 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 | 613 | 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 | 614 | (for (id <- print_ids; up <- upd(id, command.empty_state)) yield up) | 
| 44543 | 615 | } | 
| 616 | (commands2, execs2) | |
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 617 | } | 
| 52563 | 618 | val new_assignment = the_assignment(version).assign(update) | 
| 43722 | 619 | 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 | 620 | |
| 44543 | 621 | (changed_commands, new_state) | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 622 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 623 | |
| 38417 | 624 | def is_assigned(version: Version): Boolean = | 
| 43722 | 625 |       assignments.get(version.id) match {
 | 
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 626 | case Some(assgn) => assgn.is_finished | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 627 | case None => false | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 628 | } | 
| 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 629 | |
| 44436 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 630 | def is_stable(change: Change): Boolean = | 
| 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 631 | 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 | 632 | |
| 46944 
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
 wenzelm parents: 
46942diff
changeset | 633 | def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail | 
| 44672 | 634 | 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 | 635 | 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 | 636 | 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 | 637 | |
| 44446 | 638 | def continue_history( | 
| 56711 | 639 | previous: Future[Version], | 
| 640 | edits: List[Edit_Text], | |
| 641 | version: Future[Version]): State = | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 642 |     {
 | 
| 46678 | 643 | val change = Change.make(previous, edits, version) | 
| 56711 | 644 | 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 | 645 | } | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 646 | |
| 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 | 647 | def remove_versions(retain: Int = 0): (List[Version], State) = | 
| 44672 | 648 |     {
 | 
| 46679 | 649 |       history.prune(is_stable, retain) match {
 | 
| 650 | 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 | 651 | val old_versions = dropped.map(change => change.version.get_finished) | 
| 59319 | 652 | 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 | 653 | 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 | 654 | (old_versions, state1) | 
| 46679 | 655 | case None => fail | 
| 44672 | 656 | } | 
| 657 | } | |
| 658 | ||
| 52530 
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
 wenzelm parents: 
52527diff
changeset | 659 | def removed_versions(removed: List[Document_ID.Version]): State = | 
| 44676 | 660 |     {
 | 
| 661 | val versions1 = versions -- removed | |
| 662 | val assignments1 = assignments -- removed | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 663 | 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 | 664 | 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 | 665 | var execs1 = Map.empty[Document_ID.Exec, Command.State] | 
| 44676 | 666 |       for {
 | 
| 667 | (version_id, version) <- versions1.iterator | |
| 46997 | 668 | command_execs = assignments1(version_id).command_execs | 
| 56372 
fadb0fef09d7
more explicit iterator terminology, in accordance to Scala 2.8 library;
 wenzelm parents: 
56354diff
changeset | 669 | (_, node) <- version.nodes.iterator | 
| 44676 | 670 | command <- node.commands.iterator | 
| 671 |       } {
 | |
| 57842 
8e4ae2db1849
more direct access to persistent blobs (see also 8953d4cc060a), avoiding fragile digest lookup from later version (which might have removed unused blobs already);
 wenzelm parents: 
57621diff
changeset | 672 | for ((_, digest) <- command.blobs_defined; if !blobs1.contains(digest)) | 
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 673 | blobs1 += digest | 
| 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 674 | |
| 52568 | 675 | if (!commands1.isDefinedAt(command.id)) | 
| 676 | commands.get(command.id).foreach(st => commands1 += (command.id -> st)) | |
| 54519 
5fed81762406
maintain blobs within document state: digest + text in ML, digest-only in Scala;
 wenzelm parents: 
54515diff
changeset | 677 | |
| 52568 | 678 |         for (exec_id <- command_execs.getOrElse(command.id, Nil)) {
 | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 679 | if (!execs1.isDefinedAt(exec_id)) | 
| 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 680 | execs.get(exec_id).foreach(st => execs1 += (exec_id -> st)) | 
| 44676 | 681 | } | 
| 682 | } | |
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 683 | copy( | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 684 | versions = versions1, | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 685 | 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 | 686 | 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 | 687 | execs = execs1, | 
| 56475 | 688 | commands_redirection = commands_redirection.restrict(commands1.isDefinedAt(_)), | 
| 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 | 689 | assignments = assignments1, | 
| 
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 | 690 | removing_versions = false) | 
| 44676 | 691 | } | 
| 692 | ||
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 693 | private def command_states_self(version: Version, command: Command) | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 694 | : List[(Document_ID.Generic, Command.State)] = | 
| 44613 | 695 |     {
 | 
| 696 | require(is_assigned(version)) | |
| 697 |       try {
 | |
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 698 | 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 | 699 |           .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 | 700 | 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 | 701 | case res => res | 
| 52527 
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
 wenzelm parents: 
52508diff
changeset | 702 | } | 
| 44613 | 703 | } | 
| 47395 
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
 wenzelm parents: 
47388diff
changeset | 704 |       catch {
 | 
| 
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
 wenzelm parents: 
47388diff
changeset | 705 | 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 | 706 |           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 | 707 |           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 | 708 | } | 
| 44613 | 709 | } | 
| 710 | ||
| 56470 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 711 | def command_states(version: Version, command: Command): List[Command.State] = | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 712 |     {
 | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 713 | 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 | 714 | val others = | 
| 56475 | 715 |         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 | 716 |           (for {
 | 
| 56475 | 717 | 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 | 718 | (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 | 719 | 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 | 720 | } 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 | 721 | } | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 722 | else Nil | 
| 56489 | 723 | 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 | 724 | } | 
| 
8eda56033203
accumulate markup reports for "other" command ids, which are later retargeted and merged for rendering (in erratic order);
 wenzelm parents: 
56468diff
changeset | 725 | |
| 56299 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 726 | 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 | 727 | 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 | 728 | |
| 56301 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 729 | def command_markup(version: Version, command: Command, index: Command.Markup_Index, | 
| 56743 | 730 | range: Text.Range, elements: Markup.Elements): Markup_Tree = | 
| 56301 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 731 | 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 | 732 | |
| 56743 | 733 | def markup_to_XML(version: Version, node: Node, elements: Markup.Elements): XML.Body = | 
| 56299 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 734 |       (for {
 | 
| 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 735 | command <- node.commands.iterator | 
| 56301 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 736 | markup = | 
| 
1da7b4c33db9
more frugal merge of markup trees: filter wrt. subsequent query;
 wenzelm parents: 
56300diff
changeset | 737 | command_markup(version, command, Command.Markup_Index.markup, command.range, elements) | 
| 56300 | 738 | tree <- markup.to_XML(command.range, command.source, elements) | 
| 56299 
8201790fdeb9
more careful treatment of multiple command states (eval + prints): merge content that is actually required;
 wenzelm parents: 
56298diff
changeset | 739 | } yield tree).toList | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 740 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 741 | // persistent user-view | 
| 49346 | 742 | def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil) | 
| 743 | : Snapshot = | |
| 38370 
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
 wenzelm parents: 
38367diff
changeset | 744 |     {
 | 
| 44672 | 745 | val stable = recent_stable | 
| 44436 
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
 wenzelm parents: 
44385diff
changeset | 746 | val latest = history.tip | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 747 | |
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 748 | |
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 749 | /* pending edits and unstable changes */ | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 750 | |
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 751 | val rev_pending_changes = | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 752 |         for {
 | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 753 | change <- history.undo_list.takeWhile(_ != stable) | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 754 | (a, edits) <- change.rev_edits | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 755 | if a == name | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 756 | } yield edits | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 757 | |
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 758 |       val is_cleared = rev_pending_changes.exists({ case Node.Clear() => true case _ => false })
 | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 759 | val edits = | 
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 760 | if (is_cleared) Nil | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 761 | else | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 762 |           (pending_edits /: rev_pending_changes)({
 | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 763 | case (edits, Node.Edits(es)) => es ::: edits | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 764 | case (edits, _) => edits | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 765 | }) | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 766 | lazy val reverse_edits = edits.reverse | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 767 | |
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 768 | new Snapshot | 
| 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 769 |       {
 | 
| 52972 | 770 | /* global information */ | 
| 771 | ||
| 44582 | 772 | val state = State.this | 
| 39698 | 773 | val version = stable.version.get_finished | 
| 59319 | 774 | val is_outdated = pending_edits.nonEmpty || latest != stable | 
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 775 | |
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 776 | |
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 777 | /* local node content */ | 
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 778 | |
| 57620 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 779 | def convert(offset: Text.Offset) = | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 780 | if (is_cleared) 0 else (offset /: edits)((i, edit) => edit.convert(i)) | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 781 | def revert(offset: Text.Offset) = | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 782 | if (is_cleared) 0 else (offset /: reverse_edits)((i, edit) => edit.revert(i)) | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 783 | |
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 784 | def convert(range: Text.Range) = range.map(convert(_)) | 
| 
c30ab960875e
more explicit treatment of cleared nodes (removal is implicit);
 wenzelm parents: 
57619diff
changeset | 785 | def revert(range: Text.Range) = range.map(revert(_)) | 
| 39177 
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
 wenzelm parents: 
38976diff
changeset | 786 | |
| 52972 | 787 | val node_name = name | 
| 788 | val node = version.nodes(name) | |
| 789 | ||
| 56314 | 790 | val load_commands: List[Command] = | 
| 55432 
9c53198dbb1c
maintain multiple command chunks and markup trees: for main chunk and loaded files;
 wenzelm parents: 
55431diff
changeset | 791 | if (node_name.is_theory) Nil | 
| 56314 | 792 | else version.nodes.load_commands(node_name) | 
| 55432 
9c53198dbb1c
maintain multiple command chunks and markup trees: for main chunk and loaded files;
 wenzelm parents: 
55431diff
changeset | 793 | |
| 59319 | 794 | val is_loaded: Boolean = node_name.is_theory || load_commands.nonEmpty | 
| 56176 
0bc9b0ad6287
tuned rendering -- avoid flashing background of aux. files that are disconnected from the document model;
 wenzelm parents: 
55820diff
changeset | 795 | |
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 796 | |
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 797 | /* cumulate markup */ | 
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 798 | |
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 799 | def cumulate[A]( | 
| 55649 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 800 | range: Text.Range, | 
| 
1532ab0dc67b
more general / abstract Command.Markups, with separate index for status elements;
 wenzelm parents: 
55620diff
changeset | 801 | info: A, | 
| 56743 | 802 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 803 | result: List[Command.State] => (A, Text.Markup) => Option[A], | 
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 804 | status: Boolean = false): List[Text.Info[A]] = | 
| 45459 | 805 |         {
 | 
| 56590 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 806 | val former_range = revert(range).inflate_singularity | 
| 56462 
b64b0cb845fe
more explicit Command.Chunk types, less ooddities;
 wenzelm parents: 
56398diff
changeset | 807 | val (chunk_name, command_iterator) = | 
| 56314 | 808 |             load_commands match {
 | 
| 56746 | 809 | case command :: _ => (Symbol.Text_Chunk.File(node_name.node), Iterator((command, 0))) | 
| 810 | case _ => (Symbol.Text_Chunk.Default, node.command_iterator(former_range)) | |
| 56309 | 811 | } | 
| 56462 
b64b0cb845fe
more explicit Command.Chunk types, less ooddities;
 wenzelm parents: 
56398diff
changeset | 812 | val markup_index = Command.Markup_Index(status, chunk_name) | 
| 56309 | 813 |           (for {
 | 
| 56373 
0605d90be6fc
tuned signature -- more explicit iterator terminology;
 wenzelm parents: 
56372diff
changeset | 814 | (command, command_start) <- command_iterator | 
| 56462 
b64b0cb845fe
more explicit Command.Chunk types, less ooddities;
 wenzelm parents: 
56398diff
changeset | 815 | chunk <- command.chunks.get(chunk_name).iterator | 
| 56309 | 816 | states = state.command_states(version, command) | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 817 | res = result(states) | 
| 56590 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 818 | markup_range <- (former_range - command_start).try_restrict(chunk.range).iterator | 
| 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 819 | markup = Command.State.merge_markup(states, markup_index, markup_range, elements) | 
| 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 820 | Text.Info(r0, a) <- markup.cumulate[A](markup_range, info, elements, | 
| 56309 | 821 |               {
 | 
| 822 | case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b)) | |
| 823 | }).iterator | |
| 56590 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 824 | r1 <- convert(r0 + command_start).try_restrict(range).iterator | 
| 
d01d183e84ea
clarified treatment of markup ranges wrt. revert/convert: inflate_singularity allows to retrieve information like language_context more reliably during editing;
 wenzelm parents: 
56514diff
changeset | 825 | } yield Text.Info(r1, a)).toList | 
| 45459 | 826 | } | 
| 827 | ||
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 828 | def select[A]( | 
| 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 829 | range: Text.Range, | 
| 56743 | 830 | elements: Markup.Elements, | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 831 | result: List[Command.State] => Text.Markup => Option[A], | 
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 832 | status: Boolean = false): List[Text.Info[A]] = | 
| 38845 
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
 wenzelm parents: 
38841diff
changeset | 833 |         {
 | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 834 | def result1(states: List[Command.State]): (Option[A], Text.Markup) => Option[Option[A]] = | 
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50204diff
changeset | 835 |           {
 | 
| 56354 
a6f8c3566560
more direct command states -- merge_results is hardly ever needed;
 wenzelm parents: 
56337diff
changeset | 836 | val res = result(states) | 
| 52889 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 837 | (_: Option[A], x: Text.Markup) => | 
| 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 838 |               res(x) match {
 | 
| 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 839 | case None => None | 
| 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 840 | case some => Some(some) | 
| 
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
 wenzelm parents: 
52887diff
changeset | 841 | } | 
| 50500 
c94bba7906d2
identify dialogs via official serial and maintain as result message;
 wenzelm parents: 
50204diff
changeset | 842 | } | 
| 55651 
fa42cf3fe79b
tuned signature -- avoid redundancy and confusion of flags;
 wenzelm parents: 
55650diff
changeset | 843 | for (Text.Info(r, Some(x)) <- cumulate(range, None, elements, result1 _, status)) | 
| 46198 
cd040c5772de
improved select_markup: include filtering of defined results;
 wenzelm parents: 
46178diff
changeset | 844 | yield Text.Info(r, x) | 
| 38845 
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
 wenzelm parents: 
38841diff
changeset | 845 | } | 
| 55800 | 846 | |
| 847 | ||
| 848 | /* output */ | |
| 849 | ||
| 850 | override def toString: String = | |
| 851 | "Snapshot(node = " + node_name.node + ", version = " + version.id + | |
| 852 | (if (is_outdated) ", outdated" else "") + ")" | |
| 38841 
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
 wenzelm parents: 
38569diff
changeset | 853 | } | 
| 38150 
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
 wenzelm parents: 
37849diff
changeset | 854 | } | 
| 34485 | 855 | } | 
| 34840 
6c5560d48561
more precise treatment of document/state assigment;
 wenzelm parents: 
34838diff
changeset | 856 | } | 
| 
6c5560d48561
more precise treatment of document/state assigment;
 wenzelm parents: 
34838diff
changeset | 857 |