| author | wenzelm |
| Sun, 10 Jul 2011 17:58:11 +0200 | |
| changeset 43730 | a0ed7bc688b5 |
| parent 43722 | 9b5dadb0c28d |
| child 43780 | 2cb2310d68b6 |
| 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 |
{
|
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
16 |
/* formal identifiers */ |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
17 |
|
|
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
18 |
type ID = Long |
|
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38374
diff
changeset
|
19 |
|
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
39698
diff
changeset
|
20 |
object ID |
|
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
39698
diff
changeset
|
21 |
{
|
|
38414
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38374
diff
changeset
|
22 |
def apply(id: ID): String = Markup.Long.apply(id) |
|
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38374
diff
changeset
|
23 |
def unapply(s: String): Option[ID] = Markup.Long.unapply(s) |
|
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38374
diff
changeset
|
24 |
} |
|
49f1f657adc2
more basic Markup.parse_int/print_int (using signed_string_of_int) (ML);
wenzelm
parents:
38374
diff
changeset
|
25 |
|
| 38373 | 26 |
type Version_ID = ID |
|
38363
af7f41a8a0a8
clarified "state" (accumulated data) vs. "exec" (execution that produces data);
wenzelm
parents:
38361
diff
changeset
|
27 |
type Command_ID = ID |
| 38373 | 28 |
type Exec_ID = ID |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
29 |
|
|
43662
e3175ec00311
Document.no_id/new_id as in ML (new_id *could* be session-specific but it isn't right now);
wenzelm
parents:
43427
diff
changeset
|
30 |
val no_id: ID = 0 |
|
e3175ec00311
Document.no_id/new_id as in ML (new_id *could* be session-specific but it isn't right now);
wenzelm
parents:
43427
diff
changeset
|
31 |
val new_id = new Counter |
|
38355
8cb265fb12fe
represent document ids by (long) int, to benefit from the somewhat faster Inttab in ML (LinearSet in Scala is invariably indexed by native object ids);
wenzelm
parents:
38227
diff
changeset
|
32 |
|
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
33 |
|
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
34 |
|
| 38424 | 35 |
/** document structure **/ |
36 |
||
37 |
/* named nodes -- development graph */ |
|
38 |
||
| 40479 | 39 |
type Edit[A] = |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
39698
diff
changeset
|
40 |
(String, // node name |
| 40479 | 41 |
Option[List[A]]) // None: remove node, Some: edit content |
| 38424 | 42 |
|
| 40479 | 43 |
type Edit_Text = Edit[Text.Edit] |
44 |
type Edit_Command = Edit[(Option[Command], Option[Command])] |
|
45 |
type Edit_Command_ID = Edit[(Option[Command_ID], Option[Command_ID])] |
|
| 38151 | 46 |
|
47 |
object Node |
|
48 |
{
|
|
|
43715
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
49 |
sealed case class Header(val master_dir: Path, val thy_header: Exn.Result[Thy_Header.Header]) |
|
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
50 |
val empty_header = Header(Path.current, Exn.Exn(ERROR("Bad theory header")))
|
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
51 |
|
|
43715
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
52 |
val empty: Node = Node(empty_header, Map(), Linear_Set()) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
53 |
|
| 38426 | 54 |
def command_starts(commands: Iterator[Command], offset: Text.Offset = 0) |
55 |
: Iterator[(Command, Text.Offset)] = |
|
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
56 |
{
|
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
57 |
var i = offset |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
58 |
for (command <- commands) yield {
|
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
59 |
val start = i |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
60 |
i += command.length |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
61 |
(command, start) |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
62 |
} |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
63 |
} |
| 38151 | 64 |
} |
65 |
||
|
38880
5b4efe90c120
simplified/clarified Document_View.text_area_extension;
wenzelm
parents:
38879
diff
changeset
|
66 |
private val block_size = 1024 |
|
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
67 |
|
|
43715
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
68 |
sealed case class Node( |
|
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
69 |
val header: Node.Header, |
|
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
70 |
val blobs: Map[String, Blob], |
|
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
71 |
val commands: Linear_Set[Command]) |
| 38151 | 72 |
{
|
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
73 |
/* commands */ |
|
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
74 |
|
|
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
75 |
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
|
76 |
{
|
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
77 |
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
|
78 |
var next_block = 0 |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
79 |
var last_stop = 0 |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
80 |
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
|
81 |
last_stop = start + command.length |
|
38885
06582837872b
Node.full_index: allow command spans larger than block_size;
wenzelm
parents:
38882
diff
changeset
|
82 |
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
|
83 |
blocks += (command -> start) |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
84 |
next_block += block_size |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
85 |
} |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
86 |
} |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
87 |
(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
|
88 |
} |
| 38151 | 89 |
|
|
38879
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
90 |
def full_range: Text.Range = full_index._2 |
| 38151 | 91 |
|
| 38569 | 92 |
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
|
93 |
{
|
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
94 |
if (!commands.isEmpty && full_range.contains(i)) {
|
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
95 |
val (cmd0, start0) = full_index._1(i / block_size) |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
96 |
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
|
97 |
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
|
98 |
} |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
99 |
else Iterator.empty |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
100 |
} |
| 38151 | 101 |
|
| 38569 | 102 |
def command_range(range: Text.Range): Iterator[(Command, Text.Offset)] = |
103 |
command_range(range.start) takeWhile { case (_, start) => start < range.stop }
|
|
| 38151 | 104 |
|
| 38426 | 105 |
def command_at(i: Text.Offset): Option[(Command, Text.Offset)] = |
| 38151 | 106 |
{
|
107 |
val range = command_range(i) |
|
108 |
if (range.hasNext) Some(range.next) else None |
|
109 |
} |
|
110 |
||
| 38426 | 111 |
def proper_command_at(i: Text.Offset): Option[Command] = |
| 38151 | 112 |
command_at(i) match {
|
113 |
case Some((command, _)) => |
|
114 |
commands.reverse_iterator(command).find(cmd => !cmd.is_ignored) |
|
115 |
case None => None |
|
116 |
} |
|
|
38879
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 |
def command_start(cmd: Command): Option[Text.Offset] = |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
119 |
command_starts.find(_._1 == cmd).map(_._2) |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
120 |
|
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
121 |
def command_starts: Iterator[(Command, Text.Offset)] = |
|
dde403450419
Document.Node: significant speedup of command_range etc. via lazy full_index;
wenzelm
parents:
38872
diff
changeset
|
122 |
Node.command_starts(commands.iterator) |
| 38151 | 123 |
} |
124 |
||
125 |
||
| 38424 | 126 |
|
127 |
/** versioning **/ |
|
128 |
||
129 |
/* particular document versions */ |
|
| 34485 | 130 |
|
| 38417 | 131 |
object Version |
132 |
{
|
|
| 43722 | 133 |
val init: Version = Version(no_id, Map().withDefaultValue(Node.empty)) |
| 38417 | 134 |
} |
135 |
||
| 43722 | 136 |
sealed case class Version(val id: Version_ID, val nodes: Map[String, Node]) |
| 34660 | 137 |
|
| 34859 | 138 |
|
| 38424 | 139 |
/* changes of plain text, eventually resulting in document edits */ |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
140 |
|
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
141 |
object Change |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
142 |
{
|
| 43722 | 143 |
val init = Change(Future.value(Version.init), Nil, Future.value(Version.init)) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
144 |
} |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
145 |
|
| 43722 | 146 |
sealed case class Change( |
| 38417 | 147 |
val previous: Future[Version], |
| 40479 | 148 |
val edits: List[Edit_Text], |
| 43722 | 149 |
val version: Future[Version]) |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
150 |
{
|
| 39698 | 151 |
def is_finished: Boolean = previous.is_finished && version.is_finished |
|
38227
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
152 |
} |
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
153 |
|
|
6bbb42843b6e
concentrate structural document notions in document.scala;
wenzelm
parents:
38220
diff
changeset
|
154 |
|
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
155 |
/* history navigation */ |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
156 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
157 |
object History |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
158 |
{
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
159 |
val init = new History(List(Change.init)) |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
160 |
} |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
161 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
162 |
// FIXME pruning, purging of state |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
163 |
class History(val undo_list: List[Change]) |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
164 |
{
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
165 |
require(!undo_list.isEmpty) |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
166 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
167 |
def tip: Change = undo_list.head |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
168 |
def +(change: Change): History = new History(change :: undo_list) |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
169 |
} |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
170 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
171 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
172 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
173 |
/** global state -- document structure, execution process, editing history **/ |
| 38424 | 174 |
|
175 |
abstract class Snapshot |
|
176 |
{
|
|
177 |
val version: Version |
|
178 |
val node: Node |
|
179 |
val is_outdated: Boolean |
|
180 |
def lookup_command(id: Command_ID): Option[Command] |
|
181 |
def state(command: Command): Command.State |
|
| 38427 | 182 |
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
|
183 |
def convert(range: Text.Range): Text.Range |
| 38427 | 184 |
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
|
185 |
def revert(range: Text.Range): Text.Range |
|
39178
83e9f3ccea9f
concentrate Isabelle specific physical rendering markup selection in isabelle_markup.scala;
wenzelm
parents:
39177
diff
changeset
|
186 |
def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A]) |
|
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
187 |
: Stream[Text.Info[Option[A]]] |
| 38424 | 188 |
} |
189 |
||
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
190 |
object State |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
191 |
{
|
| 38373 | 192 |
class Fail(state: State) extends Exception |
193 |
||
|
38882
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38880
diff
changeset
|
194 |
val init = State().define_version(Version.init, Map()).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
|
195 |
|
|
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
|
196 |
case class Assignment( |
|
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents:
38885
diff
changeset
|
197 |
val assignment: Map[Command, Exec_ID], |
|
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents:
38885
diff
changeset
|
198 |
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
|
199 |
{
|
|
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
|
200 |
def get_finished: Map[Command, Exec_ID] = { require(is_finished); assignment }
|
|
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents:
38885
diff
changeset
|
201 |
def assign(command_execs: List[(Command, Exec_ID)]): Assignment = |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
202 |
{
|
|
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
|
203 |
require(!is_finished) |
|
a4a465dc89d9
Document.State.Assignment: eliminated promise in favour of plain values -- signalling is done via event bus in Session;
wenzelm
parents:
38885
diff
changeset
|
204 |
Assignment(assignment ++ command_execs, true) |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
205 |
} |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
206 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
207 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
208 |
|
|
43715
518e44a0ee15
some support for blobs (arbitrary text files) within document nodes;
wenzelm
parents:
43697
diff
changeset
|
209 |
sealed case class State( |
| 38417 | 210 |
val versions: Map[Version_ID, Version] = Map(), |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
211 |
val commands: Map[Command_ID, Command.State] = Map(), |
| 38417 | 212 |
val execs: Map[Exec_ID, (Command.State, Set[Version])] = Map(), |
| 43722 | 213 |
val assignments: Map[Version_ID, State.Assignment] = Map(), |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
214 |
val disposed: Set[ID] = Set(), // FIXME unused!? |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
215 |
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
|
216 |
{
|
| 38373 | 217 |
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
|
218 |
|
| 38417 | 219 |
def define_version(version: Version, former_assignment: Map[Command, Exec_ID]): State = |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
220 |
{
|
| 38417 | 221 |
val id = version.id |
222 |
if (versions.isDefinedAt(id) || disposed(id)) fail |
|
223 |
copy(versions = versions + (id -> version), |
|
| 43722 | 224 |
assignments = assignments + (id -> State.Assignment(former_assignment))) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
225 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
226 |
|
| 38373 | 227 |
def define_command(command: Command): State = |
228 |
{
|
|
229 |
val id = command.id |
|
230 |
if (commands.isDefinedAt(id) || disposed(id)) fail |
|
231 |
copy(commands = commands + (id -> command.empty_state)) |
|
232 |
} |
|
233 |
||
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
234 |
def lookup_command(id: Command_ID): Option[Command] = commands.get(id).map(_.command) |
| 38373 | 235 |
|
| 38417 | 236 |
def the_version(id: Version_ID): Version = versions.getOrElse(id, fail) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
237 |
def the_command(id: Command_ID): Command.State = commands.getOrElse(id, fail) |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
238 |
def the_exec_state(id: Exec_ID): Command.State = execs.getOrElse(id, fail)._1 |
| 43722 | 239 |
def the_assignment(version: Version): State.Assignment = |
240 |
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
|
241 |
|
| 38872 | 242 |
def accumulate(id: ID, 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
|
243 |
execs.get(id) match {
|
| 38417 | 244 |
case Some((st, occs)) => |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
245 |
val new_st = st.accumulate(message) |
| 38417 | 246 |
(new_st, copy(execs = execs + (id -> (new_st, occs)))) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
247 |
case None => |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
248 |
commands.get(id) match {
|
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
249 |
case Some(st) => |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
250 |
val new_st = st.accumulate(message) |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
251 |
(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
|
252 |
case None => fail |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
253 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
254 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
255 |
|
|
38882
e1fb3bbc22ab
Document state assignment indicates command change;
wenzelm
parents:
38880
diff
changeset
|
256 |
def assign(id: Version_ID, edits: List[(Command_ID, Exec_ID)]): (List[Command], State) = |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
257 |
{
|
| 38417 | 258 |
val version = the_version(id) |
259 |
val occs = Set(version) // FIXME unused (!?) |
|
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
260 |
|
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
261 |
var new_execs = execs |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
262 |
val assigned_execs = |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
263 |
for ((cmd_id, exec_id) <- edits) yield {
|
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
264 |
val st = the_command(cmd_id) |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
265 |
if (new_execs.isDefinedAt(exec_id) || disposed(exec_id)) fail |
| 38417 | 266 |
new_execs += (exec_id -> (st, occs)) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
267 |
(st.command, exec_id) |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
268 |
} |
|
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
|
269 |
val new_assignment = the_assignment(version).assign(assigned_execs) |
| 43722 | 270 |
val new_state = copy(assignments = assignments + (id -> new_assignment), execs = new_execs) |
|
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
|
271 |
(assigned_execs.map(_._1), new_state) |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
272 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
273 |
|
| 38417 | 274 |
def is_assigned(version: Version): Boolean = |
| 43722 | 275 |
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
|
276 |
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
|
277 |
case None => false |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
278 |
} |
|
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
279 |
|
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
280 |
def extend_history(previous: Future[Version], |
| 40479 | 281 |
edits: List[Edit_Text], |
| 43722 | 282 |
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
|
283 |
{
|
| 43722 | 284 |
val change = Change(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
|
285 |
(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
|
286 |
} |
|
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 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
289 |
// persistent user-view |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
290 |
def snapshot(name: String, pending_edits: List[Text.Edit]): Snapshot = |
|
38370
8b15d0f98962
explicit Document.State value, instead of individual state variables in Session, Command, Document;
wenzelm
parents:
38367
diff
changeset
|
291 |
{
|
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
292 |
val found_stable = history.undo_list.find(change => |
| 39698 | 293 |
change.is_finished && is_assigned(change.version.get_finished)) |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
294 |
require(found_stable.isDefined) |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
295 |
val stable = found_stable.get |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
296 |
val latest = history.undo_list.head |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
297 |
|
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
39698
diff
changeset
|
298 |
// FIXME proper treatment of deleted nodes |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
299 |
val edits = |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
300 |
(pending_edits /: history.undo_list.takeWhile(_ != stable))((edits, change) => |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
39698
diff
changeset
|
301 |
(for ((a, Some(eds)) <- change.edits if a == name) yield eds).flatten ::: edits) |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
302 |
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
|
303 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
304 |
new Snapshot |
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
305 |
{
|
| 39698 | 306 |
val version = stable.version.get_finished |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
307 |
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
|
308 |
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
|
309 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
310 |
def lookup_command(id: Command_ID): Option[Command] = State.this.lookup_command(id) |
|
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 |
def state(command: Command): Command.State = |
|
38848
9483bb678d96
use Future.get_finished where this is the intended meaning -- prefer immediate crash over deadlock due to promises that are never fulfilled;
wenzelm
parents:
38845
diff
changeset
|
313 |
try { the_exec_state(the_assignment(version).get_finished.getOrElse(command, fail)) }
|
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
314 |
catch { case _: State.Fail => command.empty_state }
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
315 |
|
|
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
316 |
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
|
317 |
def revert(offset: Text.Offset) = (offset /: reverse_edits)((i, edit) => edit.revert(i)) |
| 43425 | 318 |
def convert(range: Text.Range) = (range /: edits)((r, edit) => edit.convert(r)) |
319 |
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
|
320 |
|
|
39178
83e9f3ccea9f
concentrate Isabelle specific physical rendering markup selection in isabelle_markup.scala;
wenzelm
parents:
39177
diff
changeset
|
321 |
def select_markup[A](range: Text.Range)(result: Markup_Tree.Select[A]) |
|
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
322 |
: Stream[Text.Info[Option[A]]] = |
|
38845
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
323 |
{
|
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
324 |
val former_range = revert(range) |
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
325 |
for {
|
|
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
326 |
(command, command_start) <- node.command_range(former_range).toStream |
|
38845
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
327 |
Text.Info(r0, x) <- state(command).markup. |
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
328 |
select((former_range - command_start).restrict(command.range)) {
|
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
329 |
case Text.Info(r0, info) |
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
330 |
if result.isDefinedAt(Text.Info(convert(r0 + command_start), info)) => |
|
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
331 |
result(Text.Info(convert(r0 + command_start), info)) |
|
39177
0468964aec11
simplified Markup_Tree.select: Stream instead of Iterator (again), explicit Option instead of default;
wenzelm
parents:
38976
diff
changeset
|
332 |
} |
|
43427
5c716a68931a
select_markup: no filtering here -- results may be distorted anyway;
wenzelm
parents:
43425
diff
changeset
|
333 |
} yield Text.Info(convert(r0 + command_start), x) |
|
38845
a9e37daf5bd0
added Document.Snapshot.select_markup, which includes command iteration, range conversion etc.;
wenzelm
parents:
38841
diff
changeset
|
334 |
} |
|
38841
4df7b76249a0
include Document.History in Document.State -- just one universal session state maintained by main actor;
wenzelm
parents:
38569
diff
changeset
|
335 |
} |
|
38150
67fc24df3721
simplified/refined document model: collection of named nodes, without proper dependencies yet;
wenzelm
parents:
37849
diff
changeset
|
336 |
} |
| 34485 | 337 |
} |
|
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
338 |
} |
|
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
339 |