author | wenzelm |
Mon, 17 May 2010 14:23:54 +0200 | |
changeset 36956 | 21be4832c362 |
parent 36761 | c8ae87ce6e4d |
child 36990 | 449628c148cf |
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 |
|
34823 | 10 |
object Document |
34483 | 11 |
{ |
34859 | 12 |
/* command start positions */ |
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
13 |
|
34859 | 14 |
def command_starts(commands: Linear_Set[Command]): Iterator[(Command, Int)] = |
15 |
{ |
|
16 |
var offset = 0 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
17 |
for (cmd <- commands.iterator) yield { |
34859 | 18 |
val start = offset |
19 |
offset += cmd.length |
|
20 |
(cmd, start) |
|
21 |
} |
|
22 |
} |
|
23 |
||
24 |
||
25 |
/* empty document */ |
|
34485 | 26 |
|
34823 | 27 |
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
|
28 |
{ |
34859 | 29 |
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
|
30 |
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
|
31 |
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
|
32 |
} |
34660 | 33 |
|
34859 | 34 |
|
35 |
||
36 |
/** document edits **/ |
|
37 |
||
34853
32b49207ca20
misc tuning and clarification of Document/Change;
wenzelm
parents:
34840
diff
changeset
|
38 |
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
|
39 |
|
ac35eee85f5c
renamed current_document to recent_document (might be a bit older than current_change);
wenzelm
parents:
34823
diff
changeset
|
40 |
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
|
41 |
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
|
42 |
{ |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
43 |
require(old_doc.assignment.is_finished) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
44 |
|
34859 | 45 |
|
46 |
/* unparsed dummy commands */ |
|
34538
20bfcca24658
Prover as actor managing ProofDocument-versions (removed EventBus structural_changes);
immler@in.tum.de
parents:
34532
diff
changeset
|
47 |
|
34859 | 48 |
def unparsed(source: String) = |
36956
21be4832c362
renamed class Outer_Lex to Token and Token_Kind to Token.Kind;
wenzelm
parents:
36761
diff
changeset
|
49 |
new Command(null, List(Token(Token.Kind.UNPARSED, source))) |
34859 | 50 |
|
34860
847c00f5535a
do not override Command.hashCode -- which was inconsistent with eq anyway;
wenzelm
parents:
34859
diff
changeset
|
51 |
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
|
52 |
|
34859 | 53 |
|
54 |
/* phase 1: edit individual command source */ |
|
55 |
||
34863 | 56 |
def edit_text(eds: List[Text_Edit], commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 57 |
{ |
58 |
eds match { |
|
59 |
case e :: es => |
|
60 |
command_starts(commands).find { // FIXME relative search! |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
61 |
case (cmd, cmd_start) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
62 |
e.can_edit(cmd.source, cmd_start) || e.is_insert && e.start == cmd_start + cmd.length |
34863 | 63 |
} match { |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
64 |
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
|
65 |
val (rest, text) = e.edit(cmd.source, cmd_start) |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
66 |
val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd |
34863 | 67 |
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
|
68 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
69 |
case Some((cmd, cmd_start)) => |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
70 |
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
|
71 |
|
34859 | 72 |
case None => |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
73 |
require(e.is_insert && e.start == 0) |
34863 | 74 |
edit_text(es, commands.insert_after(None, unparsed(e.text))) |
34859 | 75 |
} |
34863 | 76 |
case Nil => commands |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
77 |
} |
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
78 |
} |
34582 | 79 |
|
34818
7df68a8f0e3e
register Proof_Document instances as session entities -- handle Markup.EDIT messages locally;
wenzelm
parents:
34815
diff
changeset
|
80 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
81 |
/* phase 2: recover command spans */ |
34859 | 82 |
|
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
83 |
def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] = |
34859 | 84 |
{ |
85 |
// FIXME relative search! |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
86 |
commands.iterator.find(is_unparsed) match { |
34859 | 87 |
case Some(first_unparsed) => |
88 |
val prefix = commands.prev(first_unparsed) |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
89 |
val body = commands.iterator(first_unparsed).takeWhile(is_unparsed).toList |
34859 | 90 |
val suffix = commands.next(body.last) |
91 |
||
34863 | 92 |
val sources = (prefix.toList ::: body ::: suffix.toList).flatMap(_.span.map(_.source)) |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
93 |
val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString)) |
34485 | 94 |
|
34859 | 95 |
val (before_edit, spans1) = |
36012 | 96 |
if (!spans0.isEmpty && Some(spans0.head) == prefix.map(_.span)) |
34866
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
97 |
(prefix, spans0.tail) |
a4fe43df58b3
new unparsed span for text right after existing command;
wenzelm
parents:
34863
diff
changeset
|
98 |
else (if (prefix.isDefined) commands.prev(prefix.get) else None, spans0) |
34544
56217d219e27
proofdocument-versions get id from changes
immler@in.tum.de
parents:
34541
diff
changeset
|
99 |
|
34859 | 100 |
val (after_edit, spans2) = |
101 |
if (!spans1.isEmpty && Some(spans1.last) == suffix.map(_.span)) |
|
102 |
(suffix, spans1.take(spans1.length - 1)) |
|
103 |
else (if (suffix.isDefined) commands.next(suffix.get) else None, spans1) |
|
104 |
||
34863 | 105 |
val inserted = spans2.map(span => new Command(session.create_id(), span)) |
106 |
val new_commands = |
|
107 |
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
|
108 |
parse_spans(new_commands) |
34485 | 109 |
|
34863 | 110 |
case None => commands |
34485 | 111 |
} |
112 |
} |
|
34739 | 113 |
|
34554
7dc6c231da40
abs. stops, markup nodes depend on doc-version;
immler@in.tum.de
parents:
34551
diff
changeset
|
114 |
|
34859 | 115 |
/* phase 3: resulting document edits */ |
116 |
||
34867 | 117 |
val result = Library.timeit("text_edits") { |
118 |
val commands0 = old_doc.commands |
|
119 |
val commands1 = Library.timeit("edit_text") { edit_text(edits, commands0) } |
|
120 |
val commands2 = Library.timeit("parse_spans") { parse_spans(commands1) } |
|
34660 | 121 |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
122 |
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList |
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34871
diff
changeset
|
123 |
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList |
34863 | 124 |
|
34867 | 125 |
val doc_edits = |
126 |
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) ::: |
|
127 |
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd))) |
|
34660 | 128 |
|
34867 | 129 |
val former_states = old_doc.assignment.join -- removed_commands |
34859 | 130 |
|
34867 | 131 |
(doc_edits, new Document(new_id, commands2, former_states)) |
132 |
} |
|
133 |
result |
|
34485 | 134 |
} |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
135 |
} |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
136 |
|
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
137 |
|
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
138 |
class Document( |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
139 |
val id: Isar_Document.Document_ID, |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
140 |
val commands: Linear_Set[Command], |
34859 | 141 |
former_states: Map[Command, Command]) |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
142 |
{ |
34859 | 143 |
/* command ranges */ |
34855
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
144 |
|
34859 | 145 |
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
|
146 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
147 |
def command_start(cmd: Command): Option[Int] = |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
148 |
command_starts.find(_._1 == cmd).map(_._2) |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
149 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
150 |
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
|
151 |
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
|
152 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
153 |
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
|
154 |
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
|
155 |
|
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
156 |
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
|
157 |
{ |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
158 |
val range = command_range(i) |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
159 |
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
|
160 |
} |
81d0410dc3ac
iterators for ranges of commands/starts -- avoid extra array per document;
wenzelm
parents:
34853
diff
changeset
|
161 |
|
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 |
/* command state assignment */ |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
164 |
|
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
165 |
val assignment = Future.promise[Map[Command, Command]] |
34853
32b49207ca20
misc tuning and clarification of Document/Change;
wenzelm
parents:
34840
diff
changeset
|
166 |
def await_assignment { assignment.join } |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
167 |
|
34859 | 168 |
@volatile private var tmp_states = former_states |
34868 | 169 |
private val time0 = System.currentTimeMillis |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
170 |
|
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
171 |
def assign_states(new_states: List[(Command, Command)]) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
172 |
{ |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
173 |
assignment.fulfill(tmp_states ++ new_states) |
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
174 |
tmp_states = Map() |
34868 | 175 |
System.err.println("assign_states: " + (System.currentTimeMillis - time0) + " ms elapsed time") |
34840
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 |
|
34867 | 178 |
def current_state(cmd: Command): Option[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) |
34867 | 181 |
(assignment.join).get(cmd).map(_.current_state) |
34840
6c5560d48561
more precise treatment of document/state assigment;
wenzelm
parents:
34838
diff
changeset
|
182 |
} |
34318
c13e168a8ae6
original sources from Johannes Hölzl a48e0c6ab1aea77c52d596f7efc007a543d3d10c with minor modifications of directory layout;
wenzelm
parents:
diff
changeset
|
183 |
} |