From 01815be16b70c18e0cdbc298460a7210fa05797e Mon Sep 17 00:00:00 2001 From: Tillmann Gaida Date: Wed, 29 Oct 2025 18:23:32 +0100 Subject: [PATCH] take "error" result from binary search into account --- CHANGELOG.md | 1 + src/components/utils/statustree.rs | 27 +++++++++++++++++++++------ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b8ded4447d..b894e73ff9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 * print slightly nicer errors when failing to create a directory [[@linkmauve](https://github.com/linkmauve)] (https://github.com/gitui-org/gitui/pull/2728) * When the terminal is insufficient to display all the commands, the cmdbar_bg configuration color does not fully take effect. ([#2347](https://github.com/extrawurst/gitui/issues/2347)) * disable blame and history popup keybinds for untracked files [[@kpbaks](https://github.com/kpbaks)] ([#2489](https://github.com/gitui-org/gitui/pull/2489)) +* 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] - 2024-01-14 diff --git a/src/components/utils/statustree.rs b/src/components/utils/statustree.rs index 47a1e00529..93409100c5 100644 --- a/src/components/utils/statustree.rs +++ b/src/components/utils/statustree.rs @@ -202,13 +202,13 @@ impl StatusTree { 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 +520,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 +545,21 @@ mod tests { assert_eq!(res.selection, Some(0)); } + #[test] + fn test_next_when_dir_disappears() { + let mut res = StatusTree::default(); + res.update(&string_vec_to_status(&["a/b", "c", "d"])) + .unwrap(); + res.selection = Some(1); + assert_eq!( + res.selected_item().unwrap().info.full_path, + "a/b" + ); + + res.update(&string_vec_to_status(&["c", "d"])).unwrap(); + assert_eq!(res.selected_item().unwrap().info.full_path, "c"); + } + #[test] fn test_keep_collapsed_states() { let mut res = StatusTree::default();