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