Skip to content

Commit f55ef8f

Browse files
committed
compiler: Add current document into snapshot
It went missing before as it gets added to the `TypeLoader` that gets snapshot only after the function we snapshot in...
1 parent a1857e6 commit f55ef8f

File tree

3 files changed

+38
-5
lines changed

3 files changed

+38
-5
lines changed

internal/compiler/passes.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ pub async fn run_passes(
8383
run_import_passes(doc, type_loader, diag);
8484
check_public_api::check_public_api(doc, diag);
8585

86-
let raw_type_loader = keep_raw.then(|| crate::typeloader::snapshot(type_loader).unwrap());
86+
let raw_type_loader =
87+
keep_raw.then(|| crate::typeloader::snapshot_with_extra_doc(type_loader, doc).unwrap());
8788

8889
collect_subcomponents::collect_subcomponents(root_component);
8990
for component in (root_component.used_types.borrow().sub_components.iter())

internal/compiler/typeloader.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,41 @@ pub fn snapshot(type_loader: &TypeLoader) -> Option<TypeLoader> {
8787
snapshotter.snapshot_type_loader(type_loader)
8888
}
8989

90+
/// This function makes a snapshot of the current state of the type loader.
91+
/// This snapshot includes everything: Elements, Components, known types, ...
92+
/// and can be used to roll back to earlier states in the compilation process.
93+
///
94+
/// One way this is used is to create a raw `TypeLoader` for analysis purposes
95+
/// or to load a set of changes, see if those compile and then role back
96+
///
97+
/// The result may be `None` if the `TypeLoader` is actually in the process
98+
/// of loading more documents and is `Some` `TypeLoader` with a copy off all
99+
/// state connected with the original `TypeLoader`.
100+
///
101+
/// The Document will be added to the type_loader after it was snapshotted as well.
102+
pub(crate) fn snapshot_with_extra_doc(
103+
type_loader: &TypeLoader,
104+
doc: &object_tree::Document,
105+
) -> Option<TypeLoader> {
106+
let mut snapshotter = Snapshotter {
107+
component_map: HashMap::new(),
108+
element_map: HashMap::new(),
109+
type_register_map: HashMap::new(),
110+
};
111+
let mut result = snapshotter.snapshot_type_loader(type_loader);
112+
113+
let new_doc = snapshotter.snapshot_document(doc);
114+
115+
if let Some(doc_node) = &new_doc.node {
116+
let path = doc_node.source_file.path().to_path_buf();
117+
if let Some(r) = &mut result {
118+
r.all_documents.docs.insert(path, new_doc);
119+
}
120+
}
121+
122+
result
123+
}
124+
90125
pub(crate) struct Snapshotter {
91126
component_map:
92127
HashMap<by_address::ByAddress<Rc<object_tree::Component>>, Rc<object_tree::Component>>,

tools/lsp/common/text_edit.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,7 @@ pub fn apply_workspace_edit(
271271
.drain()
272272
.filter_map(|(url, v)| {
273273
let edit_result = v.finalize()?;
274-
Some(EditedText {
275-
url,
276-
contents: edit_result.0,
277-
})
274+
Some(EditedText { url, contents: edit_result.0 })
278275
})
279276
.collect())
280277
}

0 commit comments

Comments
 (0)