wenzelm@36714
|
1 |
/* Title: Pure/PIDE/document.scala
|
wenzelm@36714
|
2 |
Author: Makarius
|
wenzelm@36714
|
3 |
|
wenzelm@36714
|
4 |
Document as editable list of commands.
|
wenzelm@36714
|
5 |
*/
|
wenzelm@34410
|
6 |
|
wenzelm@34874
|
7 |
package isabelle
|
wenzelm@34321
|
8 |
|
wenzelm@34763
|
9 |
|
wenzelm@34826
|
10 |
object Document
|
wenzelm@34486
|
11 |
{
|
wenzelm@34862
|
12 |
/* command start positions */
|
wenzelm@34821
|
13 |
|
wenzelm@34862
|
14 |
def command_starts(commands: Linear_Set[Command]): Iterator[(Command, Int)] =
|
wenzelm@34862
|
15 |
{
|
wenzelm@34862
|
16 |
var offset = 0
|
wenzelm@36035
|
17 |
for (cmd <- commands.iterator) yield {
|
wenzelm@34862
|
18 |
val start = offset
|
wenzelm@34862
|
19 |
offset += cmd.length
|
wenzelm@34862
|
20 |
(cmd, start)
|
wenzelm@34862
|
21 |
}
|
wenzelm@34862
|
22 |
}
|
wenzelm@34862
|
23 |
|
wenzelm@34862
|
24 |
|
wenzelm@34862
|
25 |
/* empty document */
|
wenzelm@34488
|
26 |
|
wenzelm@34826
|
27 |
def empty(id: Isar_Document.Document_ID): Document =
|
wenzelm@34838
|
28 |
{
|
wenzelm@34862
|
29 |
val doc = new Document(id, Linear_Set(), Map())
|
wenzelm@34838
|
30 |
doc.assign_states(Nil)
|
wenzelm@34838
|
31 |
doc
|
wenzelm@34838
|
32 |
}
|
immler@34663
|
33 |
|
wenzelm@34862
|
34 |
|
wenzelm@34862
|
35 |
|
wenzelm@34862
|
36 |
/** document edits **/
|
wenzelm@34862
|
37 |
|
wenzelm@34856
|
38 |
type Edit = (Option[Command], Option[Command])
|
wenzelm@34827
|
39 |
|
wenzelm@34827
|
40 |
def text_edits(session: Session, old_doc: Document, new_id: Isar_Document.Document_ID,
|
wenzelm@34856
|
41 |
edits: List[Text_Edit]): (List[Edit], Document) =
|
wenzelm@34827
|
42 |
{
|
wenzelm@34843
|
43 |
require(old_doc.assignment.is_finished)
|
wenzelm@34843
|
44 |
|
wenzelm@34862
|
45 |
|
wenzelm@34862
|
46 |
/* unparsed dummy commands */
|
wenzelm@34862
|
47 |
|
wenzelm@34862
|
48 |
def unparsed(source: String) =
|
wenzelm@34863
|
49 |
new Command(null, List(Outer_Lex.Token(Outer_Lex.Token_Kind.UNPARSED, source)))
|
wenzelm@34862
|
50 |
|
wenzelm@34863
|
51 |
def is_unparsed(command: Command) = command.id == null
|
wenzelm@34862
|
52 |
|
wenzelm@34862
|
53 |
|
wenzelm@34862
|
54 |
/* phase 1: edit individual command source */
|
wenzelm@34862
|
55 |
|
wenzelm@34866
|
56 |
def edit_text(eds: List[Text_Edit], commands: Linear_Set[Command]): Linear_Set[Command] =
|
wenzelm@34862
|
57 |
{
|
wenzelm@34862
|
58 |
eds match {
|
wenzelm@34862
|
59 |
case e :: es =>
|
wenzelm@34862
|
60 |
command_starts(commands).find { // FIXME relative search!
|
wenzelm@34869
|
61 |
case (cmd, cmd_start) =>
|
wenzelm@34869
|
62 |
e.can_edit(cmd.source, cmd_start) || e.is_insert && e.start == cmd_start + cmd.length
|
wenzelm@34866
|
63 |
} match {
|
wenzelm@34869
|
64 |
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) =>
|
wenzelm@34869
|
65 |
val (rest, text) = e.edit(cmd.source, cmd_start)
|
wenzelm@34869
|
66 |
val new_commands = commands.insert_after(Some(cmd), unparsed(text)) - cmd
|
wenzelm@34866
|
67 |
edit_text(rest.toList ::: es, new_commands)
|
wenzelm@34862
|
68 |
|
wenzelm@34869
|
69 |
case Some((cmd, cmd_start)) =>
|
wenzelm@34869
|
70 |
edit_text(es, commands.insert_after(Some(cmd), unparsed(e.text)))
|
wenzelm@34869
|
71 |
|
wenzelm@34862
|
72 |
case None =>
|
wenzelm@34869
|
73 |
require(e.is_insert && e.start == 0)
|
wenzelm@34866
|
74 |
edit_text(es, commands.insert_after(None, unparsed(e.text)))
|
wenzelm@34862
|
75 |
}
|
wenzelm@34866
|
76 |
case Nil => commands
|
wenzelm@34862
|
77 |
}
|
wenzelm@34862
|
78 |
}
|
wenzelm@34862
|
79 |
|
wenzelm@34862
|
80 |
|
wenzelm@34869
|
81 |
/* phase 2: recover command spans */
|
wenzelm@34862
|
82 |
|
wenzelm@34869
|
83 |
def parse_spans(commands: Linear_Set[Command]): Linear_Set[Command] =
|
wenzelm@34862
|
84 |
{
|
wenzelm@34862
|
85 |
// FIXME relative search!
|
wenzelm@36035
|
86 |
commands.iterator.find(is_unparsed) match {
|
wenzelm@34862
|
87 |
case Some(first_unparsed) =>
|
wenzelm@34862
|
88 |
val prefix = commands.prev(first_unparsed)
|
wenzelm@36035
|
89 |
val body = commands.iterator(first_unparsed).takeWhile(is_unparsed).toList
|
wenzelm@34862
|
90 |
val suffix = commands.next(body.last)
|
wenzelm@34862
|
91 |
|
wenzelm@34866
|
92 |
val sources = (prefix.toList ::: body ::: suffix.toList).flatMap(_.span.map(_.source))
|
wenzelm@34869
|
93 |
val spans0 = Thy_Syntax.parse_spans(session.current_syntax.scan(sources.mkString))
|
wenzelm@34862
|
94 |
|
wenzelm@34862
|
95 |
val (before_edit, spans1) =
|
wenzelm@36036
|
96 |
if (!spans0.isEmpty && Some(spans0.head) == prefix.map(_.span))
|
wenzelm@34869
|
97 |
(prefix, spans0.tail)
|
wenzelm@34869
|
98 |
else (if (prefix.isDefined) commands.prev(prefix.get) else None, spans0)
|
wenzelm@34862
|
99 |
|
wenzelm@34862
|
100 |
val (after_edit, spans2) =
|
wenzelm@34862
|
101 |
if (!spans1.isEmpty && Some(spans1.last) == suffix.map(_.span))
|
wenzelm@34862
|
102 |
(suffix, spans1.take(spans1.length - 1))
|
wenzelm@34862
|
103 |
else (if (suffix.isDefined) commands.next(suffix.get) else None, spans1)
|
wenzelm@34862
|
104 |
|
wenzelm@34866
|
105 |
val inserted = spans2.map(span => new Command(session.create_id(), span))
|
wenzelm@34866
|
106 |
val new_commands =
|
wenzelm@34866
|
107 |
commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted)
|
wenzelm@34869
|
108 |
parse_spans(new_commands)
|
wenzelm@34862
|
109 |
|
wenzelm@34866
|
110 |
case None => commands
|
wenzelm@34862
|
111 |
}
|
wenzelm@34862
|
112 |
}
|
wenzelm@34862
|
113 |
|
wenzelm@34862
|
114 |
|
wenzelm@34862
|
115 |
/* phase 3: resulting document edits */
|
wenzelm@34862
|
116 |
|
wenzelm@34870
|
117 |
val result = Library.timeit("text_edits") {
|
wenzelm@34870
|
118 |
val commands0 = old_doc.commands
|
wenzelm@34870
|
119 |
val commands1 = Library.timeit("edit_text") { edit_text(edits, commands0) }
|
wenzelm@34870
|
120 |
val commands2 = Library.timeit("parse_spans") { parse_spans(commands1) }
|
wenzelm@34862
|
121 |
|
wenzelm@36035
|
122 |
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList
|
wenzelm@36035
|
123 |
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList
|
wenzelm@34866
|
124 |
|
wenzelm@34870
|
125 |
val doc_edits =
|
wenzelm@34870
|
126 |
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) :::
|
wenzelm@34870
|
127 |
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd)))
|
wenzelm@34862
|
128 |
|
wenzelm@34870
|
129 |
val former_states = old_doc.assignment.join -- removed_commands
|
wenzelm@34862
|
130 |
|
wenzelm@34870
|
131 |
(doc_edits, new Document(new_id, commands2, former_states))
|
wenzelm@34870
|
132 |
}
|
wenzelm@34870
|
133 |
result
|
wenzelm@34827
|
134 |
}
|
wenzelm@34781
|
135 |
}
|
immler@34541
|
136 |
|
wenzelm@34858
|
137 |
|
wenzelm@34843
|
138 |
class Document(
|
wenzelm@34843
|
139 |
val id: Isar_Document.Document_ID,
|
wenzelm@34843
|
140 |
val commands: Linear_Set[Command],
|
wenzelm@34862
|
141 |
former_states: Map[Command, Command])
|
wenzelm@34843
|
142 |
{
|
wenzelm@34862
|
143 |
/* command ranges */
|
wenzelm@34843
|
144 |
|
wenzelm@34862
|
145 |
def command_starts: Iterator[(Command, Int)] = Document.command_starts(commands)
|
wenzelm@34858
|
146 |
|
wenzelm@34858
|
147 |
def command_start(cmd: Command): Option[Int] =
|
wenzelm@34858
|
148 |
command_starts.find(_._1 == cmd).map(_._2)
|
wenzelm@34858
|
149 |
|
wenzelm@34858
|
150 |
def command_range(i: Int): Iterator[(Command, Int)] =
|
wenzelm@34858
|
151 |
command_starts dropWhile { case (cmd, start) => start + cmd.length <= i }
|
wenzelm@34858
|
152 |
|
wenzelm@34858
|
153 |
def command_range(i: Int, j: Int): Iterator[(Command, Int)] =
|
wenzelm@34858
|
154 |
command_range(i) takeWhile { case (_, start) => start < j }
|
wenzelm@34858
|
155 |
|
wenzelm@34858
|
156 |
def command_at(i: Int): Option[(Command, Int)] =
|
wenzelm@34858
|
157 |
{
|
wenzelm@34858
|
158 |
val range = command_range(i)
|
wenzelm@34858
|
159 |
if (range.hasNext) Some(range.next) else None
|
wenzelm@34858
|
160 |
}
|
wenzelm@34858
|
161 |
|
wenzelm@34858
|
162 |
|
wenzelm@34858
|
163 |
/* command state assignment */
|
wenzelm@34843
|
164 |
|
wenzelm@34843
|
165 |
val assignment = Future.promise[Map[Command, Command]]
|
wenzelm@34856
|
166 |
def await_assignment { assignment.join }
|
wenzelm@34843
|
167 |
|
wenzelm@34862
|
168 |
@volatile private var tmp_states = former_states
|
wenzelm@34871
|
169 |
private val time0 = System.currentTimeMillis
|
wenzelm@34843
|
170 |
|
wenzelm@34843
|
171 |
def assign_states(new_states: List[(Command, Command)])
|
wenzelm@34843
|
172 |
{
|
wenzelm@34843
|
173 |
assignment.fulfill(tmp_states ++ new_states)
|
wenzelm@34843
|
174 |
tmp_states = Map()
|
wenzelm@34871
|
175 |
System.err.println("assign_states: " + (System.currentTimeMillis - time0) + " ms elapsed time")
|
wenzelm@34843
|
176 |
}
|
wenzelm@34843
|
177 |
|
wenzelm@34870
|
178 |
def current_state(cmd: Command): Option[State] =
|
wenzelm@34843
|
179 |
{
|
wenzelm@34843
|
180 |
require(assignment.is_finished)
|
wenzelm@34870
|
181 |
(assignment.join).get(cmd).map(_.current_state)
|
wenzelm@34843
|
182 |
}
|
wenzelm@34321
|
183 |
}
|