author | wenzelm |
Mon, 03 Apr 2017 17:00:36 +0200 | |
changeset 65361 | ecefb68dc21d |
parent 65359 | 9ca34f0407a9 |
child 65441 | 9425e4d8bdb6 |
permissions | -rw-r--r-- |
64623 | 1 |
/* Title: Tools/VSCode/src/vscode_resources.scala |
64605 | 2 |
Author: Makarius |
3 |
||
64729 | 4 |
Resources for VSCode Language Server: file-system access and global state. |
64605 | 5 |
*/ |
6 |
||
7 |
package isabelle.vscode |
|
8 |
||
9 |
||
10 |
import isabelle._ |
|
11 |
||
12 |
import java.io.{File => JFile} |
|
13 |
||
64824 | 14 |
import scala.util.parsing.input.Reader |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
15 |
|
64605 | 16 |
|
64623 | 17 |
object VSCode_Resources |
64605 | 18 |
{ |
64703 | 19 |
/* internal state */ |
20 |
||
21 |
sealed case class State( |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
22 |
models: Map[JFile, Document_Model] = Map.empty, |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
23 |
pending_input: Set[JFile] = Set.empty, |
65189 | 24 |
pending_output: Set[JFile] = Set.empty, |
25 |
caret: Option[(JFile, Line.Position)] = None) |
|
64800 | 26 |
{ |
65113 | 27 |
def update_models(changed: Traversable[(JFile, Document_Model)]): State = |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
28 |
copy( |
65113 | 29 |
models = models ++ changed, |
30 |
pending_input = (pending_input /: changed) { case (set, (file, _)) => set + file }, |
|
31 |
pending_output = (pending_output /: changed) { case (set, (file, _)) => set + file }) |
|
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
32 |
|
64800 | 33 |
lazy val document_blobs: Document.Blobs = |
34 |
Document.Blobs( |
|
35 |
(for { |
|
36 |
(_, model) <- models.iterator |
|
37 |
blob <- model.get_blob |
|
38 |
} yield (model.node_name -> blob)).toMap) |
|
39 |
} |
|
64605 | 40 |
} |
41 |
||
64623 | 42 |
class VSCode_Resources( |
65361 | 43 |
val options: Options, |
44 |
session_base: Sessions.Base, |
|
45 |
log: Logger = No_Logger) |
|
46 |
extends Resources(session_name = "", session_base, log) |
|
64605 | 47 |
{ |
64703 | 48 |
private val state = Synchronized(VSCode_Resources.State()) |
49 |
||
50 |
||
65137 | 51 |
/* options */ |
52 |
||
53 |
def pide_extensions: Boolean = options.bool("vscode_pide_extensions") |
|
54 |
def unicode_symbols: Boolean = options.bool("vscode_unicode_symbols") |
|
55 |
def tooltip_margin: Int = options.int("vscode_tooltip_margin") |
|
56 |
def message_margin: Int = options.int("vscode_message_margin") |
|
57 |
||
58 |
||
64703 | 59 |
/* document node name */ |
60 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
61 |
def node_file(name: Document.Node.Name): JFile = new JFile(name.node) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
62 |
|
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
63 |
def node_name(file: JFile): Document.Node.Name = |
64605 | 64 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
65 |
val node = file.getPath |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
66 |
val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("") |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
67 |
val master_dir = if (theory == "") "" else file.getParent |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
68 |
Document.Node.Name(node, master_dir, theory) |
64605 | 69 |
} |
64703 | 70 |
|
64759 | 71 |
override def append(dir: String, source_path: Path): String = |
64716 | 72 |
{ |
64759 | 73 |
val path = source_path.expand |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
74 |
if (dir == "" || path.is_absolute) File.platform_path(path) |
64759 | 75 |
else if (path.is_current) dir |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
76 |
else if (path.is_basic && !dir.endsWith("/") && !dir.endsWith(JFile.separator)) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
77 |
dir + JFile.separator + File.platform_path(path) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
78 |
else if (path.is_basic) dir + File.platform_path(path) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
79 |
else new JFile(dir + JFile.separator + File.platform_path(path)).getCanonicalPath |
64716 | 80 |
} |
81 |
||
64834 | 82 |
def get_model(file: JFile): Option[Document_Model] = state.value.models.get(file) |
83 |
def get_model(name: Document.Node.Name): Option[Document_Model] = get_model(node_file(name)) |
|
84 |
||
85 |
||
86 |
/* file content */ |
|
87 |
||
88 |
def read_file_content(file: JFile): Option[String] = |
|
89 |
try { Some(Line.normalize(File.read(file))) } |
|
90 |
catch { case ERROR(_) => None } |
|
91 |
||
92 |
def get_file_content(file: JFile): Option[String] = |
|
93 |
get_model(file) match { |
|
94 |
case Some(model) => Some(model.content.text) |
|
95 |
case None => read_file_content(file) |
|
96 |
} |
|
97 |
||
98 |
def bibtex_entries_iterator(): Iterator[Text.Info[(String, Document_Model)]] = |
|
99 |
for { |
|
100 |
(_, model) <- state.value.models.iterator |
|
65132 | 101 |
info <- model.content.bibtex_entries.iterator |
102 |
} yield info.map((_, model)) |
|
64834 | 103 |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
104 |
override def with_thy_reader[A](name: Document.Node.Name, f: Reader[Char] => A): A = |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
105 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
106 |
val file = node_file(name) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
107 |
get_model(file) match { |
64830 | 108 |
case Some(model) => f(Scan.char_reader(model.content.text)) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
109 |
case None if file.isFile => |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
110 |
val reader = Scan.byte_reader(file) |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
111 |
try { f(reader) } finally { reader.close } |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
112 |
case None => |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
113 |
error("No such file: " + quote(file.toString)) |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
114 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
115 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
116 |
|
64703 | 117 |
|
118 |
/* document models */ |
|
119 |
||
64800 | 120 |
def visible_node(name: Document.Node.Name): Boolean = |
121 |
get_model(name) match { |
|
122 |
case Some(model) => model.node_visible |
|
123 |
case None => false |
|
124 |
} |
|
125 |
||
65160 | 126 |
def change_model(session: Session, file: JFile, text: String, range: Option[Line.Range] = None) |
64703 | 127 |
{ |
128 |
state.change(st => |
|
64709 | 129 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
130 |
val model = st.models.getOrElse(file, Document_Model.init(session, node_name(file))) |
65160 | 131 |
val model1 = (model.change_text(text, range) getOrElse model).external(false) |
65114
200b787ceb51
potentially redundant pending_output, for the sake of uniformity and reactivity;
wenzelm
parents:
65113
diff
changeset
|
132 |
st.update_models(Some(file -> model1)) |
64709 | 133 |
}) |
64703 | 134 |
} |
135 |
||
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
136 |
def close_model(file: JFile): Boolean = |
64710 | 137 |
state.change_result(st => |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
138 |
st.models.get(file) match { |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
139 |
case None => (false, st) |
65113 | 140 |
case Some(model) => (true, st.update_models(Some(file -> model.external(true)))) |
64710 | 141 |
}) |
142 |
||
65116 | 143 |
def sync_models(changed_files: Set[JFile]): Unit = |
144 |
state.change(st => |
|
64710 | 145 |
{ |
64719 | 146 |
val changed_models = |
64710 | 147 |
(for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
148 |
(file, model) <- st.models.iterator |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
149 |
if changed_files(file) && model.external_file |
64812 | 150 |
text <- read_file_content(file) |
65160 | 151 |
model1 <- model.change_text(text) |
65116 | 152 |
} yield (file, model1)).toList |
153 |
st.update_models(changed_models) |
|
64710 | 154 |
}) |
155 |
||
64703 | 156 |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
157 |
/* resolve dependencies */ |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
158 |
|
64857 | 159 |
def resolve_dependencies(session: Session, file_watcher: File_Watcher): (Boolean, Boolean) = |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
160 |
{ |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
161 |
state.change_result(st => |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
162 |
{ |
64800 | 163 |
/* theory files */ |
164 |
||
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
165 |
val thys = |
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
166 |
(for ((_, model) <- st.models.iterator if model.is_theory) |
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
167 |
yield (model.node_name, Position.none)).toList |
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
168 |
|
65359 | 169 |
val thy_files = thy_info.dependencies(thys).deps.map(_.name) |
64800 | 170 |
|
171 |
||
172 |
/* auxiliary files */ |
|
173 |
||
174 |
val stable_tip_version = |
|
64815 | 175 |
if (st.models.forall(entry => entry._2.is_stable)) |
64800 | 176 |
session.current_state().stable_tip_version |
177 |
else None |
|
178 |
||
179 |
val aux_files = |
|
180 |
stable_tip_version match { |
|
181 |
case Some(version) => undefined_blobs(version.nodes) |
|
182 |
case None => Nil |
|
183 |
} |
|
184 |
||
185 |
||
186 |
/* loaded models */ |
|
187 |
||
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
188 |
val loaded_models = |
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
189 |
(for { |
64800 | 190 |
node_name <- thy_files.iterator ++ aux_files.iterator |
191 |
file = node_file(node_name) |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
192 |
if !st.models.isDefinedAt(file) |
64857 | 193 |
text <- { file_watcher.register_parent(file); read_file_content(file) } |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
194 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
195 |
yield { |
64800 | 196 |
val model = Document_Model.init(session, node_name) |
65160 | 197 |
val model1 = (model.change_text(text) getOrElse model).external(true) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
198 |
(file, model1) |
65117 | 199 |
}).toList |
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
200 |
|
64800 | 201 |
val invoke_input = loaded_models.nonEmpty |
202 |
val invoke_load = stable_tip_version.isEmpty |
|
203 |
||
65113 | 204 |
((invoke_input, invoke_load), st.update_models(loaded_models)) |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
205 |
}) |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
206 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
207 |
|
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
208 |
|
64703 | 209 |
/* pending input */ |
210 |
||
211 |
def flush_input(session: Session) |
|
212 |
{ |
|
213 |
state.change(st => |
|
214 |
{ |
|
64719 | 215 |
val changed_models = |
64703 | 216 |
(for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
217 |
file <- st.pending_input.iterator |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
218 |
model <- st.models.get(file) |
64800 | 219 |
(edits, model1) <- model.flush_edits(st.document_blobs) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
220 |
} yield (edits, (file, model1))).toList |
64707 | 221 |
|
64800 | 222 |
session.update(st.document_blobs, changed_models.flatMap(_._1)) |
64703 | 223 |
st.copy( |
65112 | 224 |
models = st.models ++ changed_models.iterator.map(_._2), |
64703 | 225 |
pending_input = Set.empty) |
226 |
}) |
|
227 |
} |
|
228 |
||
229 |
||
230 |
/* pending output */ |
|
231 |
||
65118 | 232 |
def update_output(changed_nodes: Traversable[JFile]): Unit = |
64703 | 233 |
state.change(st => st.copy(pending_output = st.pending_output ++ changed_nodes)) |
234 |
||
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
235 |
def flush_output(channel: Channel): Boolean = |
64703 | 236 |
{ |
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
237 |
state.change_result(st => |
64703 | 238 |
{ |
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
239 |
val (postponed, flushed) = |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
240 |
(for { |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
241 |
file <- st.pending_output.iterator |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
242 |
model <- st.models.get(file) |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
243 |
} yield (file, model, model.rendering())).toList.partition(_._3.snapshot.is_outdated) |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
244 |
|
64703 | 245 |
val changed_iterator = |
246 |
for { |
|
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
247 |
(file, model, rendering) <- flushed.iterator |
65115 | 248 |
(changed_diags, changed_decos, model1) = model.publish(rendering) |
249 |
if changed_diags.isDefined || changed_decos.isDefined |
|
65095 | 250 |
} |
251 |
yield { |
|
65115 | 252 |
for (diags <- changed_diags) |
253 |
channel.write(Protocol.PublishDiagnostics(file, rendering.diagnostics_output(diags))) |
|
65137 | 254 |
if (pide_extensions) { |
255 |
for (decos <- changed_decos; deco <- decos) |
|
256 |
channel.write(rendering.decoration_output(deco).json(file)) |
|
257 |
} |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
258 |
(file, model1) |
64703 | 259 |
} |
65232
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
260 |
|
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
261 |
(postponed.nonEmpty, |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
262 |
st.copy( |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
263 |
models = st.models ++ changed_iterator, |
ca571c8c0788
avoid race condition between current_state().stable_tip_version and model.rendering();
wenzelm
parents:
65198
diff
changeset
|
264 |
pending_output = postponed.map(_._1).toSet)) |
64703 | 265 |
} |
266 |
) |
|
267 |
} |
|
64870 | 268 |
|
269 |
||
270 |
/* output text */ |
|
271 |
||
272 |
def output_text(s: String): String = |
|
65137 | 273 |
if (unicode_symbols) Symbol.decode(s) else Symbol.encode(s) |
64870 | 274 |
|
65107 | 275 |
def output_xml(xml: XML.Tree): String = |
276 |
output_text(XML.content(xml)) |
|
277 |
||
64870 | 278 |
def output_pretty(body: XML.Body, margin: Int): String = |
279 |
output_text(Pretty.string_of(body, margin)) |
|
65107 | 280 |
def output_pretty_tooltip(body: XML.Body): String = output_pretty(body, tooltip_margin) |
281 |
def output_pretty_message(body: XML.Body): String = output_pretty(body, message_margin) |
|
65142 | 282 |
|
283 |
||
65189 | 284 |
/* caret handling */ |
285 |
||
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
286 |
def update_caret(caret: Option[(JFile, Line.Position)]) |
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
287 |
{ state.change(_.copy(caret = caret)) } |
65189 | 288 |
|
65198 | 289 |
def caret_offset(): Option[(Document_Model, Text.Offset)] = |
65189 | 290 |
{ |
291 |
val st = state.value |
|
292 |
for { |
|
293 |
(file, pos) <- st.caret |
|
294 |
model <- st.models.get(file) |
|
65191
4c9c83311cad
dynamic output, depending on caret focus (see also Tools/jEdit/src/output_dockable.scala);
wenzelm
parents:
65189
diff
changeset
|
295 |
offset <- model.content.doc.offset(pos) |
65189 | 296 |
} |
65198 | 297 |
yield (model, offset) |
65189 | 298 |
} |
299 |
||
300 |
||
65142 | 301 |
/* spell checker */ |
302 |
||
303 |
val spell_checker = new Spell_Checker_Variable |
|
304 |
spell_checker.update(options) |
|
64605 | 305 |
} |