Tweak change collapsing

This commit is contained in:
Laurențiu Nicola 2023-01-26 17:19:35 +02:00
parent dd673eea33
commit deff5f22f6

View file

@ -3,7 +3,7 @@
//! //!
//! Each tick provides an immutable snapshot of the state as `WorldSnapshot`. //! Each tick provides an immutable snapshot of the state as `WorldSnapshot`.
use std::{mem, sync::Arc, time::Instant}; use std::{sync::Arc, time::Instant};
use crossbeam_channel::{unbounded, Receiver, Sender}; use crossbeam_channel::{unbounded, Receiver, Sender};
use flycheck::FlycheckHandle; use flycheck::FlycheckHandle;
@ -179,10 +179,9 @@ impl GlobalState {
pub(crate) fn process_changes(&mut self) -> bool { pub(crate) fn process_changes(&mut self) -> bool {
let _p = profile::span("GlobalState::process_changes"); let _p = profile::span("GlobalState::process_changes");
// A file was added or deleted
let mut has_structure_changes = false;
let mut workspace_structure_change = None; let mut workspace_structure_change = None;
let mut file_changes = FxHashMap::default();
let (change, changed_files) = { let (change, changed_files) = {
let mut change = Change::new(); let mut change = Change::new();
let (vfs, line_endings_map) = &mut *self.vfs.write(); let (vfs, line_endings_map) = &mut *self.vfs.write();
@ -191,57 +190,56 @@ impl GlobalState {
return false; return false;
} }
// important: this needs to be a stable sort, the order between changes is relevant // We need to fix up the changed events a bit. If we have a create or modify for a file
// for the same file ids // id that is followed by a delete we actually skip observing the file text from the
changed_files.sort_by_key(|file| file.file_id); // earlier event, to avoid problems later on.
// We need to fix up the changed events a bit, if we have a create or modify for a file for changed_file in &changed_files {
// id that is followed by a delete we actually no longer observe the file text from the
// create or modify which may cause problems later on
let mut collapsed_create_delete = false;
changed_files.dedup_by(|a, b| {
use vfs::ChangeKind::*; use vfs::ChangeKind::*;
let has_collapsed_create_delete = mem::replace(&mut collapsed_create_delete, false); file_changes
.entry(changed_file.file_id)
if a.file_id != b.file_id { .and_modify(|(change, just_created)| {
return false; // None -> Delete => keep
// Create -> Delete => collapse
//
match (change, just_created, changed_file.change_kind) {
// latter `Delete` wins
(change, _, Delete) => *change = Delete,
// merge `Create` with `Create` or `Modify`
(Create, _, Create | Modify) => {}
// collapse identical `Modify`es
(Modify, _, Modify) => {}
// equivalent to `Modify`
(change @ Delete, just_created, Create) => {
*change = Modify;
*just_created = true;
}
// shouldn't occur, but collapse into `Create`
(change @ Delete, just_created, Modify) => {
*change = Create;
*just_created = true;
}
// shouldn't occur, but collapse into `Modify`
(Modify, _, Create) => {}
}
})
.or_insert((
changed_file.change_kind,
matches!(changed_file.change_kind, Create),
));
} }
// true => delete the second element (a), we swap them here as they are inverted by dedup_by changed_files.extend(
match (b.change_kind, a.change_kind) { file_changes
// duplicate can be merged .into_iter()
(Create, Create) | (Modify, Modify) | (Delete, Delete) => true, .filter(|(_, (change_kind, just_created))| {
// just leave the create, modify is irrelevant !matches!((change_kind, just_created), (vfs::ChangeKind::Delete, true))
(Create, Modify) => true, })
// modify becomes irrelevant if the file is deleted .map(|(file_id, (change_kind, _))| vfs::ChangedFile { file_id, change_kind }),
(Modify, Delete) => { );
mem::swap(a, b);
true // A file was added or deleted
} let mut has_structure_changes = false;
// Remove the create message, and in the following loop, also remove the delete
(Create, Delete) => {
collapsed_create_delete = true;
b.change_kind = Delete;
true
}
// trailing delete from earlier
(Delete, Create | Modify) if has_collapsed_create_delete => {
b.change_kind = Create;
true
}
// this is equivalent to a modify
(Delete, Create) => {
b.change_kind = Modify;
true
}
// can't really occur
(Modify, Create) => false,
(Delete, Modify) => false,
}
});
if collapsed_create_delete {
changed_files.pop();
}
for file in &changed_files { for file in &changed_files {
if let Some(path) = vfs.file_path(file.file_id).as_path() { if let Some(path) = vfs.file_path(file.file_id).as_path() {
let path = path.to_path_buf(); let path = path.to_path_buf();