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