author | wenzelm |
Wed, 07 Aug 2013 13:46:32 +0200 | |
changeset 52889 | ea3338812e67 |
parent 52887 | 98eac7b7eec3 |
child 52900 | d29bf6db8a2d |
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 |
||
34823 | 14 |
object Document |
34483 | 15 |
{ |
38424 | 16 |
/** document structure **/ |
17 |
||
46723 | 18 |
/* individual nodes */ |
38424 | 19 |
|
44615 | 20 |
type Edit[A, B] = (Node.Name, Node.Edit[A, B]) |
44384 | 21 |
type Edit_Text = Edit[Text.Edit, Text.Perspective] |
52849 | 22 |
type Edit_Command = Edit[Command.Edit, Command.Perspective] |
38151 | 23 |
|
24 |
object Node |
|
25 |
{ |
|
52887 | 26 |
val empty: Node = new Node() |
27 |
||
28 |
||
29 |
/* header and name */ |
|
30 |
||
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
31 |
sealed case class Header( |
46938
cda018294515
some support for outer syntax keyword declarations within theory header;
wenzelm
parents:
46814
diff
changeset
|
32 |
imports: List[Name], |
48706 | 33 |
keywords: Thy_Header.Keywords, |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
34 |
errors: List[String] = Nil) |
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
35 |
{ |
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
36 |
def error(msg: String): Header = copy(errors = errors ::: List(msg)) |
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
37 |
} |
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
38 |
|
51294
0850d43cb355
discontinued obsolete header "files" -- these are loaded explicitly after exploring dependencies;
wenzelm
parents:
51293
diff
changeset
|
39 |
def bad_header(msg: String): Header = Header(Nil, Nil, List(msg)) |
46737 | 40 |
|
44957
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents:
44676
diff
changeset
|
41 |
object Name |
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents:
44676
diff
changeset
|
42 |
{ |
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents:
44676
diff
changeset
|
43 |
val empty = Name("", "", "") |
46723 | 44 |
|
45 |
object Ordering extends scala.math.Ordering[Name] |
|
46 |
{ |
|
47 |
def compare(name1: Name, name2: Name): Int = name1.node compare name2.node |
|
48 |
} |
|
44957
098dd95349e7
more elaborate Node_Renderer, which paints node_name.theory only;
wenzelm
parents:
44676
diff
changeset
|
49 |
} |
52887 | 50 |
|
44616 | 51 |
sealed case class Name(node: String, dir: String, theory: String) |
44615 | 52 |
{ |
53 |
override def hashCode: Int = node.hashCode |
|
54 |
override def equals(that: Any): Boolean = |
|
55 |
that match { |
|
56 |
case other: Name => node == other.node |
|
57 |
case _ => false |
|
58 |
} |
|
44642 | 59 |
override def toString: String = theory |
44615 | 60 |
} |
61 |
||
52887 | 62 |
|
63 |
/* overlays -- print functions with arguments */ |
|
64 |
||
65 |
type Overlay = (Command, (String, List[String])) |
|
66 |
||
67 |
object Overlays |
|
68 |
{ |
|
69 |
val empty = new Overlays(Map.empty) |
|
70 |
} |
|
71 |
||
72 |
final class Overlays private(rep: Map[Command, List[(String, List[String])]]) |
|
73 |
{ |
|
74 |
def commands: Set[Command] = rep.keySet |
|
75 |
def is_empty: Boolean = rep.isEmpty |
|
76 |
||
77 |
def insert(cmd: Command, over: (String, List[String])): Overlays = |
|
78 |
{ |
|
79 |
val overs = rep.getOrElse(cmd, Nil) |
|
80 |
if (overs.contains(over)) this |
|
81 |
else new Overlays(rep + (cmd -> (over :: overs))) |
|
82 |
} |
|
83 |
||
84 |
def remove(cmd: Command, over: (String, List[String])): Overlays = |
|
85 |
rep.get(cmd) match { |
|
86 |
case Some(overs) => |
|
87 |
if (overs.contains(over)) { |
|
88 |
overs.filterNot(_ == over) match { |
|
89 |
case Nil => new Overlays(rep - cmd) |
|
90 |
case overs1 => new Overlays(rep + (cmd -> overs1)) |
|
91 |
} |
|
92 |
} |
|
93 |
else this |
|
94 |
case None => this |
|
95 |
} |
|
96 |
||
97 |
def dest: List[Overlay] = |
|
98 |
(for { |
|
99 |
(cmd, overs) <- rep.iterator |
|
100 |
over <- overs |
|
101 |
} yield (cmd, over)).toList |
|
102 |
} |
|
103 |
||
104 |
||
105 |
/* edits */ |
|
106 |
||
44384 | 107 |
sealed abstract class Edit[A, B] |
44156 | 108 |
{ |
44383 | 109 |
def foreach(f: A => Unit) |
110 |
{ |
|
44156 | 111 |
this match { |
44383 | 112 |
case Edits(es) => es.foreach(f) |
113 |
case _ => |
|
44156 | 114 |
} |
44383 | 115 |
} |
44156 | 116 |
} |
44384 | 117 |
case class Clear[A, B]() extends Edit[A, B] |
118 |
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
|
119 |
case class Deps[A, B](header: Header) extends Edit[A, B] |
52849 | 120 |
case class Perspective[A, B](required: Boolean, visible: B, overlays: Overlays) extends Edit[A, B] |
52808
143f225e50f5
allow explicit indication of required node: full eval, no prints;
wenzelm
parents:
52568
diff
changeset
|
121 |
type Perspective_Text = Perspective[Text.Edit, Text.Perspective] |
52849 | 122 |
type Perspective_Command = Perspective[Command.Edit, Command.Perspective] |
44156 | 123 |
|
52887 | 124 |
|
125 |
/* commands */ |
|
126 |
||
38426 | 127 |
def command_starts(commands: Iterator[Command], offset: Text.Offset = 0) |
128 |
: Iterator[(Command, Text.Offset)] = |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
129 |
{ |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
130 |
var i = offset |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
131 |
for (command <- commands) yield { |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
132 |
val start = i |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
133 |
i += command.length |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
134 |
(command, start) |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
135 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
136 |
} |
46680 | 137 |
|
138 |
private val block_size = 1024 |
|
38151 | 139 |
} |
140 |
||
46712 | 141 |
final class Node private( |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
142 |
val header: Node.Header = Node.bad_header("Bad theory header"), |
52849 | 143 |
val perspective: Node.Perspective_Command = |
52887 | 144 |
Node.Perspective(false, Command.Perspective.empty, Node.Overlays.empty), |
46677 | 145 |
val commands: Linear_Set[Command] = Linear_Set.empty) |
38151 | 146 |
{ |
46680 | 147 |
def clear: Node = new Node(header = header) |
148 |
||
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
149 |
def update_header(new_header: Node.Header): Node = |
49678 | 150 |
new Node(new_header, perspective, commands) |
46680 | 151 |
|
52849 | 152 |
def update_perspective(new_perspective: Node.Perspective_Command): Node = |
49678 | 153 |
new Node(header, new_perspective, commands) |
46680 | 154 |
|
52849 | 155 |
def same_perspective(other_perspective: Node.Perspective_Command): Boolean = |
156 |
perspective.required == other_perspective.required && |
|
157 |
perspective.visible.same(other_perspective.visible) && |
|
158 |
perspective.overlays == other_perspective.overlays |
|
52808
143f225e50f5
allow explicit indication of required node: full eval, no prints;
wenzelm
parents:
52568
diff
changeset
|
159 |
|
46680 | 160 |
def update_commands(new_commands: Linear_Set[Command]): Node = |
49678 | 161 |
new Node(header, perspective, new_commands) |
44443
35d67b2056cc
clarified Document.Node.clear -- retain header (cf. ML version);
wenzelm
parents:
44442
diff
changeset
|
162 |
|
35d67b2056cc
clarified Document.Node.clear -- retain header (cf. ML version);
wenzelm
parents:
44442
diff
changeset
|
163 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
164 |
/* commands */ |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
165 |
|
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
166 |
private lazy val full_index: (Array[(Command, Text.Offset)], Text.Range) = |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
167 |
{ |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
168 |
val blocks = new mutable.ListBuffer[(Command, Text.Offset)] |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
169 |
var next_block = 0 |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
170 |
var last_stop = 0 |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
171 |
for ((command, start) <- Node.command_starts(commands.iterator)) { |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
172 |
last_stop = start + command.length |
38885
06582837872b
Node.full_index: allow command spans larger than block_size;
wenzelm
parents:
38882
diff
changeset
|
173 |
while (last_stop + 1 > next_block) { |
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
174 |
blocks += (command -> start) |
46680 | 175 |
next_block += Node.block_size |
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
176 |
} |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
177 |
} |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
178 |
(blocks.toArray, Text.Range(0, last_stop)) |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
179 |
} |
38151 | 180 |
|
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
181 |
def full_range: Text.Range = full_index._2 |
38151 | 182 |
|
38569 | 183 |
def command_range(i: Text.Offset = 0): Iterator[(Command, Text.Offset)] = |
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
184 |
{ |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
185 |
if (!commands.isEmpty && full_range.contains(i)) { |
46680 | 186 |
val (cmd0, start0) = full_index._1(i / Node.block_size) |
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
187 |
Node.command_starts(commands.iterator(cmd0), start0) dropWhile { |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
188 |
case (cmd, start) => start + cmd.length <= i } |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
189 |
} |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
190 |
else Iterator.empty |
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
191 |
} |
38151 | 192 |
|
38569 | 193 |
def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] = |
194 |
command_range(range.start) takeWhile { case (_, start) => start < range.stop } |
|
38151 | 195 |
|
38426 | 196 |
def command_at(i: Text.Offset): Option[(Command, Text.Offset)] = |
38151 | 197 |
{ |
198 |
val range = command_range(i) |
|
199 |
if (range.hasNext) Some(range.next) else None |
|
200 |
} |
|
201 |
||
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
202 |
def command_start(cmd: Command): Option[Text.Offset] = |
46677 | 203 |
Node.command_starts(commands.iterator).find(_._1 == cmd).map(_._2) |
38151 | 204 |
} |
205 |
||
206 |
||
46723 | 207 |
/* development graph */ |
208 |
||
209 |
object Nodes |
|
210 |
{ |
|
211 |
val empty: Nodes = new Nodes(Graph.empty(Node.Name.Ordering)) |
|
212 |
} |
|
213 |
||
214 |
final class Nodes private(graph: Graph[Node.Name, Node]) |
|
215 |
{ |
|
216 |
def get_name(s: String): Option[Node.Name] = |
|
217 |
graph.keys.find(name => name.node == s) |
|
218 |
||
219 |
def apply(name: Node.Name): Node = |
|
220 |
graph.default_node(name, Node.empty).get_node(name) |
|
221 |
||
222 |
def + (entry: (Node.Name, Node)): Nodes = |
|
223 |
{ |
|
224 |
val (name, node) = entry |
|
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
225 |
val imports = node.header.imports |
46723 | 226 |
val graph1 = |
46739
6024353549ca
clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
wenzelm
parents:
46737
diff
changeset
|
227 |
(graph.default_node(name, Node.empty) /: imports)((g, p) => g.default_node(p, Node.empty)) |
46723 | 228 |
val graph2 = (graph1 /: graph1.imm_preds(name))((g, dep) => g.del_edge(dep, name)) |
46739
6024353549ca
clarified document nodes (full import graph) vs. node_status (non-preloaded theories);
wenzelm
parents:
46737
diff
changeset
|
229 |
val graph3 = (graph2 /: imports)((g, dep) => g.add_edge(dep, name)) |
46723 | 230 |
new Nodes(graph3.map_node(name, _ => node)) |
231 |
} |
|
232 |
||
233 |
def entries: Iterator[(Node.Name, Node)] = |
|
234 |
graph.entries.map({ case (name, (node, _)) => (name, node) }) |
|
235 |
||
46942
f5c2d66faa04
basic support for outer syntax keywords in theory header;
wenzelm
parents:
46941
diff
changeset
|
236 |
def descendants(names: List[Node.Name]): List[Node.Name] = graph.all_succs(names) |
46723 | 237 |
def topological_order: List[Node.Name] = graph.topological_order |
238 |
} |
|
239 |
||
240 |
||
38424 | 241 |
|
242 |
/** versioning **/ |
|
243 |
||
244 |
/* particular document versions */ |
|
34485 | 245 |
|
38417 | 246 |
object Version |
247 |
{ |
|
46681 | 248 |
val init: Version = new Version() |
249 |
||
46941 | 250 |
def make(syntax: Outer_Syntax, nodes: Nodes): Version = |
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
251 |
new Version(Document_ID.make(), syntax, nodes) |
38417 | 252 |
} |
253 |
||
46712 | 254 |
final class Version private( |
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
255 |
val id: Document_ID.Version = Document_ID.none, |
46941 | 256 |
val syntax: Outer_Syntax = Outer_Syntax.empty, |
46723 | 257 |
val nodes: Nodes = Nodes.empty) |
46941 | 258 |
{ |
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
259 |
def is_init: Boolean = id == Document_ID.none |
46941 | 260 |
} |
34660 | 261 |
|
34859 | 262 |
|
38424 | 263 |
/* changes of plain text, eventually resulting in document edits */ |
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 |
object Change |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
266 |
{ |
46678 | 267 |
val init: Change = new Change() |
268 |
||
269 |
def make(previous: Future[Version], edits: List[Edit_Text], version: Future[Version]): Change = |
|
270 |
new Change(Some(previous), edits, version) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
271 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
272 |
|
46712 | 273 |
final class Change private( |
46677 | 274 |
val previous: Option[Future[Version]] = Some(Future.value(Version.init)), |
275 |
val edits: List[Edit_Text] = Nil, |
|
276 |
val version: Future[Version] = Future.value(Version.init)) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
277 |
{ |
44672 | 278 |
def is_finished: Boolean = |
279 |
(previous match { case None => true case Some(future) => future.is_finished }) && |
|
280 |
version.is_finished |
|
281 |
||
46678 | 282 |
def truncate: Change = new Change(None, Nil, version) |
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
283 |
} |
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
284 |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
285 |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
286 |
/* history navigation */ |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
287 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
288 |
object History |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
289 |
{ |
46679 | 290 |
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
|
291 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
292 |
|
46712 | 293 |
final class History private( |
46679 | 294 |
val undo_list: List[Change] = List(Change.init)) // non-empty list |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
295 |
{ |
46679 | 296 |
def tip: Change = undo_list.head |
297 |
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
|
298 |
|
46679 | 299 |
def prune(check: Change => Boolean, retain: Int): Option[(List[Change], History)] = |
300 |
{ |
|
301 |
val n = undo_list.iterator.zipWithIndex.find(p => check(p._1)).get._2 + 1 |
|
302 |
val (retained, dropped) = undo_list.splitAt(n max retain) |
|
303 |
||
304 |
retained.splitAt(retained.length - 1) match { |
|
305 |
case (prefix, List(last)) => Some(dropped, new History(prefix ::: List(last.truncate))) |
|
306 |
case _ => None |
|
307 |
} |
|
308 |
} |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
309 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
310 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
311 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
312 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
313 |
/** global state -- document structure, execution process, editing history **/ |
38424 | 314 |
|
315 |
abstract class Snapshot |
|
316 |
{ |
|
44582 | 317 |
val state: State |
38424 | 318 |
val version: Version |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48793
diff
changeset
|
319 |
val node_name: Node.Name |
38424 | 320 |
val node: Node |
321 |
val is_outdated: Boolean |
|
38427 | 322 |
def convert(i: Text.Offset): Text.Offset |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
323 |
def revert(i: Text.Offset): Text.Offset |
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
324 |
def convert(range: Text.Range): Text.Range |
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
325 |
def revert(range: Text.Range): Text.Range |
51494 | 326 |
def eq_content(other: Snapshot): Boolean |
52508 | 327 |
def cumulate_markup[A]( |
328 |
range: Text.Range, |
|
329 |
info: A, |
|
330 |
elements: Option[Set[String]], |
|
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
331 |
result: Command.State => (A, Text.Markup) => Option[A]): Stream[Text.Info[A]] |
52508 | 332 |
def select_markup[A]( |
333 |
range: Text.Range, |
|
334 |
elements: Option[Set[String]], |
|
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
335 |
result: Command.State => Text.Markup => Option[A]): Stream[Text.Info[A]] |
38424 | 336 |
} |
337 |
||
52563 | 338 |
type Assign_Update = |
339 |
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
|
340 |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
341 |
object State |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
342 |
{ |
38373 | 343 |
class Fail(state: State) extends Exception |
344 |
||
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
345 |
object Assignment |
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
346 |
{ |
46683 | 347 |
val init: Assignment = new Assignment() |
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
348 |
} |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
349 |
|
46712 | 350 |
final class Assignment private( |
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
351 |
val command_execs: Map[Document_ID.Command, List[Document_ID.Exec]] = Map.empty, |
46677 | 352 |
val is_finished: Boolean = false) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
353 |
{ |
44477 | 354 |
def check_finished: Assignment = { require(is_finished); this } |
47388
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
wenzelm
parents:
46997
diff
changeset
|
355 |
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
|
356 |
|
52563 | 357 |
def assign(update: Assign_Update): Assignment = |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
358 |
{ |
38976
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents:
38885
diff
changeset
|
359 |
require(!is_finished) |
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
360 |
val command_execs1 = |
52563 | 361 |
(command_execs /: update) { |
362 |
case (res, (command_id, exec_ids)) => |
|
363 |
if (exec_ids.isEmpty) res - command_id |
|
364 |
else res + (command_id -> exec_ids) |
|
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
365 |
} |
47388
fe4b245af74c
discontinued obsolete last_execs (cf. cd3ab7625519);
wenzelm
parents:
46997
diff
changeset
|
366 |
new Assignment(command_execs1, true) |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
367 |
} |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
368 |
} |
46682 | 369 |
|
370 |
val init: State = |
|
52563 | 371 |
State().define_version(Version.init, Assignment.init).assign(Version.init.id, Nil)._2 |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
372 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
373 |
|
46712 | 374 |
final case class State private( |
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
375 |
val versions: Map[Document_ID.Version, Version] = Map.empty, |
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
376 |
val commands: Map[Document_ID.Command, Command.State] = Map.empty, // static markup from define_command |
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
377 |
val execs: Map[Document_ID.Exec, Command.State] = Map.empty, // dynamic markup from execution |
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
378 |
val assignments: Map[Document_ID.Version, State.Assignment] = Map.empty, |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
379 |
val history: History = History.init) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
380 |
{ |
38373 | 381 |
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
|
382 |
|
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
383 |
def define_version(version: Version, assignment: State.Assignment): State = |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
384 |
{ |
38417 | 385 |
val id = version.id |
386 |
copy(versions = versions + (id -> version), |
|
44479
9a04e7502e22
refined document state assignment: observe perspective, more explicit assignment message;
wenzelm
parents:
44477
diff
changeset
|
387 |
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
|
388 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
389 |
|
38373 | 390 |
def define_command(command: Command): State = |
391 |
{ |
|
392 |
val id = command.id |
|
49414 | 393 |
copy(commands = commands + (id -> command.init_state)) |
38373 | 394 |
} |
395 |
||
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
396 |
def defined_command(id: Document_ID.Command): Boolean = commands.isDefinedAt(id) |
44582 | 397 |
|
52531 | 398 |
def find_command(version: Version, id: Document_ID.Generic): Option[(Node, Command)] = |
44582 | 399 |
commands.get(id) orElse execs.get(id) match { |
400 |
case None => None |
|
401 |
case Some(st) => |
|
402 |
val command = st.command |
|
46681 | 403 |
val node = version.nodes(command.node_name) |
404 |
Some((node, command)) |
|
44582 | 405 |
} |
38373 | 406 |
|
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
407 |
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
|
408 |
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
|
409 |
def the_dynamic_state(id: Document_ID.Exec): Command.State = execs.getOrElse(id, fail) |
52563 | 410 |
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
|
411 |
|
52531 | 412 |
def accumulate(id: Document_ID.Generic, message: XML.Elem): (Command.State, State) = |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
413 |
execs.get(id) match { |
44446 | 414 |
case Some(st) => |
49527 | 415 |
val new_st = st + (id, message) |
44446 | 416 |
(new_st, copy(execs = execs + (id -> new_st))) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
417 |
case None => |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
418 |
commands.get(id) match { |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
419 |
case Some(st) => |
49527 | 420 |
val new_st = st + (id, message) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
421 |
(new_st, copy(commands = commands + (id -> new_st))) |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
422 |
case None => fail |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
423 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
424 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
425 |
|
52563 | 426 |
def assign(id: Document_ID.Version, update: Assign_Update): (List[Command], State) = |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
427 |
{ |
38417 | 428 |
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
|
429 |
|
52566
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
wenzelm
parents:
52564
diff
changeset
|
430 |
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
|
431 |
: 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
|
432 |
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
|
433 |
|
44543 | 434 |
val (changed_commands, new_execs) = |
52563 | 435 |
((Nil: List[Command], execs) /: update) { |
52566
52a0eacf04d1
more formal type assign_update: avoid duplicate results and redundant update of global State.execs;
wenzelm
parents:
52564
diff
changeset
|
436 |
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
|
437 |
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
|
438 |
val command = st.command |
52527
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
439 |
val commands2 = command :: commands1 |
44543 | 440 |
val execs2 = |
441 |
exec match { |
|
52527
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
442 |
case Nil => execs1 |
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
443 |
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
|
444 |
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
|
445 |
(for (id <- print_ids; up <- upd(id, command.empty_state)) yield up) |
44543 | 446 |
} |
447 |
(commands2, execs2) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
448 |
} |
52563 | 449 |
val new_assignment = the_assignment(version).assign(update) |
43722 | 450 |
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
|
451 |
|
44543 | 452 |
(changed_commands, new_state) |
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
453 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
454 |
|
38417 | 455 |
def is_assigned(version: Version): Boolean = |
43722 | 456 |
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
|
457 |
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
|
458 |
case None => false |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
459 |
} |
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
460 |
|
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
461 |
def is_stable(change: Change): Boolean = |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
462 |
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
|
463 |
|
46944
9fc22eb6408c
more recent recent_syntax, e.g. relevant for document rendering during startup;
wenzelm
parents:
46942
diff
changeset
|
464 |
def recent_finished: Change = history.undo_list.find(_.is_finished) getOrElse fail |
44672 | 465 |
def recent_stable: Change = history.undo_list.find(is_stable) getOrElse fail |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
466 |
def tip_stable: Boolean = is_stable(history.tip) |
44475 | 467 |
def tip_version: Version = history.tip.version.get_finished |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
468 |
|
44446 | 469 |
def continue_history( |
470 |
previous: Future[Version], |
|
40479 | 471 |
edits: List[Edit_Text], |
43722 | 472 |
version: Future[Version]): (Change, State) = |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
473 |
{ |
46678 | 474 |
val change = Change.make(previous, edits, version) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
475 |
(change, copy(history = history + change)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
476 |
} |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
477 |
|
44672 | 478 |
def prune_history(retain: Int = 0): (List[Version], State) = |
479 |
{ |
|
46679 | 480 |
history.prune(is_stable, retain) match { |
481 |
case Some((dropped, history1)) => |
|
44672 | 482 |
val dropped_versions = dropped.map(change => change.version.get_finished) |
46679 | 483 |
val state1 = copy(history = history1) |
44672 | 484 |
(dropped_versions, state1) |
46679 | 485 |
case None => fail |
44672 | 486 |
} |
487 |
} |
|
488 |
||
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
489 |
def removed_versions(removed: List[Document_ID.Version]): State = |
44676 | 490 |
{ |
491 |
val versions1 = versions -- removed |
|
492 |
val assignments1 = assignments -- removed |
|
52530
99dd8b4ef3fe
explicit module Document_ID as source of globally unique identifiers across ML/Scala;
wenzelm
parents:
52527
diff
changeset
|
493 |
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
|
494 |
var execs1 = Map.empty[Document_ID.Exec, Command.State] |
44676 | 495 |
for { |
496 |
(version_id, version) <- versions1.iterator |
|
46997 | 497 |
command_execs = assignments1(version_id).command_execs |
46723 | 498 |
(_, node) <- version.nodes.entries |
44676 | 499 |
command <- node.commands.iterator |
500 |
} { |
|
52568 | 501 |
if (!commands1.isDefinedAt(command.id)) |
502 |
commands.get(command.id).foreach(st => commands1 += (command.id -> st)) |
|
503 |
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
|
504 |
if (!execs1.isDefinedAt(exec_id)) |
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
505 |
execs.get(exec_id).foreach(st => execs1 += (exec_id -> st)) |
44676 | 506 |
} |
507 |
} |
|
508 |
copy(versions = versions1, commands = commands1, execs = execs1, assignments = assignments1) |
|
509 |
} |
|
510 |
||
44613 | 511 |
def command_state(version: Version, command: Command): Command.State = |
512 |
{ |
|
513 |
require(is_assigned(version)) |
|
514 |
try { |
|
52527
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
515 |
the_assignment(version).check_finished.command_execs.getOrElse(command.id, Nil) |
52564
4e855c120f6a
tuned signature -- NB: Command.read is actually part of Command.eval;
wenzelm
parents:
52563
diff
changeset
|
516 |
.map(the_dynamic_state(_)) match { |
52527
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
517 |
case eval_state :: print_states => (eval_state /: print_states)(_ ++ _) |
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
518 |
case Nil => fail |
dbac84eab3bc
separate exec_id assignment for Command.print states, without affecting result of eval;
wenzelm
parents:
52508
diff
changeset
|
519 |
} |
44613 | 520 |
} |
47395
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
wenzelm
parents:
47388
diff
changeset
|
521 |
catch { |
e6261a493f04
added static command status markup, to emphasize accepted but unassigned/unparsed commands (notably in overview panel);
wenzelm
parents:
47388
diff
changeset
|
522 |
case _: State.Fail => |
52564
4e855c120f6a
tuned signature -- NB: Command.read is actually part of Command.eval;
wenzelm
parents:
52563
diff
changeset
|
523 |
try { the_static_state(command.id) } |
49414 | 524 |
catch { case _: State.Fail => 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
|
525 |
} |
44613 | 526 |
} |
527 |
||
49645 | 528 |
def markup_to_XML(version: Version, node: Node, filter: XML.Elem => Boolean): XML.Body = |
529 |
node.commands.toList.map(cmd => command_state(version, cmd).markup_to_XML(filter)).flatten |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
530 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
531 |
// persistent user-view |
49346 | 532 |
def snapshot(name: Node.Name = Node.Name.empty, pending_edits: List[Text.Edit] = Nil) |
533 |
: Snapshot = |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
534 |
{ |
44672 | 535 |
val stable = recent_stable |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44385
diff
changeset
|
536 |
val latest = history.tip |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
537 |
|
44156 | 538 |
// FIXME proper treatment of removed nodes |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
539 |
val edits = |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
540 |
(pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) => |
44156 | 541 |
(for ((a, Node.Edits(es)) <- change.edits if a == name) yield es).flatten ::: edits) |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
542 |
lazy val reverse_edits = edits.reverse |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
543 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
544 |
new Snapshot |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
545 |
{ |
44582 | 546 |
val state = State.this |
39698 | 547 |
val version = stable.version.get_finished |
48923
a2df77fcf1eb
prefer jEdit file name representation (potentially via VFS);
wenzelm
parents:
48793
diff
changeset
|
548 |
val node_name = name |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
549 |
val node = version.nodes(name) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
550 |
val is_outdated = !(pending_edits.isEmpty && latest == stable) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
551 |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
552 |
def convert(offset: Text.Offset) = (offset /: edits)((i, edit) => edit.convert(i)) |
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
553 |
def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i)) |
43425 | 554 |
def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r)) |
555 |
def revert(range: Text.Range) = (range /: reverse_edits)((r, edit) => edit.revert(r)) |
|
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
556 |
|
51494 | 557 |
def eq_content(other: Snapshot): Boolean = |
49346 | 558 |
!is_outdated && !other.is_outdated && |
559 |
node.commands.size == other.node.commands.size && |
|
560 |
((node.commands.iterator zip other.node.commands.iterator) forall { |
|
561 |
case (cmd1, cmd2) => |
|
51494 | 562 |
state.command_state(version, cmd1) eq_content |
563 |
other.state.command_state(other.version, cmd2) |
|
49346 | 564 |
}) |
565 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46152
diff
changeset
|
566 |
def cumulate_markup[A](range: Text.Range, info: A, elements: Option[Set[String]], |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
567 |
result: Command.State => (A, Text.Markup) => Option[A]): Stream[Text.Info[A]] = |
45459 | 568 |
{ |
45467 | 569 |
val former_range = revert(range) |
45459 | 570 |
for { |
571 |
(command, command_start) <- node.command_range(former_range).toStream |
|
50500
c94bba7906d2
identify dialogs via official serial and maintain as result message;
wenzelm
parents:
50204
diff
changeset
|
572 |
st = state.command_state(version, command) |
c94bba7906d2
identify dialogs via official serial and maintain as result message;
wenzelm
parents:
50204
diff
changeset
|
573 |
res = result(st) |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
574 |
Text.Info(r0, a) <- st.markup.cumulate[A]( |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
575 |
(former_range - command_start).restrict(command.range), info, elements, |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
576 |
{ case (a, Text.Info(r0, b)) => res(a, Text.Info(convert(r0 + command_start), b)) }) |
45459 | 577 |
} yield Text.Info(convert(r0 + command_start), a) |
578 |
} |
|
579 |
||
46178
1c5c88f6feb5
clarified Isabelle_Rendering vs. physical painting;
wenzelm
parents:
46152
diff
changeset
|
580 |
def select_markup[A](range: Text.Range, elements: Option[Set[String]], |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
581 |
result: Command.State => Text.Markup => Option[A]): Stream[Text.Info[A]] = |
38845
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
582 |
{ |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
583 |
def result1(st: Command.State): (Option[A], Text.Markup) => Option[Option[A]] = |
50500
c94bba7906d2
identify dialogs via official serial and maintain as result message;
wenzelm
parents:
50204
diff
changeset
|
584 |
{ |
c94bba7906d2
identify dialogs via official serial and maintain as result message;
wenzelm
parents:
50204
diff
changeset
|
585 |
val res = result(st) |
52889
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
586 |
(_: Option[A], x: Text.Markup) => |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
587 |
res(x) match { |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
588 |
case None => None |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
589 |
case some => Some(some) |
ea3338812e67
more tight interface for markup cumulate/select: avoid duplicate application, allow to defer decision about definedness;
wenzelm
parents:
52887
diff
changeset
|
590 |
} |
50500
c94bba7906d2
identify dialogs via official serial and maintain as result message;
wenzelm
parents:
50204
diff
changeset
|
591 |
} |
46198
cd040c5772de
improved select_markup: include filtering of defined results;
wenzelm
parents:
46178
diff
changeset
|
592 |
for (Text.Info(r, Some(x)) <- cumulate_markup(range, None, elements, result1)) |
cd040c5772de
improved select_markup: include filtering of defined results;
wenzelm
parents:
46178
diff
changeset
|
593 |
yield Text.Info(r, x) |
38845
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
594 |
} |
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
595 |
} |
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
596 |
} |
34485 | 597 |
} |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
598 |
} |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
599 |