author | wenzelm |
Tue, 28 Aug 2018 11:28:38 +0200 | |
changeset 68828 | 7030922e91a1 |
parent 68381 | 2fd3a6d6ba2e |
child 69559 | 66c8dff9639f |
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 |
{ |
44436
546adfa8a6fc
update_perspective without actual edits, bypassing the full state assignment protocol;
wenzelm
parents:
44388
diff
changeset
|
16 |
/** perspective **/ |
44388 | 17 |
|
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
18 |
def command_perspective( |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
19 |
node: Document.Node, |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
20 |
perspective: Text.Perspective, |
52887 | 21 |
overlays: Document.Node.Overlays): (Command.Perspective, Command.Perspective) = |
44388 | 22 |
{ |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
23 |
if (perspective.is_empty && overlays.is_empty) |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
24 |
(Command.Perspective.empty, Command.Perspective.empty) |
44388 | 25 |
else { |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
26 |
val has_overlay = overlays.commands |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
27 |
val visible = new mutable.ListBuffer[Command] |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
28 |
val visible_overlay = new mutable.ListBuffer[Command] |
44388 | 29 |
@tailrec |
30 |
def check_ranges(ranges: List[Text.Range], commands: Stream[(Command, Text.Offset)]) |
|
31 |
{ |
|
32 |
(ranges, commands) match { |
|
33 |
case (range :: more_ranges, (command, offset) #:: more_commands) => |
|
34 |
val command_range = command.range + offset |
|
35 |
range compare command_range match { |
|
36 |
case 0 => |
|
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
37 |
visible += command |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
38 |
visible_overlay += command |
44388 | 39 |
check_ranges(ranges, more_commands) |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
40 |
case c => |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
41 |
if (has_overlay(command)) visible_overlay += command |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
42 |
|
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
43 |
if (c < 0) check_ranges(more_ranges, commands) |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
44 |
else check_ranges(ranges, more_commands) |
44388 | 45 |
} |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
46 |
|
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
47 |
case (Nil, (command, _) #:: more_commands) => |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
48 |
if (has_overlay(command)) visible_overlay += command |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
49 |
|
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
50 |
check_ranges(Nil, more_commands) |
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
51 |
|
44388 | 52 |
case _ => |
53 |
} |
|
54 |
} |
|
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
55 |
|
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
56 |
val commands = |
56373
0605d90be6fc
tuned signature -- more explicit iterator terminology;
wenzelm
parents:
56372
diff
changeset
|
57 |
(if (overlays.is_empty) node.command_iterator(perspective.range) |
0605d90be6fc
tuned signature -- more explicit iterator terminology;
wenzelm
parents:
56372
diff
changeset
|
58 |
else node.command_iterator()).toStream |
0605d90be6fc
tuned signature -- more explicit iterator terminology;
wenzelm
parents:
56372
diff
changeset
|
59 |
check_ranges(perspective.ranges, commands) |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
60 |
(Command.Perspective(visible.toList), Command.Perspective(visible_overlay.toList)) |
44388 | 61 |
} |
62 |
} |
|
63 |
||
64 |
||
65 |
||
59699 | 66 |
/** header edits: graph structure and outer syntax **/ |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
67 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
68 |
private def header_edits( |
56394
bbf4d512f395
clarified Version.syntax -- avoid guessing initial situation;
wenzelm
parents:
56393
diff
changeset
|
69 |
resources: Resources, |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
70 |
previous: Document.Version, |
55134
1b67b17cdad5
propagate update of outer syntax keywords: global propertiesChanged, buffer TokenMarker.markTokens, text area repainting;
wenzelm
parents:
55118
diff
changeset
|
71 |
edits: List[Document.Edit_Text]): |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
72 |
(List[Document.Node.Name], Document.Nodes, List[Document.Edit_Command]) = |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
73 |
{ |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
74 |
val syntax_changed0 = new mutable.ListBuffer[Document.Node.Name] |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
75 |
var nodes = previous.nodes |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
76 |
val doc_edits = new mutable.ListBuffer[Document.Edit_Command] |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
77 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
78 |
edits foreach { |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
79 |
case (name, Document.Node.Deps(header)) => |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
80 |
val node = nodes(name) |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
81 |
val update_header = |
59319 | 82 |
node.header.errors.nonEmpty || header.errors.nonEmpty || node.header != header |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
83 |
if (update_header) { |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
84 |
val node1 = node.update_header(header) |
59695 | 85 |
if (node.header.imports.map(_._1) != node1.header.imports.map(_._1) || |
60197 | 86 |
node.header.keywords != node1.header.keywords || |
63579 | 87 |
node.header.abbrevs != node1.header.abbrevs || |
60197 | 88 |
node.header.errors != node1.header.errors) syntax_changed0 += name |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
89 |
nodes += (name -> node1) |
48707
ba531af91148
simplified Document.Node.Header -- internalized errors;
wenzelm
parents:
48706
diff
changeset
|
90 |
doc_edits += (name -> Document.Node.Deps(header)) |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
91 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
92 |
case _ => |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
93 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
94 |
|
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
95 |
val syntax_changed = nodes.descendants(syntax_changed0.toList) |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
96 |
|
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
97 |
for (name <- syntax_changed) { |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
98 |
val node = nodes(name) |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
99 |
val syntax = |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
100 |
if (node.is_empty) None |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
101 |
else { |
59086
94b2690ad494
node-specific keywords, with session base syntax as default;
wenzelm
parents:
59077
diff
changeset
|
102 |
val header = node.header |
66721
ae38b8c0fdd9
more accurate node_syntax: avoid overall_syntax for PIDE edits;
wenzelm
parents:
66720
diff
changeset
|
103 |
val imports_syntax = |
66772 | 104 |
if (header.imports.nonEmpty) { |
105 |
Outer_Syntax.merge( |
|
106 |
header.imports.map(p => resources.session_base.node_syntax(nodes, p._1))) |
|
107 |
} |
|
108 |
else resources.session_base.overall_syntax |
|
66721
ae38b8c0fdd9
more accurate node_syntax: avoid overall_syntax for PIDE edits;
wenzelm
parents:
66720
diff
changeset
|
109 |
Some(imports_syntax + header) |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
110 |
} |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
111 |
nodes += (name -> node.update_syntax(syntax)) |
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
112 |
} |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
113 |
|
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
114 |
(syntax_changed, nodes, doc_edits.toList) |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
115 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
116 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
117 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
118 |
|
38374 | 119 |
/** text edits **/ |
120 |
||
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
121 |
/* edit individual command source */ |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
122 |
|
50761 | 123 |
@tailrec def edit_text(eds: List[Text.Edit], commands: Linear_Set[Command]): Linear_Set[Command] = |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
124 |
{ |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
125 |
eds match { |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
126 |
case e :: es => |
65074
df14a0e872e6
improved performance of remove, e.g. relevant for Theories_Dockable.purge;
wenzelm
parents:
64854
diff
changeset
|
127 |
def insert_text(cmd: Option[Command], text: String): Linear_Set[Command] = |
65341 | 128 |
if (text == "") commands else commands.insert_after(cmd, Command.text(text)) |
65074
df14a0e872e6
improved performance of remove, e.g. relevant for Theories_Dockable.purge;
wenzelm
parents:
64854
diff
changeset
|
129 |
|
52901
8be75f53db82
maintain commands together with index -- avoid redundant reconstruction of full_index;
wenzelm
parents:
52887
diff
changeset
|
130 |
Document.Node.Commands.starts(commands.iterator).find { |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
131 |
case (cmd, cmd_start) => |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
132 |
e.can_edit(cmd.source, cmd_start) || |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
133 |
e.is_insert && e.start == cmd_start + cmd.length |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
134 |
} match { |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
135 |
case Some((cmd, cmd_start)) if e.can_edit(cmd.source, cmd_start) => |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
136 |
val (rest, text) = e.edit(cmd.source, cmd_start) |
65074
df14a0e872e6
improved performance of remove, e.g. relevant for Theories_Dockable.purge;
wenzelm
parents:
64854
diff
changeset
|
137 |
val new_commands = insert_text(Some(cmd), text) - cmd |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
138 |
edit_text(rest.toList ::: es, new_commands) |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
139 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
140 |
case Some((cmd, cmd_start)) => |
65074
df14a0e872e6
improved performance of remove, e.g. relevant for Theories_Dockable.purge;
wenzelm
parents:
64854
diff
changeset
|
141 |
edit_text(es, insert_text(Some(cmd), e.text)) |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
142 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
143 |
case None => |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
144 |
require(e.is_insert && e.start == 0) |
65074
df14a0e872e6
improved performance of remove, e.g. relevant for Theories_Dockable.purge;
wenzelm
parents:
64854
diff
changeset
|
145 |
edit_text(es, insert_text(None, e.text)) |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
146 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
147 |
case Nil => commands |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
148 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
149 |
} |
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
150 |
|
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
151 |
|
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
152 |
/* reparse range of command spans */ |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
153 |
|
48748
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
154 |
@tailrec private def chop_common( |
55779 | 155 |
cmds: List[Command], |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
156 |
blobs_spans: List[(Command.Blobs_Info, Command_Span.Span)]) |
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
157 |
: (List[Command], List[(Command.Blobs_Info, Command_Span.Span)]) = |
55779 | 158 |
{ |
159 |
(cmds, blobs_spans) match { |
|
59803
88a89f01fc27
proper comparison of blobs_info (amending illtyped equality from 86a76300137e) -- avoid redundant update of unchanged commands;
wenzelm
parents:
59705
diff
changeset
|
160 |
case (cmd :: cmds, (blobs_info, span) :: rest) |
88a89f01fc27
proper comparison of blobs_info (amending illtyped equality from 86a76300137e) -- avoid redundant update of unchanged commands;
wenzelm
parents:
59705
diff
changeset
|
161 |
if cmd.blobs_info == blobs_info && cmd.span == span => chop_common(cmds, rest) |
55779 | 162 |
case _ => (cmds, blobs_spans) |
48748
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
163 |
} |
55779 | 164 |
} |
48748
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
165 |
|
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
166 |
private def reparse_spans( |
56208 | 167 |
resources: Resources, |
63584
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
wenzelm
parents:
63579
diff
changeset
|
168 |
syntax: Outer_Syntax, |
56336 | 169 |
get_blob: Document.Node.Name => Option[Document.Blob], |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
170 |
can_import: Document.Node.Name => Boolean, |
59699 | 171 |
node_name: Document.Node.Name, |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
172 |
commands: Linear_Set[Command], |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
173 |
first: Command, last: Command): Linear_Set[Command] = |
48748
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
174 |
{ |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
175 |
val cmds0 = commands.iterator(first, last).toList |
55779 | 176 |
val blobs_spans0 = |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
177 |
syntax.parse_spans(cmds0.iterator.map(_.source).mkString).map(span => |
59705 | 178 |
(Command.blobs_info(resources, syntax, get_blob, can_import, node_name, span), span)) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
179 |
|
55779 | 180 |
val (cmds1, blobs_spans1) = chop_common(cmds0, blobs_spans0) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
181 |
|
55779 | 182 |
val (rev_cmds2, rev_blobs_spans2) = chop_common(cmds1.reverse, blobs_spans1.reverse) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
183 |
val cmds2 = rev_cmds2.reverse |
55779 | 184 |
val blobs_spans2 = rev_blobs_spans2.reverse |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
185 |
|
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
186 |
cmds2 match { |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
187 |
case Nil => |
55779 | 188 |
assert(blobs_spans2.isEmpty) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
189 |
commands |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
190 |
case cmd :: _ => |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
191 |
val hook = commands.prev(cmd) |
54462 | 192 |
val inserted = |
59684 | 193 |
blobs_spans2.map({ case (blobs, span) => |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
194 |
Command(Document_ID.make(), node_name, blobs, span) }) |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
195 |
(commands /: cmds2)(_ - _).append_after(hook, inserted) |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
196 |
} |
48748
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
197 |
} |
89b4e7d83d6f
refined recover_spans: take visible range into account, reparse and trim results -- to improve editing experience wrt. unbalanced quotations etc.;
wenzelm
parents:
48747
diff
changeset
|
198 |
|
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
199 |
|
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
200 |
/* main */ |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
201 |
|
50761 | 202 |
def diff_commands(old_cmds: Linear_Set[Command], new_cmds: Linear_Set[Command]) |
52849 | 203 |
: List[Command.Edit] = |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
204 |
{ |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
205 |
val removed = old_cmds.iterator.filter(!new_cmds.contains(_)).toList |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
206 |
val inserted = new_cmds.iterator.filter(!old_cmds.contains(_)).toList |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
207 |
|
63866 | 208 |
removed.map(cmd => (old_cmds.prev(cmd), None)) reverse_::: |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
209 |
inserted.map(cmd => (new_cmds.prev(cmd), Some(cmd))) |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
210 |
} |
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
211 |
|
54513 | 212 |
private def text_edit( |
56208 | 213 |
resources: Resources, |
63584
68751fe1c036
tuned signature -- prover-independence is presently theoretical;
wenzelm
parents:
63579
diff
changeset
|
214 |
syntax: Outer_Syntax, |
56336 | 215 |
get_blob: Document.Node.Name => Option[Document.Blob], |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
216 |
can_import: Document.Node.Name => Boolean, |
54513 | 217 |
reparse_limit: Int, |
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
218 |
node: Document.Node, edit: Document.Edit_Text): Document.Node = |
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
219 |
{ |
59704 | 220 |
/* recover command spans after edits */ |
221 |
// FIXME somewhat slow |
|
222 |
def recover_spans( |
|
223 |
name: Document.Node.Name, |
|
224 |
perspective: Command.Perspective, |
|
225 |
commands: Linear_Set[Command]): Linear_Set[Command] = |
|
226 |
{ |
|
227 |
val is_visible = perspective.commands.toSet |
|
228 |
||
229 |
def next_invisible(cmds: Linear_Set[Command], from: Command): Command = |
|
230 |
cmds.iterator(from).dropWhile(cmd => !cmd.is_proper || is_visible(cmd)) |
|
231 |
.find(_.is_proper) getOrElse cmds.last |
|
232 |
||
233 |
@tailrec def recover(cmds: Linear_Set[Command]): Linear_Set[Command] = |
|
234 |
cmds.find(_.is_unparsed) match { |
|
235 |
case Some(first_unparsed) => |
|
236 |
val first = next_invisible(cmds.reverse, first_unparsed) |
|
237 |
val last = next_invisible(cmds, first_unparsed) |
|
238 |
recover( |
|
59705 | 239 |
reparse_spans(resources, syntax, get_blob, can_import, name, cmds, first, last)) |
59704 | 240 |
case None => cmds |
241 |
} |
|
242 |
recover(commands) |
|
243 |
} |
|
244 |
||
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
245 |
edit match { |
56335
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
56316
diff
changeset
|
246 |
case (_, Document.Node.Blob(blob)) => node.init_blob(blob) |
54562
301a721af68b
clarified node edits sent to prover -- Clear/Blob only required for text edits within editor;
wenzelm
parents:
54524
diff
changeset
|
247 |
|
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
248 |
case (name, Document.Node.Edits(text_edits)) => |
56335
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
56316
diff
changeset
|
249 |
if (name.is_theory) { |
55435
662e0fd39823
maintain blob edits within history, which is important for Snapshot.convert/revert;
wenzelm
parents:
55431
diff
changeset
|
250 |
val commands0 = node.commands |
662e0fd39823
maintain blob edits within history, which is important for Snapshot.convert/revert;
wenzelm
parents:
55431
diff
changeset
|
251 |
val commands1 = edit_text(text_edits, commands0) |
59705 | 252 |
val commands2 = recover_spans(name, node.perspective.visible, commands1) |
55435
662e0fd39823
maintain blob edits within history, which is important for Snapshot.convert/revert;
wenzelm
parents:
55431
diff
changeset
|
253 |
node.update_commands(commands2) |
662e0fd39823
maintain blob edits within history, which is important for Snapshot.convert/revert;
wenzelm
parents:
55431
diff
changeset
|
254 |
} |
56335
8953d4cc060a
store blob content within document node: aux. files that were once open are made persistent;
wenzelm
parents:
56316
diff
changeset
|
255 |
else node |
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
256 |
|
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
257 |
case (_, Document.Node.Deps(_)) => node |
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
258 |
|
52849 | 259 |
case (name, Document.Node.Perspective(required, text_perspective, overlays)) => |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
260 |
val (visible, visible_overlay) = command_perspective(node, text_perspective, overlays) |
52849 | 261 |
val perspective: Document.Node.Perspective_Command = |
52861
e93d73b51fd0
commands with overlay remain visible, to avoid loosing printed output;
wenzelm
parents:
52849
diff
changeset
|
262 |
Document.Node.Perspective(required, visible_overlay, overlays) |
59372
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
263 |
if (node.same_perspective(text_perspective, perspective)) node |
59703 | 264 |
else { |
265 |
/* consolidate unfinished spans */ |
|
266 |
val is_visible = visible.commands.toSet |
|
267 |
val commands = node.commands |
|
268 |
val commands1 = |
|
269 |
if (is_visible.isEmpty) commands |
|
270 |
else { |
|
271 |
commands.find(_.is_unfinished) match { |
|
272 |
case Some(first_unfinished) => |
|
273 |
commands.reverse.find(is_visible) match { |
|
274 |
case Some(last_visible) => |
|
275 |
val it = commands.iterator(last_visible) |
|
276 |
var last = last_visible |
|
277 |
var i = 0 |
|
278 |
while (i < reparse_limit && it.hasNext) { |
|
279 |
last = it.next |
|
280 |
i += last.length |
|
281 |
} |
|
59705 | 282 |
reparse_spans(resources, syntax, get_blob, can_import, |
283 |
name, commands, first_unfinished, last) |
|
59703 | 284 |
case None => commands |
285 |
} |
|
286 |
case None => commands |
|
287 |
} |
|
288 |
} |
|
289 |
node.update_perspective(text_perspective, perspective).update_commands(commands1) |
|
290 |
} |
|
48755
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
291 |
} |
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
292 |
} |
393a37003851
apply all text edits to each node, before determining the resulting doc_edits -- allow several iterations to consolidate spans etc.;
wenzelm
parents:
48754
diff
changeset
|
293 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
294 |
def parse_change( |
56208 | 295 |
resources: Resources, |
49524
68796a77c42b
Thy_Syntax.consolidate_spans is subject to editor_reparse_limit, for improved experience of unbalanced comments etc.;
wenzelm
parents:
49414
diff
changeset
|
296 |
reparse_limit: Int, |
43722 | 297 |
previous: Document.Version, |
54521
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
wenzelm
parents:
54519
diff
changeset
|
298 |
doc_blobs: Document.Blobs, |
68336
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents:
66772
diff
changeset
|
299 |
edits: List[Document.Edit_Text], |
68381
2fd3a6d6ba2e
less wasteful consolidation, based on PIDE front-end state and recent changes;
wenzelm
parents:
68336
diff
changeset
|
300 |
consolidate: List[Document.Node.Name]): Session.Change = |
38374 | 301 |
{ |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
302 |
val (syntax_changed, nodes0, doc_edits0) = header_edits(resources, previous, edits) |
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
303 |
|
56336 | 304 |
def get_blob(name: Document.Node.Name) = |
305 |
doc_blobs.get(name) orElse previous.nodes(name).get_blob |
|
306 |
||
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
307 |
def can_import(name: Document.Node.Name): Boolean = |
65361 | 308 |
resources.session_base.loaded_theory(name) || nodes0(name).has_header |
54518
81a9a54c57fc
always reparse nodes with thy_load commands, to update inlined files;
wenzelm
parents:
54517
diff
changeset
|
309 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
310 |
val (doc_edits, version) = |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
311 |
if (edits.isEmpty) (Nil, Document.Version.make(previous.nodes)) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
312 |
else { |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
313 |
val reparse = |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
314 |
(syntax_changed /: nodes0.iterator)({ |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
315 |
case (reparse, (name, node)) => |
64799 | 316 |
if (node.load_commands_changed(doc_blobs) && !reparse.contains(name)) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
317 |
name :: reparse |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
318 |
else reparse |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
319 |
}) |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
320 |
val reparse_set = reparse.toSet |
46946
acc8ebf980ca
more explicit header_edits before main text_edits;
wenzelm
parents:
46942
diff
changeset
|
321 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
322 |
var nodes = nodes0 |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
323 |
val doc_edits = new mutable.ListBuffer[Document.Edit_Command]; doc_edits ++= doc_edits0 |
54521
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
wenzelm
parents:
54519
diff
changeset
|
324 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
325 |
val node_edits = |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
326 |
(edits ::: reparse.map((_, Document.Node.Edits(Nil)))).groupBy(_._1) |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
327 |
.asInstanceOf[Map[Document.Node.Name, List[Document.Edit_Text]]] // FIXME ??? |
38374 | 328 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
329 |
node_edits foreach { |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
330 |
case (name, edits) => |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
331 |
val node = nodes(name) |
66770
122df1fde073
clarified node_syntax (amending ae38b8c0fdd9): default to overall_syntax, e.g. relevant for command spans wrt. bad header;
wenzelm
parents:
66721
diff
changeset
|
332 |
val syntax = resources.session_base.node_syntax(nodes, name) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
333 |
val commands = node.commands |
48754
c2c1e5944536
clarified undefined, unparsed, unfinished command spans;
wenzelm
parents:
48748
diff
changeset
|
334 |
|
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
335 |
val node1 = |
59319 | 336 |
if (reparse_set(name) && commands.nonEmpty) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
337 |
node.update_commands( |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
338 |
reparse_spans(resources, syntax, get_blob, can_import, name, |
59705 | 339 |
commands, commands.head, commands.last)) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
340 |
else node |
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
341 |
val node2 = |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
342 |
(node1 /: edits)( |
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
343 |
text_edit(resources, syntax, get_blob, can_import, reparse_limit, _, _)) |
59372
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
344 |
val node3 = |
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
345 |
if (reparse_set.contains(name)) |
59702
58dfaa369c11
hybrid use of command blobs: inlined errors and auxiliary files;
wenzelm
parents:
59699
diff
changeset
|
346 |
text_edit(resources, syntax, get_blob, can_import, reparse_limit, |
59372
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
347 |
node2, (name, node2.edit_perspective)) |
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
348 |
else node2 |
54521
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
wenzelm
parents:
54519
diff
changeset
|
349 |
|
60215 | 350 |
if (!node.same_perspective(node3.text_perspective, node3.perspective)) |
59372
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
351 |
doc_edits += (name -> node3.perspective) |
54521
744ea0025e11
clarified Document.Blobs environment vs. actual edits of auxiliary files;
wenzelm
parents:
54519
diff
changeset
|
352 |
|
59372
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
353 |
doc_edits += (name -> Document.Node.Edits(diff_commands(commands, node3.commands))) |
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
354 |
|
503739360344
proper update of perspective after implicit edit due to reparse (e.g. ~~/src/HOL/Nat.thy);
wenzelm
parents:
59319
diff
changeset
|
355 |
nodes += (name -> node3) |
56316
b1cf8ddc2e04
propagate deps_changed, to resolve missing files without requiring jEdit events (e.g. buffer load/save);
wenzelm
parents:
56315
diff
changeset
|
356 |
} |
59077
7e0d3da6e6d8
node-specific syntax, with base_syntax as default;
wenzelm
parents:
57906
diff
changeset
|
357 |
(doc_edits.toList.filterNot(_._2.is_void), Document.Version.make(nodes)) |
54513 | 358 |
} |
359 |
||
68336
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents:
66772
diff
changeset
|
360 |
Session.Change( |
09ac56914b29
Document.update includes node consolidation / presentation as regular print operation: avoid user operations on protocol thread;
wenzelm
parents:
66772
diff
changeset
|
361 |
previous, syntax_changed, syntax_changed.nonEmpty, doc_edits, consolidate, version) |
38374 | 362 |
} |
34268 | 363 |
} |