author | wenzelm |
Sun, 05 Mar 2017 14:43:39 +0100 | |
changeset 65116 | 06d9bcb66ef3 |
parent 65115 | 93a0683e075a |
child 65117 | 29c19bc97d20 |
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, |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
24 |
pending_output: Set[JFile] = Set.empty) |
64800 | 25 |
{ |
65113 | 26 |
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
|
27 |
copy( |
65113 | 28 |
models = models ++ changed, |
29 |
pending_input = (pending_input /: changed) { case (set, (file, _)) => set + file }, |
|
30 |
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
|
31 |
|
64800 | 32 |
lazy val document_blobs: Document.Blobs = |
33 |
Document.Blobs( |
|
34 |
(for { |
|
35 |
(_, model) <- models.iterator |
|
36 |
blob <- model.get_blob |
|
37 |
} yield (model.node_name -> blob)).toMap) |
|
38 |
} |
|
64605 | 39 |
} |
40 |
||
64623 | 41 |
class VSCode_Resources( |
64854 | 42 |
val options: Options, |
43 |
val text_length: Text.Length, |
|
64856 | 44 |
base: Sessions.Base, |
64854 | 45 |
log: Logger = No_Logger) extends Resources(base, log) |
64605 | 46 |
{ |
64703 | 47 |
private val state = Synchronized(VSCode_Resources.State()) |
48 |
||
49 |
||
50 |
/* document node name */ |
|
51 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
52 |
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
|
53 |
|
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
54 |
def node_name(file: JFile): Document.Node.Name = |
64605 | 55 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
56 |
val node = file.getPath |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
57 |
val theory = Thy_Header.thy_name_bootstrap(node).getOrElse("") |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
58 |
val master_dir = if (theory == "") "" else file.getParent |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
59 |
Document.Node.Name(node, master_dir, theory) |
64605 | 60 |
} |
64703 | 61 |
|
64759 | 62 |
override def append(dir: String, source_path: Path): String = |
64716 | 63 |
{ |
64759 | 64 |
val path = source_path.expand |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
65 |
if (dir == "" || path.is_absolute) File.platform_path(path) |
64759 | 66 |
else if (path.is_current) dir |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
67 |
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
|
68 |
dir + JFile.separator + File.platform_path(path) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
69 |
else if (path.is_basic) dir + File.platform_path(path) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
70 |
else new JFile(dir + JFile.separator + File.platform_path(path)).getCanonicalPath |
64716 | 71 |
} |
72 |
||
64834 | 73 |
def get_model(file: JFile): Option[Document_Model] = state.value.models.get(file) |
74 |
def get_model(name: Document.Node.Name): Option[Document_Model] = get_model(node_file(name)) |
|
75 |
||
76 |
||
77 |
/* file content */ |
|
78 |
||
79 |
def read_file_content(file: JFile): Option[String] = |
|
80 |
try { Some(Line.normalize(File.read(file))) } |
|
81 |
catch { case ERROR(_) => None } |
|
82 |
||
83 |
def get_file_content(file: JFile): Option[String] = |
|
84 |
get_model(file) match { |
|
85 |
case Some(model) => Some(model.content.text) |
|
86 |
case None => read_file_content(file) |
|
87 |
} |
|
88 |
||
89 |
def bibtex_entries_iterator(): Iterator[Text.Info[(String, Document_Model)]] = |
|
90 |
for { |
|
91 |
(_, model) <- state.value.models.iterator |
|
92 |
Text.Info(range, entry) <- model.content.bibtex_entries.iterator |
|
93 |
} yield Text.Info(range, (entry, model)) |
|
94 |
||
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
95 |
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
|
96 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
97 |
val file = node_file(name) |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
98 |
get_model(file) match { |
64830 | 99 |
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
|
100 |
case None if file.isFile => |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
101 |
val reader = Scan.byte_reader(file) |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
102 |
try { f(reader) } finally { reader.close } |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
103 |
case None => |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
104 |
error("No such file: " + quote(file.toString)) |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
105 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
106 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
107 |
|
64703 | 108 |
|
109 |
/* document models */ |
|
110 |
||
64800 | 111 |
def visible_node(name: Document.Node.Name): Boolean = |
112 |
get_model(name) match { |
|
113 |
case Some(model) => model.node_visible |
|
114 |
case None => false |
|
115 |
} |
|
116 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
117 |
def update_model(session: Session, file: JFile, text: String) |
64703 | 118 |
{ |
119 |
state.change(st => |
|
64709 | 120 |
{ |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
121 |
val model = st.models.getOrElse(file, Document_Model.init(session, node_name(file))) |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
122 |
val model1 = (model.update_text(text) getOrElse model).external(false) |
65114
200b787ceb51
potentially redundant pending_output, for the sake of uniformity and reactivity;
wenzelm
parents:
65113
diff
changeset
|
123 |
st.update_models(Some(file -> model1)) |
64709 | 124 |
}) |
64703 | 125 |
} |
126 |
||
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
127 |
def close_model(file: JFile): Boolean = |
64710 | 128 |
state.change_result(st => |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
129 |
st.models.get(file) match { |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
130 |
case None => (false, st) |
65113 | 131 |
case Some(model) => (true, st.update_models(Some(file -> model.external(true)))) |
64710 | 132 |
}) |
133 |
||
65116 | 134 |
def sync_models(changed_files: Set[JFile]): Unit = |
135 |
state.change(st => |
|
64710 | 136 |
{ |
64719 | 137 |
val changed_models = |
64710 | 138 |
(for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
139 |
(file, model) <- st.models.iterator |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
140 |
if changed_files(file) && model.external_file |
64812 | 141 |
text <- read_file_content(file) |
64779 | 142 |
model1 <- model.update_text(text) |
65116 | 143 |
} yield (file, model1)).toList |
144 |
st.update_models(changed_models) |
|
64710 | 145 |
}) |
146 |
||
64703 | 147 |
|
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
148 |
/* resolve dependencies */ |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
149 |
|
64857 | 150 |
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
|
151 |
{ |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
152 |
state.change_result(st => |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
153 |
{ |
64800 | 154 |
/* theory files */ |
155 |
||
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
156 |
val thys = |
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
157 |
(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
|
158 |
yield (model.node_name, Position.none)).toList |
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
159 |
|
64800 | 160 |
val thy_files = thy_info.dependencies("", thys).deps.map(_.name) |
161 |
||
162 |
||
163 |
/* auxiliary files */ |
|
164 |
||
165 |
val stable_tip_version = |
|
64815 | 166 |
if (st.models.forall(entry => entry._2.is_stable)) |
64800 | 167 |
session.current_state().stable_tip_version |
168 |
else None |
|
169 |
||
170 |
val aux_files = |
|
171 |
stable_tip_version match { |
|
172 |
case Some(version) => undefined_blobs(version.nodes) |
|
173 |
case None => Nil |
|
174 |
} |
|
175 |
||
176 |
||
177 |
/* loaded models */ |
|
178 |
||
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
179 |
val loaded_models = |
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
180 |
(for { |
64800 | 181 |
node_name <- thy_files.iterator ++ aux_files.iterator |
182 |
file = node_file(node_name) |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
183 |
if !st.models.isDefinedAt(file) |
64857 | 184 |
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
|
185 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
186 |
yield { |
64800 | 187 |
val model = Document_Model.init(session, node_name) |
64727
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
188 |
val model1 = (model.update_text(text) getOrElse model).external(true) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
189 |
(file, model1) |
65111
3224a6f7bd6b
more robust treatment of pending input/output: these are often correlated;
wenzelm
parents:
65107
diff
changeset
|
190 |
}).toMap |
64731
84192ecae582
just one synchronized access to global state: works recursively on JVM;
wenzelm
parents:
64729
diff
changeset
|
191 |
|
64800 | 192 |
val invoke_input = loaded_models.nonEmpty |
193 |
val invoke_load = stable_tip_version.isEmpty |
|
194 |
||
65113 | 195 |
((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
|
196 |
}) |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
197 |
} |
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
198 |
|
13e37567a0d6
automatically resolve dependencies from document models and file-system;
wenzelm
parents:
64726
diff
changeset
|
199 |
|
64703 | 200 |
/* pending input */ |
201 |
||
202 |
def flush_input(session: Session) |
|
203 |
{ |
|
204 |
state.change(st => |
|
205 |
{ |
|
64719 | 206 |
val changed_models = |
64703 | 207 |
(for { |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
208 |
file <- st.pending_input.iterator |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
209 |
model <- st.models.get(file) |
64800 | 210 |
(edits, model1) <- model.flush_edits(st.document_blobs) |
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
211 |
} yield (edits, (file, model1))).toList |
64707 | 212 |
|
64800 | 213 |
session.update(st.document_blobs, changed_models.flatMap(_._1)) |
64703 | 214 |
st.copy( |
65112 | 215 |
models = st.models ++ changed_models.iterator.map(_._2), |
64703 | 216 |
pending_input = Set.empty) |
217 |
}) |
|
218 |
} |
|
219 |
||
220 |
||
221 |
/* pending output */ |
|
222 |
||
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
223 |
def update_output(changed_nodes: List[JFile]): Unit = |
64703 | 224 |
state.change(st => st.copy(pending_output = st.pending_output ++ changed_nodes)) |
225 |
||
226 |
def flush_output(channel: Channel) |
|
227 |
{ |
|
228 |
state.change(st => |
|
229 |
{ |
|
230 |
val changed_iterator = |
|
231 |
for { |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
232 |
file <- st.pending_output.iterator |
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
233 |
model <- st.models.get(file) |
64704 | 234 |
rendering = model.rendering() |
65115 | 235 |
(changed_diags, changed_decos, model1) = model.publish(rendering) |
236 |
if changed_diags.isDefined || changed_decos.isDefined |
|
65095 | 237 |
} |
238 |
yield { |
|
65115 | 239 |
for (diags <- changed_diags) |
240 |
channel.write(Protocol.PublishDiagnostics(file, rendering.diagnostics_output(diags))) |
|
241 |
for (decos <- changed_decos; deco <- decos) |
|
242 |
channel.write(rendering.decoration_output(deco).json(file)) |
|
64777
ca09695eb43c
clarified Document.Node.Name (again): canonical platform file;
wenzelm
parents:
64775
diff
changeset
|
243 |
(file, model1) |
64703 | 244 |
} |
245 |
st.copy( |
|
65112 | 246 |
models = st.models ++ changed_iterator, |
64703 | 247 |
pending_output = Set.empty) |
248 |
} |
|
249 |
) |
|
250 |
} |
|
64870 | 251 |
|
252 |
||
253 |
/* output text */ |
|
254 |
||
64877 | 255 |
def decode_text: Boolean = options.bool("vscode_unicode_symbols") |
65107 | 256 |
def tooltip_margin: Int = options.int("vscode_tooltip_margin") |
257 |
def message_margin: Int = options.int("vscode_message_margin") |
|
64877 | 258 |
|
64870 | 259 |
def output_text(s: String): String = |
64877 | 260 |
if (decode_text) Symbol.decode(s) else Symbol.encode(s) |
64870 | 261 |
|
65107 | 262 |
def output_xml(xml: XML.Tree): String = |
263 |
output_text(XML.content(xml)) |
|
264 |
||
64870 | 265 |
def output_pretty(body: XML.Body, margin: Int): String = |
266 |
output_text(Pretty.string_of(body, margin)) |
|
65107 | 267 |
def output_pretty_tooltip(body: XML.Body): String = output_pretty(body, tooltip_margin) |
268 |
def output_pretty_message(body: XML.Body): String = output_pretty(body, message_margin) |
|
64605 | 269 |
} |