author | wenzelm |
Sat, 26 Nov 2011 17:10:03 +0100 | |
changeset 45644 | 8634b4e61b88 |
parent 44615 | a4ff8a787202 |
child 46680 | 234f1730582d |
permissions | -rw-r--r-- |
34268 | 1 |
/* Title: Pure/Thy/thy_syntax.scala |
2 |
Author: Makarius |
|
3 |
||
38374 | 4 |
Superficial theory syntax: tokens and spans. |
34268 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
38239
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
10 |
import scala.collection.mutable |
38374 | 11 |
import scala.annotation.tailrec |
38239
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
12 |
|
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
13 |
|
34303 | 14 |
object Thy_Syntax |
34268 | 15 |
{ |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
16 |
/** nested structure **/ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
17 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
18 |
object Structure |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
19 |
{ |
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
20 |
sealed abstract class Entry { def length: Int } |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
21 |
case class Block(val name: String, val body: List[Entry]) extends Entry |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
22 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
23 |
val length: Int = (0 /: body)(_ + _.length) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
24 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
25 |
case class Atom(val command: Command) extends Entry |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
26 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
27 |
def length: Int = command.length |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
28 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
29 |
|
44615 | 30 |
def parse(syntax: Outer_Syntax, node_name: Document.Node.Name, text: CharSequence) |
44607
274eff0ea12e
maintain name of *the* enclosing node as part of command -- avoid full document traversal;
wenzelm
parents:
44474
diff
changeset
|
31 |
: Entry = |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
32 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
33 |
/* stack operations */ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
34 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
35 |
def buffer(): mutable.ListBuffer[Entry] = new mutable.ListBuffer[Entry] |
44615 | 36 |
var stack: List[(Int, String, mutable.ListBuffer[Entry])] = |
37 |
List((0, "theory " + node_name.theory, buffer())) |
|
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
38 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
39 |
@tailrec def close(level: Int => Boolean) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
40 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
41 |
stack match { |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
42 |
case (lev, name, body) :: (_, _, body2) :: rest if level(lev) => |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
43 |
body2 += Block(name, body.toList) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
44 |
stack = stack.tail |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
45 |
close(level) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
46 |
case _ => |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
47 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
48 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
49 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
50 |
def result(): Entry = |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
51 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
52 |
close(_ => true) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
53 |
val (_, name, body) = stack.head |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
54 |
Block(name, body.toList) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
55 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
56 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
57 |
def add(command: Command) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
58 |
{ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
59 |
syntax.heading_level(command) match { |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
60 |
case Some(i) => |
40457 | 61 |
close(_ >= i) |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
62 |
stack = (i, command.source, buffer()) :: stack |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
63 |
case None => |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
64 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
65 |
stack.head._3 += Atom(command) |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
66 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
67 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
68 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
69 |
/* result structure */ |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
70 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
71 |
val spans = parse_spans(syntax.scan(text)) |
45644 | 72 |
spans.foreach(span => add(Command(node_name, span))) |
40454
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
73 |
result() |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
74 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
75 |
} |
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
76 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
77 |
|
2516ea25a54b
some support for nested source structure, based on section headings;
wenzelm
parents:
38878
diff
changeset
|
78 |
|
38374 | 79 |
/** parse spans **/ |
80 |
||
38373 | 81 |
def parse_spans(toks: List[Token]): List[List[Token]] = |
34268 | 82 |
{ |
38373 | 83 |
val result = new mutable.ListBuffer[List[Token]] |
38239
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
84 |
val span = new mutable.ListBuffer[Token] |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
85 |
val whitespace = new mutable.ListBuffer[Token] |
34268 | 86 |
|
38239
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
87 |
def flush(buffer: mutable.ListBuffer[Token]) |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
88 |
{ |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
89 |
if (!buffer.isEmpty) { result += buffer.toList; buffer.clear } |
34268 | 90 |
} |
38239
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
91 |
for (tok <- toks) { |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
92 |
if (tok.is_command) { flush(span); flush(whitespace); span += tok } |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
93 |
else if (tok.is_ignored) whitespace += tok |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
94 |
else { span ++= whitespace; whitespace.clear; span += tok } |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
95 |
} |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
96 |
flush(span); flush(whitespace) |
89a4d1028fb3
parse_spans: somewhat faster low-level implementation;
wenzelm
parents:
36956
diff
changeset
|
97 |
result.toList |
34268 | 98 |
} |
38374 | 99 |
|
100 |
||
101 |
||
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
102 |
/** perspective **/ |
44388 | 103 |
|
104 |
def command_perspective(node: Document.Node, perspective: Text.Perspective): Command.Perspective = |
|
105 |
{ |
|
44474 | 106 |
if (perspective.is_empty) Command.Perspective.empty |
44388 | 107 |
else { |
108 |
val result = new mutable.ListBuffer[Command] |
|
109 |
@tailrec |
|
110 |
def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)]) |
|
111 |
{ |
|
112 |
(ranges, commands) match { |
|
113 |
case (range :: more_ranges, (command, offset) #:: more_commands) => |
|
114 |
val command_range = command.range + offset |
|
115 |
range compare command_range match { |
|
116 |
case -1 => check_ranges(more_ranges, commands) |
|
117 |
case 0 => |
|
118 |
result += command |
|
119 |
check_ranges(ranges, more_commands) |
|
120 |
case 1 => check_ranges(ranges, more_commands) |
|
121 |
} |
|
122 |
case _ => |
|
123 |
} |
|
124 |
} |
|
44473 | 125 |
check_ranges(perspective.ranges, node.command_range(perspective.range).toStream) |
44474 | 126 |
Command.Perspective(result.toList) |
44388 | 127 |
} |
128 |
} |
|
129 |
||
44615 | 130 |
def update_perspective(nodes: Document.Nodes, |
131 |
name: Document.Node.Name, text_perspective: Text.Perspective) |
|
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
132 |
: (Command.Perspective, Option[Document.Nodes]) = |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
133 |
{ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
134 |
val node = nodes(name) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
135 |
val perspective = command_perspective(node, text_perspective) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
136 |
val new_nodes = |
44474 | 137 |
if (node.perspective same perspective) None |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
138 |
else Some(nodes + (name -> node.copy(perspective = perspective))) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
139 |
(perspective, new_nodes) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
140 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
141 |
|
44615 | 142 |
def edit_perspective(previous: Document.Version, |
143 |
name: Document.Node.Name, text_perspective: Text.Perspective) |
|
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
144 |
: (Command.Perspective, Document.Version) = |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
145 |
{ |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
146 |
val nodes = previous.nodes |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
147 |
val (perspective, new_nodes) = update_perspective(nodes, name, text_perspective) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
148 |
val version = Document.Version(Document.new_id(), new_nodes getOrElse nodes) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
149 |
(perspective, version) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
150 |
} |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
151 |
|
44388 | 152 |
|
153 |
||
38374 | 154 |
/** text edits **/ |
155 |
||
43722 | 156 |
def text_edits( |
157 |
syntax: Outer_Syntax, |
|
158 |
previous: Document.Version, |
|
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
159 |
edits: List[Document.Edit_Text]) |
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
160 |
: (List[Document.Edit_Command], Document.Version) = |
38374 | 161 |
{ |
162 |
/* phase 1: edit individual command source */ |
|
163 |
||
38425 | 164 |
@tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]) |
38374 | 165 |
: Linear_Set[Command] = |
166 |
{ |
|
167 |
eds match { |
|
168 |
case e :: es => |
|
169 |
Document.Node.command_starts(commands.iterator).find { |
|
170 |
case (cmd, cmd_start) => |
|
171 |
e.can_edit(cmd.source, cmd_start) || |
|
172 |
e.is_insert && e.start == cmd_start + cmd.length |
|
173 |
} match { |
|
174 |
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) => |
|
175 |
val (rest, text) = e.edit(cmd.source, cmd_start) |
|
176 |
val new_commands = commands.insert_after(Some(cmd), Command.unparsed(text)) - cmd |
|
177 |
edit_text(rest.toList ::: es, new_commands) |
|
178 |
||
179 |
case Some((cmd, cmd_start)) => |
|
180 |
edit_text(es, commands.insert_after(Some(cmd), Command.unparsed(e.text))) |
|
181 |
||
182 |
case None => |
|
183 |
require(e.is_insert && e.start == 0) |
|
184 |
edit_text(es, commands.insert_after(None, Command.unparsed(e.text))) |
|
185 |
} |
|
186 |
case Nil => commands |
|
187 |
} |
|
188 |
} |
|
189 |
||
190 |
||
191 |
/* phase 2: recover command spans */ |
|
192 |
||
44615 | 193 |
@tailrec def recover_spans(node_name: Document.Node.Name, commands: Linear_Set[Command]) |
44607
274eff0ea12e
maintain name of *the* enclosing node as part of command -- avoid full document traversal;
wenzelm
parents:
44474
diff
changeset
|
194 |
: Linear_Set[Command] = |
38374 | 195 |
{ |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44185
diff
changeset
|
196 |
commands.iterator.find(cmd => !cmd.is_defined) match { |
38374 | 197 |
case Some(first_unparsed) => |
198 |
val first = |
|
38878
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
wenzelm
parents:
38569
diff
changeset
|
199 |
commands.reverse_iterator(first_unparsed). |
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
wenzelm
parents:
38569
diff
changeset
|
200 |
dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.head |
38374 | 201 |
val last = |
38878
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
wenzelm
parents:
38569
diff
changeset
|
202 |
commands.iterator(first_unparsed). |
1d5b3175fd30
text_edits/recover_spans: reparse at least until line boundary -- increases chance of recovery for bad ML text, for example;
wenzelm
parents:
38569
diff
changeset
|
203 |
dropWhile(_.newlines == 0).find(_.is_command) getOrElse commands.last |
38374 | 204 |
val range = |
205 |
commands.iterator(first).takeWhile(_ != last).toList ::: List(last) |
|
206 |
||
207 |
val sources = range.flatMap(_.span.map(_.source)) |
|
43647 | 208 |
val spans0 = parse_spans(syntax.scan(sources.mkString)) |
38374 | 209 |
|
210 |
val (before_edit, spans1) = |
|
211 |
if (!spans0.isEmpty && first.is_command && first.span == spans0.head) |
|
212 |
(Some(first), spans0.tail) |
|
213 |
else (commands.prev(first), spans0) |
|
214 |
||
215 |
val (after_edit, spans2) = |
|
216 |
if (!spans1.isEmpty && last.is_command && last.span == spans1.last) |
|
217 |
(Some(last), spans1.take(spans1.length - 1)) |
|
218 |
else (commands.next(last), spans1) |
|
219 |
||
45644 | 220 |
val inserted = spans2.map(span => Command(Document.new_id(), node_name, span)) |
38374 | 221 |
val new_commands = |
222 |
commands.delete_between(before_edit, after_edit).append_after(before_edit, inserted) |
|
44607
274eff0ea12e
maintain name of *the* enclosing node as part of command -- avoid full document traversal;
wenzelm
parents:
44474
diff
changeset
|
223 |
recover_spans(node_name, new_commands) |
38374 | 224 |
|
225 |
case None => commands |
|
226 |
} |
|
227 |
} |
|
228 |
||
229 |
||
230 |
/* resulting document edits */ |
|
231 |
||
232 |
{ |
|
40479 | 233 |
val doc_edits = new mutable.ListBuffer[Document.Edit_Command] |
38417 | 234 |
var nodes = previous.nodes |
38374 | 235 |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
236 |
edits foreach { |
44185 | 237 |
case (name, Document.Node.Clear()) => |
238 |
doc_edits += (name -> Document.Node.Clear()) |
|
44443
35d67b2056cc
clarified Document.Node.clear -- retain header (cf. ML version);
wenzelm
parents:
44436
diff
changeset
|
239 |
nodes += (name -> nodes(name).clear) |
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
240 |
|
44156 | 241 |
case (name, Document.Node.Edits(text_edits)) => |
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
242 |
val node = nodes(name) |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43662
diff
changeset
|
243 |
val commands0 = node.commands |
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
244 |
val commands1 = edit_text(text_edits, commands0) |
44607
274eff0ea12e
maintain name of *the* enclosing node as part of command -- avoid full document traversal;
wenzelm
parents:
44474
diff
changeset
|
245 |
val commands2 = recover_spans(name, commands1) // FIXME somewhat slow |
38374 | 246 |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
247 |
val removed_commands = commands0.iterator.filter(!commands2.contains(_)).toList |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
248 |
val inserted_commands = commands2.iterator.filter(!commands0.contains(_)).toList |
38374 | 249 |
|
40478
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
250 |
val cmd_edits = |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
251 |
removed_commands.reverse.map(cmd => (commands0.prev(cmd), None)) ::: |
4bae781b8f7c
replaced Document.Node_Text_Edit by Document.Text_Edit, with treatment of deleted nodes;
wenzelm
parents:
40457
diff
changeset
|
252 |
inserted_commands.map(cmd => (commands2.prev(cmd), Some(cmd))) |
38374 | 253 |
|
44156 | 254 |
doc_edits += (name -> Document.Node.Edits(cmd_edits)) |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
255 |
nodes += (name -> node.copy(commands = commands2)) |
43722 | 256 |
|
44182 | 257 |
case (name, Document.Node.Header(header)) => |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
258 |
val node = nodes(name) |
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
259 |
val update_header = |
44182 | 260 |
(node.header, header) match { |
261 |
case (Exn.Res(thy_header0), Exn.Res(thy_header)) => thy_header0 != thy_header |
|
44160
8848867501fb
clarified document model header: master_dir (native wrt. editor, potentially URL) and node_name (full canonical path);
wenzelm
parents:
44157
diff
changeset
|
262 |
case _ => true |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
263 |
} |
44180 | 264 |
if (update_header) { |
44182 | 265 |
doc_edits += (name -> Document.Node.Header(header)) |
44180 | 266 |
nodes += (name -> node.copy(header = header)) |
267 |
} |
|
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44185
diff
changeset
|
268 |
|
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44185
diff
changeset
|
269 |
case (name, Document.Node.Perspective(text_perspective)) => |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
270 |
update_perspective(nodes, name, text_perspective) match { |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
271 |
case (_, None) => |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
272 |
case (perspective, Some(nodes1)) => |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
273 |
doc_edits += (name -> Document.Node.Perspective(perspective)) |
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
274 |
nodes = nodes1 |
44385
e7fdb008aa7d
propagate editor perspective through document model;
wenzelm
parents:
44185
diff
changeset
|
275 |
} |
38374 | 276 |
} |
44157
a21d3e1e64fd
uniform treatment of header edits as document edits;
wenzelm
parents:
44156
diff
changeset
|
277 |
(doc_edits.toList, Document.Version(Document.new_id(), nodes)) |
38374 | 278 |
} |
279 |
} |
|
34268 | 280 |
} |