fix: Differentiate between vfs config load and file changed events

This commit is contained in:
Lukas Wirth 2024-01-09 10:30:16 +01:00
parent f5f7ddae23
commit e1c67485bf
8 changed files with 26 additions and 16 deletions

View file

@ -322,7 +322,7 @@ fn load_crate_graph(
break;
}
}
vfs::loader::Message::Loaded { files } => {
vfs::loader::Message::Loaded { files } | vfs::loader::Message::Changed { files } => {
for (path, contents) in files {
vfs.set_file_contents(path.into(), contents);
}

View file

@ -1398,7 +1398,7 @@ fn sysroot_to_crate_graph(
let public_deps = SysrootPublicDeps {
deps: sysroot
.public_deps()
.map(|(name, idx, prelude)| (name, sysroot_crates[&idx], prelude))
.filter_map(|(name, idx, prelude)| Some((name, *sysroot_crates.get(&idx)?, prelude)))
.collect::<Vec<_>>(),
};

View file

@ -218,7 +218,7 @@ impl GlobalState {
let _p = profile::span("GlobalState::process_changes");
let mut file_changes = FxHashMap::<_, (bool, ChangedFile)>::default();
let (change, modified_files, workspace_structure_change) = {
let (change, modified_rust_files, workspace_structure_change) = {
let mut change = Change::new();
let mut guard = self.vfs.write();
let changed_files = guard.0.take_changes();
@ -254,8 +254,8 @@ impl GlobalState {
*change = Create(new);
*just_created = true;
}
// shouldn't occur, but collapse into `Modify`
(Modify(prev), _, Create(new)) => *prev = new,
// shouldn't occur, but keep the Create
(prev @ Modify(_), _, new @ Create(_)) => *prev = new,
}
}
Entry::Vacant(v) => {
@ -276,7 +276,7 @@ impl GlobalState {
// A file was added or deleted
let mut has_structure_changes = false;
let mut bytes = vec![];
let mut modified_files = vec![];
let mut modified_rust_files = vec![];
for file in changed_files {
let vfs_path = &vfs.file_path(file.file_id);
if let Some(path) = vfs_path.as_path() {
@ -288,8 +288,8 @@ impl GlobalState {
has_structure_changes = true;
workspace_structure_change =
Some((path, self.crate_graph_file_dependencies.contains(vfs_path)));
} else {
modified_files.push(file.file_id);
} else if path.extension() == Some("rs".as_ref()) {
modified_rust_files.push(file.file_id);
}
}
@ -324,7 +324,7 @@ impl GlobalState {
let roots = self.source_root_config.partition(vfs);
change.set_roots(roots);
}
(change, modified_files, workspace_structure_change)
(change, modified_rust_files, workspace_structure_change)
};
self.analysis_host.apply_change(change);
@ -339,7 +339,7 @@ impl GlobalState {
force_crate_graph_reload,
);
}
self.proc_macro_changed = modified_files.into_iter().any(|file_id| {
self.proc_macro_changed = modified_rust_files.into_iter().any(|file_id| {
let crates = raw_database.relevant_crates(file_id);
let crate_graph = raw_database.crate_graph();

View file

@ -101,8 +101,10 @@ pub(crate) fn handle_did_change_text_document(
params.content_changes,
)
.into_bytes();
*data = new_contents.clone();
state.vfs.write().0.set_file_contents(path, Some(new_contents));
if *data != new_contents {
*data = new_contents.clone();
state.vfs.write().0.set_file_contents(path, Some(new_contents));
}
}
Ok(())
}

View file

@ -571,15 +571,18 @@ impl GlobalState {
}
fn handle_vfs_msg(&mut self, message: vfs::loader::Message) {
let is_changed = matches!(message, vfs::loader::Message::Changed { .. });
match message {
vfs::loader::Message::Loaded { files } => {
vfs::loader::Message::Changed { files } | vfs::loader::Message::Loaded { files } => {
let vfs = &mut self.vfs.write().0;
for (path, contents) in files {
let path = VfsPath::from(path);
// if the file is in mem docs, it's managed by the client via notifications
// so only set it if its not in there
if !self.mem_docs.contains(&path) {
vfs.set_file_contents(path, contents);
if is_changed || vfs.file_id(&path).is_none() {
vfs.set_file_contents(path, contents);
}
}
}
}

View file

@ -835,7 +835,7 @@ fn main() {
#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
fn resolve_proc_macro() {
use expect_test::expect;
if skip_slow_tests() || true {
if skip_slow_tests() {
return;
}

View file

@ -160,7 +160,7 @@ impl NotifyActor {
Some((path, contents))
})
.collect();
self.send(loader::Message::Loaded { files });
self.send(loader::Message::Changed { files });
}
}
}

View file

@ -51,6 +51,8 @@ pub enum Message {
Progress { n_total: usize, n_done: usize, config_version: u32 },
/// The handle loaded the following files' content.
Loaded { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> },
/// The handle loaded the following files' content.
Changed { files: Vec<(AbsPathBuf, Option<Vec<u8>>)> },
}
/// Type that will receive [`Messages`](Message) from a [`Handle`].
@ -199,6 +201,9 @@ impl fmt::Debug for Message {
Message::Loaded { files } => {
f.debug_struct("Loaded").field("n_files", &files.len()).finish()
}
Message::Changed { files } => {
f.debug_struct("Changed").field("n_files", &files.len()).finish()
}
Message::Progress { n_total, n_done, config_version } => f
.debug_struct("Progress")
.field("n_total", n_total)