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