Skip to content

Commit

Permalink
fix: offset cursor shift when deleting multiple files in bulk (#2030)
Browse files Browse the repository at this point in the history
  • Loading branch information
sxyazi authored Dec 11, 2024
1 parent ccf466d commit a64e8a8
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 29 deletions.
5 changes: 4 additions & 1 deletion yazi-core/src/tab/folder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ impl Folder {
FilesOp::IOErr(..) => self.files.update_ioerr(),

FilesOp::Creating(_, files) => self.files.update_creating(files),
FilesOp::Deleting(_, urns) => self.files.update_deleting(urns),
FilesOp::Deleting(_, urns) => {
let deleted = self.files.update_deleting(urns);
self.cursor -= deleted.into_iter().filter(|&i| i < self.cursor).count();
}
FilesOp::Updating(_, files) => _ = self.files.update_updating(files),
FilesOp::Upserting(_, files) => self.files.update_upserting(files),
}
Expand Down
60 changes: 32 additions & 28 deletions yazi-fs/src/files.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::{HashMap, HashSet}, mem, ops::Deref};
use std::{collections::{HashMap, HashSet}, mem, ops::{Deref, Not}};

use tokio::{fs::{self, DirEntry}, select, sync::mpsc::{self, UnboundedReceiver}};
use yazi_shared::{Id, url::{Url, Urn, UrnBuf}};
Expand Down Expand Up @@ -182,19 +182,9 @@ impl Files {
}

#[cfg(unix)]
pub fn update_deleting(&mut self, urns: HashSet<UrnBuf>) {
pub fn update_deleting(&mut self, urns: HashSet<UrnBuf>) -> Vec<usize> {
if urns.is_empty() {
return;
}

macro_rules! go {
($dist:expr, $src:expr, $inc:literal) => {
let len = $dist.len();
$dist.retain(|f| !$src.remove(f.urn()));
if $dist.len() != len {
self.revision += $inc;
}
};
return vec![];
}

let (mut hidden, mut items) = if let Some(filter) = &self.filter {
Expand All @@ -208,32 +198,46 @@ impl Files {
urns.into_iter().partition(|u| u.as_urn().is_hidden())
};

let mut deleted = Vec::with_capacity(items.len());
if !items.is_empty() {
go!(self.items, items, 1);
let mut i = 0;
self.items.retain(|f| {
let b = items.remove(f.urn());
if b {
deleted.push(i);
}
i += 1;
!b
});
}
if !hidden.is_empty() {
go!(self.hidden, hidden, 0);
self.hidden.retain(|f| !hidden.remove(f.urn()));
}

self.revision += deleted.is_empty().not() as u64;
deleted
}

#[cfg(windows)]
pub fn update_deleting(&mut self, mut urns: HashSet<UrnBuf>) {
macro_rules! go {
($dist:expr, $src:expr, $inc:literal) => {
let len = $dist.len();
$dist.retain(|f| !$src.remove(f.urn()));
if $dist.len() != len {
self.revision += $inc;
}
};
}

pub fn update_deleting(&mut self, mut urns: HashSet<UrnBuf>) -> Vec<usize> {
let mut deleted = Vec::with_capacity(urns.len());
if !urns.is_empty() {
go!(self.items, urns, 1);
let mut i = 0;
self.items.retain(|f| {
let b = urns.remove(f.urn());
if b {
deleted.push(i)
}
i += 1;
!b
});
}
if !urns.is_empty() {
go!(self.hidden, urns, 0);
self.hidden.retain(|f| !urns.remove(f.urn()));
}

self.revision += deleted.is_empty().not() as u64;
deleted
}

pub fn update_updating(
Expand Down

0 comments on commit a64e8a8

Please sign in to comment.