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