take "error" result from binary search into account (#2767)

Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
This commit is contained in:
Tillerino 2026-03-25 10:27:25 +01:00 committed by GitHub
parent e24fb45df1
commit 3ad42d23c4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 36 additions and 13 deletions

View file

@ -61,6 +61,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* disable blame and history popup keybinds for untracked files [[@kpbaks](https://github.com/kpbaks)] ([#2489](https://github.com/gitui-org/gitui/pull/2489))
* overwrites committer on amend of unsigned commits [[@cruessler](https://github.com/cruessler)] ([#2784](https://github.com/gitui-org/gitui/issues/2784))
* Updated project links to point to `gitui-org` instead of `extrawurst` [[@vasleymus](https://github.com/vasleymus)] ([#2538](https://github.com/gitui-org/gitui/pull/2538))
* when staging the last file in a directory, the first item after the directory is no longer skipped [[@Tillerino](https://github.com/Tillerino)] ([#2748](https://github.com/gitui-org/gitui/issues/2748))
## [0.27.0] - 2025-01-14

View file

@ -51,17 +51,13 @@ impl StatusTree {
let last_selection =
self.selected_item().map(|e| e.info.full_path);
let last_selection_index = self.selection.unwrap_or(0);
self.tree = FileTreeItems::new(list, &last_collapsed)?;
self.selection = last_selection.as_ref().map_or_else(
|| self.tree.items().first().map(|_| 0),
|last_selection| {
self.find_last_selection(
last_selection,
last_selection_index,
)
.or_else(|| self.tree.items().first().map(|_| 0))
self.find_last_selection(last_selection)
.or_else(|| self.tree.items().first().map(|_| 0))
},
);
@ -196,19 +192,18 @@ impl StatusTree {
fn find_last_selection(
&self,
last_selection: &str,
last_index: usize,
) -> Option<usize> {
if self.is_empty() {
return None;
}
if let Ok(i) = self.tree.items().binary_search_by(|e| {
let res = self.tree.items().binary_search_by(|e| {
e.info.full_path.as_str().cmp(last_selection)
}) {
return Some(i);
});
match res {
Ok(i) => Some(i),
Err(i) => Some(cmp::min(i, self.tree.len() - 1)),
}
Some(cmp::min(last_index, self.tree.len() - 1))
}
fn selection_updown(
@ -520,7 +515,7 @@ mod tests {
res.update(&string_vec_to_status(&["a", "b"])).unwrap();
res.selection = Some(1);
res.update(&string_vec_to_status(&["d", "c", "a"])).unwrap();
res.update(&string_vec_to_status(&["a", "c", "d"])).unwrap();
assert_eq!(res.selection, Some(1));
}
@ -545,6 +540,33 @@ mod tests {
assert_eq!(res.selection, Some(0));
}
#[test]
fn test_next_when_dir_disappears() {
let mut tree = StatusTree::default();
tree.update(&string_vec_to_status(&["a/b", "c", "d"]))
.unwrap();
tree.selection = Some(1);
assert_eq!(
tree.selected_item().unwrap().info.full_path,
"a/b"
);
tree.update(&string_vec_to_status(&["c", "d"])).unwrap();
assert_eq!(tree.selected_item().unwrap().info.full_path, "c");
}
#[test]
fn test_next_when_last_dir_disappears() {
let mut tree = StatusTree::default();
tree.update(&string_vec_to_status(&["a", "b", "c"]))
.unwrap();
tree.selection = Some(2);
assert_eq!(tree.selected_item().unwrap().info.full_path, "c");
tree.update(&string_vec_to_status(&["a", "b"])).unwrap();
assert_eq!(tree.selected_item().unwrap().info.full_path, "b");
}
#[test]
fn test_keep_collapsed_states() {
let mut res = StatusTree::default();