author | wenzelm |
Thu, 28 Dec 2017 22:53:45 +0100 | |
changeset 67292 | 386ddccfccbf |
parent 67265 | f32287c95432 |
child 67293 | 2fe338d91d47 |
permissions | -rw-r--r-- |
64605 | 1 |
/* Title: Tools/VSCode/src/document_model.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Document model for line-oriented text. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle.vscode |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
64710 | 12 |
import java.io.{File => JFile} |
13 |
||
64605 | 14 |
|
64707 | 15 |
object Document_Model |
16 |
{ |
|
65095 | 17 |
/* decorations */ |
18 |
||
65140 | 19 |
object Decoration |
20 |
{ |
|
21 |
def empty(typ: String): Decoration = Decoration(typ, Nil) |
|
22 |
||
23 |
def ranges(typ: String, ranges: List[Text.Range]): Decoration = |
|
24 |
Decoration(typ, ranges.map(Text.Info(_, List.empty[XML.Body]))) |
|
25 |
} |
|
65105 | 26 |
sealed case class Decoration(typ: String, content: List[Text.Info[List[XML.Body]]]) |
65095 | 27 |
|
28 |
||
29 |
/* content */ |
|
30 |
||
65923 | 31 |
object Content |
32 |
{ |
|
33 |
val empty: Content = Content(Line.Document.empty) |
|
34 |
} |
|
35 |
||
64830 | 36 |
sealed case class Content(doc: Line.Document) |
37 |
{ |
|
65161 | 38 |
override def toString: String = doc.toString |
65197 | 39 |
def text_length: Text.Offset = doc.text_length |
64830 | 40 |
def text_range: Text.Range = doc.text_range |
41 |
def text: String = doc.text |
|
64833 | 42 |
|
64830 | 43 |
lazy val bytes: Bytes = Bytes(text) |
44 |
lazy val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(text) |
|
64833 | 45 |
lazy val bibtex_entries: List[Text.Info[String]] = |
66150 | 46 |
try { Bibtex.entries(text) } |
64833 | 47 |
catch { case ERROR(_) => Nil } |
66676
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
48 |
|
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
49 |
def recode_symbols: List[Protocol.TextEdit] = |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
50 |
(for { |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
51 |
(line, l) <- doc.lines.iterator.zipWithIndex |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
52 |
text1 = Symbol.encode(line.text) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
53 |
if (line.text != text1) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
54 |
} yield { |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
55 |
val range = Line.Range(Line.Position(l), Line.Position(l, line.text.length)) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
56 |
Protocol.TextEdit(range, text1) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
57 |
}).toList |
64830 | 58 |
} |
59 |
||
66100 | 60 |
def init(session: Session, editor: Server.Editor, node_name: Document.Node.Name): Document_Model = |
67292
386ddccfccbf
implicit thy_load context for bibtex files (VSCode);
wenzelm
parents:
67265
diff
changeset
|
61 |
Document_Model(session, editor, node_name, Content.empty, |
386ddccfccbf
implicit thy_load context for bibtex files (VSCode);
wenzelm
parents:
67265
diff
changeset
|
62 |
node_required = Bibtex.is_bibtex_theory(node_name)) |
64707 | 63 |
} |
64 |
||
64710 | 65 |
sealed case class Document_Model( |
64707 | 66 |
session: Session, |
66100 | 67 |
editor: Server.Editor, |
64707 | 68 |
node_name: Document.Node.Name, |
64830 | 69 |
content: Document_Model.Content, |
66674 | 70 |
version: Option[Long] = None, |
64726 | 71 |
external_file: Boolean = false, |
64707 | 72 |
node_required: Boolean = false, |
73 |
last_perspective: Document.Node.Perspective_Text = Document.Node.no_perspective_text, |
|
64816 | 74 |
pending_edits: List[Text.Edit] = Nil, |
65095 | 75 |
published_diagnostics: List[Text.Info[Command.Results]] = Nil, |
76 |
published_decorations: List[Document_Model.Decoration] = Nil) extends Document.Model |
|
64605 | 77 |
{ |
66114 | 78 |
model => |
79 |
||
80 |
||
66674 | 81 |
/* content */ |
66114 | 82 |
|
67014 | 83 |
def get_text(range: Text.Range): Option[String] = content.doc.get_text(range) |
66114 | 84 |
|
66674 | 85 |
def set_version(new_version: Long): Document_Model = copy(version = Some(new_version)) |
86 |
||
66114 | 87 |
|
64710 | 88 |
/* external file */ |
89 |
||
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
90 |
def external(b: Boolean): Document_Model = copy(external_file = b) |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
91 |
|
64800 | 92 |
def node_visible: Boolean = !external_file |
93 |
||
64710 | 94 |
|
64690 | 95 |
/* header */ |
96 |
||
64702 | 97 |
def node_header: Document.Node.Header = |
64673
b5965890e54d
more uniform treatment of file name vs. theory name and special header;
wenzelm
parents:
64672
diff
changeset
|
98 |
resources.special_header(node_name) getOrElse |
65359 | 99 |
resources.check_thy_reader(node_name, Scan.char_reader(content.text)) |
64605 | 100 |
|
101 |
||
64707 | 102 |
/* perspective */ |
103 |
||
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
104 |
def node_perspective(doc_blobs: Document.Blobs, caret: Option[Line.Position]) |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
105 |
: (Boolean, Document.Node.Perspective_Text) = |
64800 | 106 |
{ |
107 |
if (is_theory) { |
|
66114 | 108 |
val snapshot = model.snapshot() |
64800 | 109 |
|
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
110 |
val caret_perspective = resources.options.int("vscode_caret_perspective") max 0 |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
111 |
val caret_range = |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
112 |
if (caret_perspective != 0) { |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
113 |
caret match { |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
114 |
case Some(pos) => |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
115 |
val doc = content.doc |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
116 |
val pos1 = Line.Position((pos.line - caret_perspective) max 0) |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
117 |
val pos2 = Line.Position((pos.line + caret_perspective + 1) min doc.lines.length) |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
118 |
Text.Range(doc.offset(pos1).get, doc.offset(pos2).get) |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
119 |
case None => Text.Range.offside |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
120 |
} |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
121 |
} |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
122 |
else if (node_visible) content.text_range |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
123 |
else Text.Range.offside |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
124 |
|
64800 | 125 |
val text_perspective = |
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
126 |
if (snapshot.commands_loading_ranges(resources.visible_node(_)).nonEmpty) |
64800 | 127 |
Text.Perspective.full |
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
128 |
else |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
129 |
content.text_range.try_restrict(caret_range) match { |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
130 |
case Some(range) => Text.Perspective(List(range)) |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
131 |
case None => Text.Perspective.empty |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
132 |
} |
64723 | 133 |
|
66100 | 134 |
val overlays = editor.node_overlays(node_name) |
135 |
||
64800 | 136 |
(snapshot.node.load_commands_changed(doc_blobs), |
66100 | 137 |
Document.Node.Perspective(node_required, text_perspective, overlays)) |
64800 | 138 |
} |
139 |
else (false, Document.Node.no_perspective_text) |
|
140 |
} |
|
141 |
||
64707 | 142 |
|
64800 | 143 |
/* blob */ |
144 |
||
145 |
def get_blob: Option[Document.Blob] = |
|
146 |
if (is_theory) None |
|
67265
f32287c95432
store full blob source for the sake of markup_to_XML;
wenzelm
parents:
67014
diff
changeset
|
147 |
else Some((Document.Blob(content.bytes, content.text, content.chunk, pending_edits.nonEmpty))) |
64707 | 148 |
|
149 |
||
66150 | 150 |
/* bibtex entries */ |
151 |
||
152 |
def bibtex_entries: List[Text.Info[String]] = |
|
153 |
model.content.bibtex_entries |
|
154 |
||
155 |
||
64605 | 156 |
/* edits */ |
157 |
||
65160 | 158 |
def change_text(text: String, range: Option[Line.Range] = None): Option[Document_Model] = |
64709 | 159 |
{ |
65160 | 160 |
val insert = Line.normalize(text) |
161 |
range match { |
|
162 |
case None => |
|
163 |
Text.Edit.replace(0, content.text, insert) match { |
|
164 |
case Nil => None |
|
165 |
case edits => |
|
65196
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
wenzelm
parents:
65191
diff
changeset
|
166 |
val content1 = Document_Model.Content(Line.Document(insert)) |
65160 | 167 |
Some(copy(content = content1, pending_edits = pending_edits ::: edits)) |
168 |
} |
|
169 |
case Some(remove) => |
|
170 |
content.doc.change(remove, insert) match { |
|
171 |
case None => error("Failed to apply document change: " + remove) |
|
172 |
case Some((Nil, _)) => None |
|
173 |
case Some((edits, doc1)) => |
|
174 |
val content1 = Document_Model.Content(doc1) |
|
175 |
Some(copy(content = content1, pending_edits = pending_edits ::: edits)) |
|
176 |
} |
|
64709 | 177 |
} |
178 |
} |
|
179 |
||
66676
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
180 |
def flush_edits( |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
181 |
unicode_symbols: Boolean, |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
182 |
doc_blobs: Document.Blobs, |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
183 |
file: JFile, |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
184 |
caret: Option[Line.Position]) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
185 |
: Option[((List[Protocol.TextDocumentEdit], List[Document.Edit_Text]), Document_Model)] = |
64707 | 186 |
{ |
66676
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
187 |
val workspace_edits = |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
188 |
if (unicode_symbols && version.isDefined) { |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
189 |
val edits = content.recode_symbols |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
190 |
if (edits.nonEmpty) List(Protocol.TextDocumentEdit(file, version.get, edits)) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
191 |
else Nil |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
192 |
} |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
193 |
else Nil |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
194 |
|
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
195 |
val (reparse, perspective) = node_perspective(doc_blobs, caret) |
66676
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
196 |
if (reparse || pending_edits.nonEmpty || last_perspective != perspective || |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
197 |
workspace_edits.nonEmpty) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
198 |
{ |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
199 |
val prover_edits = node_edits(node_header, pending_edits, perspective) |
39db5bb7eb0a
recode Unicode text on the spot, e.g. from copy-paste of output;
wenzelm
parents:
66674
diff
changeset
|
200 |
val edits = (workspace_edits, prover_edits) |
64816 | 201 |
Some((edits, copy(pending_edits = Nil, last_perspective = perspective))) |
64605 | 202 |
} |
64707 | 203 |
else None |
204 |
} |
|
64605 | 205 |
|
206 |
||
65095 | 207 |
/* publish annotations */ |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
208 |
|
65115 | 209 |
def publish(rendering: VSCode_Rendering): |
210 |
(Option[List[Text.Info[Command.Results]]], Option[List[Document_Model.Decoration]], Document_Model) = |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
211 |
{ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
212 |
val diagnostics = rendering.diagnostics |
65120
db6cc23fcf33
proper reset of published decorations: initial value is Nil, afterwards it is a list of canonical length and order;
wenzelm
parents:
65115
diff
changeset
|
213 |
val decorations = |
db6cc23fcf33
proper reset of published decorations: initial value is Nil, afterwards it is a list of canonical length and order;
wenzelm
parents:
65115
diff
changeset
|
214 |
if (node_visible) rendering.decorations |
65140 | 215 |
else { for (deco <- published_decorations) yield Document_Model.Decoration.empty(deco.typ) } |
65095 | 216 |
|
65115 | 217 |
val changed_diagnostics = |
218 |
if (diagnostics == published_diagnostics) None else Some(diagnostics) |
|
219 |
val changed_decorations = |
|
220 |
if (decorations == published_decorations) None |
|
65120
db6cc23fcf33
proper reset of published decorations: initial value is Nil, afterwards it is a list of canonical length and order;
wenzelm
parents:
65115
diff
changeset
|
221 |
else if (published_decorations.isEmpty) Some(decorations) |
65115 | 222 |
else Some(for { (a, b) <- decorations zip published_decorations if a != b } yield a) |
223 |
||
224 |
(changed_diagnostics, changed_decorations, |
|
225 |
copy(published_diagnostics = diagnostics, published_decorations = decorations)) |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
226 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
227 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
228 |
|
64725 | 229 |
/* prover session */ |
64702 | 230 |
|
231 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
|
64605 | 232 |
|
64816 | 233 |
def is_stable: Boolean = pending_edits.isEmpty |
234 |
def snapshot(): Document.Snapshot = session.snapshot(node_name, pending_edits) |
|
64622
529bbb8977c7
more uniform rendering for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents:
64605
diff
changeset
|
235 |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65161
diff
changeset
|
236 |
def rendering(snapshot: Document.Snapshot): VSCode_Rendering = |
66114 | 237 |
new VSCode_Rendering(snapshot, model) |
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65161
diff
changeset
|
238 |
def rendering(): VSCode_Rendering = rendering(snapshot()) |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
239 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
240 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
241 |
/* syntax */ |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
242 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
243 |
def syntax(): Outer_Syntax = |
67004
af72fa58f71b
clarified lazy Completion within Outer_Syntax: measurable speedup of Sessions.deps;
wenzelm
parents:
66984
diff
changeset
|
244 |
if (is_theory) session.recent_syntax(node_name) else Outer_Syntax.empty |
64605 | 245 |
} |