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