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