author | wenzelm |
Sat, 29 May 2010 19:46:29 +0200 | |
changeset 37186 | 349e9223c685 |
parent 37073 | 5e42e36a6693 |
child 37685 | 305c326db33b |
permissions | -rw-r--r-- |
36676 | 1 |
/* Title: Pure/PIDE/document.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Document as editable list of commands. |
|
5 |
*/ |
|
34407 | 6 |
|
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
|
7 |
package isabelle |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
8 |
|
34760 | 9 |
|
37073 | 10 |
import scala.annotation.tailrec |
11 |
||
12 |
||
34823 | 13 |
object Document |
34483 | 14 |
{ |
34859 | 15 |
/* command start positions */ |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
16 |
|
34859 | 17 |
def command_starts(commands: Linear_Set[Command]): Iterator[(Command, Int)] = |
18 |
{ |
|
19 |
var offset = 0 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
20 |
for (cmd <- commands.iterator) yield { |
34859 | 21 |
val start = offset |
22 |
offset += cmd.length |
|
23 |
(cmd, start) |
|
24 |
} |
|
25 |
} |
|
26 |
||
27 |
||
28 |
/* empty document */ |
|
34485 | 29 |
|
34823 | 30 |
def empty(id: Isar_Document.Document_ID): Document = |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
31 |
{ |
34859 | 32 |
val doc = new Document(id, Linear_Set(), Map()) |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
33 |
doc.assign_states(Nil) |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
34 |
doc |
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
35 |
} |
34660 | 36 |
|
34859 | 37 |
|
38 |
||
39 |
/** document edits **/ |
|
40 |
||
34853
32b49207ca20
misc tuning and clarification of Document/Change;
wenzelm
parents:
34840
diff
changeset
|
41 |
type Edit = (Option[Command], Option[Command]) |
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
42 |
|
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
43 |
def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID, |
34853
32b49207ca20
misc tuning and clarification of Document/Change;
wenzelm
parents:
34840
diff
changeset
|
44 |
edits: List[Text_Edit]): (List[Edit], Document) = |
34824
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
45 |
{ |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
46 |
require(old_doc.assignment.is_finished) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
47 |
|
34859 | 48 |
|
49 |
/* unparsed dummy commands */ |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
50 |
|
34859 | 51 |
def unparsed(source: String) = |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36761
diff
changeset
|
52 |
new Command(null, List(Token(Token.Kind.UNPARSED, source))) |
34859 | 53 |
|
34860
847c00f5535a
do not override Command.hashCode -- which was inconsistent with eq anyway;
wenzelm
parents:
34859
diff
changeset
|
54 |
def is_unparsed(command: Command) = command.id == null |
34835
67733fd0e3fa
back to explicit management of documents -- not as generic Session.Entity -- to avoid ill-defined referencing of new states;
wenzelm
parents:
34832
diff
changeset
|
55 |
|
34859 | 56 |
|
57 |
/* phase 1: edit individual command source */ |
|
58 |
||
34863 | 59 |
def edit_text(eds: List[Text_Edit], commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 60 |
{ |
61 |
eds match { |
|
62 |
case e :: es => |
|
63 |
command_starts(commands).find { // FIXME relative search! |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
64 |
case (cmd, cmd_start) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
65 |
e.can_edit(cmd.source, cmd_start) || e.is_insert && e.start == cmd_start + cmd.length |
34863 | 66 |
} match { |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
67 |
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
68 |
val (rest, text) = e.edit(cmd.source, cmd_start) |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
69 |
val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd |
34863 | 70 |
edit_text(rest.toList ::: es, new_commands) |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
71 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
72 |
case Some((cmd, cmd_start)) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
73 |
edit_text(es, commands.insert_after(Some(cmd), unparsed(e.text))) |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
74 |
|
34859 | 75 |
case None => |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
76 |
require(e.is_insert && e.start == 0) |
34863 | 77 |
edit_text(es, commands.insert_after(None, unparsed(e.text))) |
34859 | 78 |
} |
34863 | 79 |
case Nil => commands |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
80 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
81 |
} |
34582 | 82 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
83 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
84 |
/* phase 2: recover command spans */ |
34859 | 85 |
|
37073 | 86 |
@tailrec def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 87 |
{ |
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
88 |
commands.iterator.find(is_unparsed) match { |
34859 | 89 |
case Some(first_unparsed) => |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
90 |
val first = |
37072
9105c8237c7a
renamed "rev" to "reverse" following usual Scala conventions;
wenzelm
parents:
37071
diff
changeset
|
91 |
commands.reverse_iterator(first_unparsed).find(_.is_command) getOrElse commands.head |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
92 |
val last = |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
93 |
commands.iterator(first_unparsed).find(_.is_command) getOrElse commands.last |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
94 |
val range = |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
95 |
commands.iterator(first).takeWhile(_ != last).toList ::: List(last) |
34859 | 96 |
|
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
97 |
val sources = range.flatMap(_.span.map(_.source)) |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
98 |
val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString)) |
34485 | 99 |
|
34859 | 100 |
val (before_edit, spans1) = |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
101 |
if (!spans0.isEmpty && first.is_command && first.span == spans0.head) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
102 |
(Some(first), spans0.tail) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
103 |
else (commands.prev(first), spans0) |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
104 |
|
34859 | 105 |
val (after_edit, spans2) = |
37071
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
106 |
if (!spans1.isEmpty && last.is_command && last.span == spans1.last) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
107 |
(Some(last), spans1.take(spans1.length - 1)) |
dd3540a489f7
parse_spans: cover full range including adjacent well-formed commands -- intermediate ignored and malformed commands are reparsed as well;
wenzelm
parents:
37059
diff
changeset
|
108 |
else (commands.next(last), spans1) |
34859 | 109 |
|
34863 | 110 |
val inserted = spans2.map(span => new Command(session.create_id(), span)) |
111 |
val new_commands = |
|
112 |
commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted) |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
113 |
parse_spans(new_commands) |
34485 | 114 |
|
34863 | 115 |
case None => commands |
34485 | 116 |
} |
117 |
} |
|
34739 | 118 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
119 |
|
34859 | 120 |
/* phase 3: resulting document edits */ |
121 |
||
37059 | 122 |
val commands0 = old_doc.commands |
123 |
val commands1 = edit_text(edits, commands0) |
|
124 |
val commands2 = parse_spans(commands1) |
|
34660 | 125 |
|
37059 | 126 |
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList |
127 |
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList |
|
34863 | 128 |
|
37059 | 129 |
val doc_edits = |
130 |
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) ::: |
|
131 |
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd))) |
|
34660 | 132 |
|
37059 | 133 |
val former_states = old_doc.assignment.join -- removed_commands |
34859 | 134 |
|
37059 | 135 |
(doc_edits, new Document(new_id, commands2, former_states)) |
34485 | 136 |
} |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
137 |
} |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
138 |
|
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
139 |
|
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
140 |
class Document( |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
141 |
val id: Isar_Document.Document_ID, |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
142 |
val commands: Linear_Set[Command], |
34859 | 143 |
former_states: Map[Command, Command]) |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
144 |
{ |
34859 | 145 |
/* command ranges */ |
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
146 |
|
34859 | 147 |
def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands) |
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
148 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
149 |
def command_start(cmd: Command): Option[Int] = |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
150 |
command_starts.find(_._1 == cmd).map(_._2) |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
151 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
152 |
def command_range(i: Int): Iterator[(Command, Int)] = |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
153 |
command_starts dropWhile { case (cmd, start) => start + cmd.length <= i } |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
154 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
155 |
def command_range(i: Int, j: Int): Iterator[(Command, Int)] = |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
156 |
command_range(i) takeWhile { case (_, start) => start < j } |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
157 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
158 |
def command_at(i: Int): Option[(Command, Int)] = |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
159 |
{ |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
160 |
val range = command_range(i) |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
161 |
if (range.hasNext) Some(range.next) else None |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
162 |
} |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
163 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
164 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
165 |
/* command state assignment */ |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
166 |
|
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
167 |
val assignment = Future.promise[Map[Command, Command]] |
34853
32b49207ca20
misc tuning and clarification of Document/Change;
wenzelm
parents:
34840
diff
changeset
|
168 |
def await_assignment { assignment.join } |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
169 |
|
34859 | 170 |
@volatile private var tmp_states = former_states |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
171 |
|
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
172 |
def assign_states(new_states: List[(Command, Command)]) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
173 |
{ |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
174 |
assignment.fulfill(tmp_states ++ new_states) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
175 |
tmp_states = Map() |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
176 |
} |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
177 |
|
36990
449628c148cf
explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents:
36956
diff
changeset
|
178 |
def current_state(cmd: Command): State = |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
179 |
{ |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
180 |
require(assignment.is_finished) |
36990
449628c148cf
explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents:
36956
diff
changeset
|
181 |
(assignment.join).get(cmd) match { |
449628c148cf
explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents:
36956
diff
changeset
|
182 |
case Some(cmd_state) => cmd_state.current_state |
37186
349e9223c685
explicit markup for forked goals, as indicated by Goal.fork;
wenzelm
parents:
37073
diff
changeset
|
183 |
case None => new State(cmd, Command.Status.UNDEFINED, 0, Nil, cmd.empty_markup) |
36990
449628c148cf
explicit Command.Status.UNDEFINED -- avoid fragile/cumbersome treatment of Option[State];
wenzelm
parents:
36956
diff
changeset
|
184 |
} |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
185 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
186 |
} |