author | wenzelm |
Fri, 09 Jun 2017 21:43:31 +0200 | |
changeset 66054 | fb0eea226a4d |
parent 65926 | 0f7821a07aa9 |
child 66100 | d1ad5a7458c2 |
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 |
|
64877 | 42 |
def try_get_text(range: Text.Range): Option[String] = doc.try_get_text(range) |
64833 | 43 |
|
64830 | 44 |
lazy val bytes: Bytes = Bytes(text) |
45 |
lazy val chunk: Symbol.Text_Chunk = Symbol.Text_Chunk(text) |
|
64833 | 46 |
lazy val bibtex_entries: List[Text.Info[String]] = |
47 |
try { Bibtex.document_entries(text) } |
|
48 |
catch { case ERROR(_) => Nil } |
|
64830 | 49 |
} |
50 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
51 |
def init(session: Session, node_name: Document.Node.Name): Document_Model = |
65923 | 52 |
Document_Model(session, node_name, Content.empty) |
64707 | 53 |
} |
54 |
||
64710 | 55 |
sealed case class Document_Model( |
64707 | 56 |
session: Session, |
57 |
node_name: Document.Node.Name, |
|
64830 | 58 |
content: Document_Model.Content, |
64726 | 59 |
external_file: Boolean = false, |
64707 | 60 |
node_required: Boolean = false, |
61 |
last_perspective: Document.Node.Perspective_Text = Document.Node.no_perspective_text, |
|
64816 | 62 |
pending_edits: List[Text.Edit] = Nil, |
65095 | 63 |
published_diagnostics: List[Text.Info[Command.Results]] = Nil, |
64 |
published_decorations: List[Document_Model.Decoration] = Nil) extends Document.Model |
|
64605 | 65 |
{ |
64710 | 66 |
/* external file */ |
67 |
||
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
68 |
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
|
69 |
|
64800 | 70 |
def node_visible: Boolean = !external_file |
71 |
||
64710 | 72 |
|
64690 | 73 |
/* header */ |
74 |
||
64702 | 75 |
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
|
76 |
resources.special_header(node_name) getOrElse |
65359 | 77 |
resources.check_thy_reader(node_name, Scan.char_reader(content.text)) |
64605 | 78 |
|
79 |
||
64707 | 80 |
/* perspective */ |
81 |
||
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
82 |
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
|
83 |
: (Boolean, Document.Node.Perspective_Text) = |
64800 | 84 |
{ |
85 |
if (is_theory) { |
|
86 |
val snapshot = this.snapshot() |
|
87 |
||
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
88 |
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
|
89 |
val caret_range = |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
90 |
if (caret_perspective != 0) { |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
91 |
caret match { |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
92 |
case Some(pos) => |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
93 |
val doc = content.doc |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
94 |
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
|
95 |
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
|
96 |
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
|
97 |
case None => Text.Range.offside |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
98 |
} |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
99 |
} |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
100 |
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
|
101 |
else Text.Range.offside |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
102 |
|
64800 | 103 |
val text_perspective = |
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
104 |
if (snapshot.commands_loading_ranges(resources.visible_node(_)).nonEmpty) |
64800 | 105 |
Text.Perspective.full |
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
106 |
else |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
107 |
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
|
108 |
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
|
109 |
case None => Text.Perspective.empty |
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
110 |
} |
64723 | 111 |
|
64800 | 112 |
(snapshot.node.load_commands_changed(doc_blobs), |
113 |
Document.Node.Perspective(node_required, text_perspective, Document.Node.Overlays.empty)) |
|
114 |
} |
|
115 |
else (false, Document.Node.no_perspective_text) |
|
116 |
} |
|
117 |
||
64707 | 118 |
|
64800 | 119 |
/* blob */ |
120 |
||
121 |
def get_blob: Option[Document.Blob] = |
|
122 |
if (is_theory) None |
|
64830 | 123 |
else Some((Document.Blob(content.bytes, content.chunk, pending_edits.nonEmpty))) |
64707 | 124 |
|
125 |
||
64605 | 126 |
/* edits */ |
127 |
||
65160 | 128 |
def change_text(text: String, range: Option[Line.Range] = None): Option[Document_Model] = |
64709 | 129 |
{ |
65160 | 130 |
val insert = Line.normalize(text) |
131 |
range match { |
|
132 |
case None => |
|
133 |
Text.Edit.replace(0, content.text, insert) match { |
|
134 |
case Nil => None |
|
135 |
case edits => |
|
65196
e8760a98db78
discontinued pointless Text.Length: Javascript and Java agree in old-fashioned UTF-16;
wenzelm
parents:
65191
diff
changeset
|
136 |
val content1 = Document_Model.Content(Line.Document(insert)) |
65160 | 137 |
Some(copy(content = content1, pending_edits = pending_edits ::: edits)) |
138 |
} |
|
139 |
case Some(remove) => |
|
140 |
content.doc.change(remove, insert) match { |
|
141 |
case None => error("Failed to apply document change: " + remove) |
|
142 |
case Some((Nil, _)) => None |
|
143 |
case Some((edits, doc1)) => |
|
144 |
val content1 = Document_Model.Content(doc1) |
|
145 |
Some(copy(content = content1, pending_edits = pending_edits ::: edits)) |
|
146 |
} |
|
64709 | 147 |
} |
148 |
} |
|
149 |
||
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
150 |
def flush_edits(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
|
151 |
: Option[(List[Document.Edit_Text], Document_Model)] = |
64707 | 152 |
{ |
65926
0f7821a07aa9
restricted perspective depending on the caret -- important for reactivity when editing big files;
wenzelm
parents:
65923
diff
changeset
|
153 |
val (reparse, perspective) = node_perspective(doc_blobs, caret) |
64800 | 154 |
if (reparse || pending_edits.nonEmpty || last_perspective != perspective) { |
64867 | 155 |
val edits = node_edits(node_header, pending_edits, perspective) |
64816 | 156 |
Some((edits, copy(pending_edits = Nil, last_perspective = perspective))) |
64605 | 157 |
} |
64707 | 158 |
else None |
159 |
} |
|
64605 | 160 |
|
161 |
||
65095 | 162 |
/* publish annotations */ |
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
163 |
|
65115 | 164 |
def publish(rendering: VSCode_Rendering): |
165 |
(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
|
166 |
{ |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
167 |
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
|
168 |
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
|
169 |
if (node_visible) rendering.decorations |
65140 | 170 |
else { for (deco <- published_decorations) yield Document_Model.Decoration.empty(deco.typ) } |
65095 | 171 |
|
65115 | 172 |
val changed_diagnostics = |
173 |
if (diagnostics == published_diagnostics) None else Some(diagnostics) |
|
174 |
val changed_decorations = |
|
175 |
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
|
176 |
else if (published_decorations.isEmpty) Some(decorations) |
65115 | 177 |
else Some(for { (a, b) <- decorations zip published_decorations if a != b } yield a) |
178 |
||
179 |
(changed_diagnostics, changed_decorations, |
|
180 |
copy(published_diagnostics = diagnostics, published_decorations = decorations)) |
|
64679
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
181 |
} |
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
182 |
|
b2bf280b7e13
more uniform treatment of input/output wrt. client;
wenzelm
parents:
64673
diff
changeset
|
183 |
|
64725 | 184 |
/* prover session */ |
64702 | 185 |
|
186 |
def resources: VSCode_Resources = session.resources.asInstanceOf[VSCode_Resources] |
|
64605 | 187 |
|
64816 | 188 |
def is_stable: Boolean = pending_edits.isEmpty |
189 |
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
|
190 |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65161
diff
changeset
|
191 |
def rendering(snapshot: Document.Snapshot): VSCode_Rendering = |
65213
51c0f094dc02
proper local debugger state, depending on session;
wenzelm
parents:
65199
diff
changeset
|
192 |
new VSCode_Rendering(this, snapshot) |
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65161
diff
changeset
|
193 |
def rendering(): VSCode_Rendering = rendering(snapshot()) |
66054
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
194 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
195 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
196 |
/* syntax */ |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
197 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
198 |
lazy private val syntax0 = Outer_Syntax.init() |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
199 |
|
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
200 |
def syntax(): Outer_Syntax = |
fb0eea226a4d
more uniform syntax_completion + semantic_completion;
wenzelm
parents:
65926
diff
changeset
|
201 |
if (is_theory) session.recent_syntax(node_name) else syntax0 |
64605 | 202 |
} |