Skip to content

Commit 14f190e

Browse files
committed
take "error" result from binary search into account
1 parent 6d62241 commit 14f190e

File tree

2 files changed

+36
-12
lines changed

2 files changed

+36
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
4848
* disable blame and history popup keybinds for untracked files [[@kpbaks](https://github.com/kpbaks)] ([#2489](https://github.com/gitui-org/gitui/pull/2489))
4949
* overwrites committer on amend of unsigned commits [[@cruessler](https://github.com/cruessler)] ([#2784](https://github.com/gitui-org/gitui/issues/2784))
5050
* 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))
51+
* 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))
5152

5253
## [0.27.0] - 2024-01-14
5354

src/components/utils/statustree.rs

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,8 @@ impl StatusTree {
5757
self.selection = last_selection.as_ref().map_or_else(
5858
|| self.tree.items().first().map(|_| 0),
5959
|last_selection| {
60-
self.find_last_selection(
61-
last_selection,
62-
last_selection_index,
63-
)
64-
.or_else(|| self.tree.items().first().map(|_| 0))
60+
self.find_last_selection(last_selection)
61+
.or_else(|| self.tree.items().first().map(|_| 0))
6562
},
6663
);
6764

@@ -196,19 +193,18 @@ impl StatusTree {
196193
fn find_last_selection(
197194
&self,
198195
last_selection: &str,
199-
last_index: usize,
200196
) -> Option<usize> {
201197
if self.is_empty() {
202198
return None;
203199
}
204200

205-
if let Ok(i) = self.tree.items().binary_search_by(|e| {
201+
let res = self.tree.items().binary_search_by(|e| {
206202
e.info.full_path.as_str().cmp(last_selection)
207-
}) {
208-
return Some(i);
203+
});
204+
match res {
205+
Ok(i) => Some(i),
206+
Err(i) => Some(cmp::min(i, self.tree.len() - 1)),
209207
}
210-
211-
Some(cmp::min(last_index, self.tree.len() - 1))
212208
}
213209

214210
fn selection_updown(
@@ -520,7 +516,7 @@ mod tests {
520516
res.update(&string_vec_to_status(&["a", "b"])).unwrap();
521517
res.selection = Some(1);
522518

523-
res.update(&string_vec_to_status(&["d", "c", "a"])).unwrap();
519+
res.update(&string_vec_to_status(&["a", "c", "d"])).unwrap();
524520
assert_eq!(res.selection, Some(1));
525521
}
526522

@@ -545,6 +541,33 @@ mod tests {
545541
assert_eq!(res.selection, Some(0));
546542
}
547543

544+
#[test]
545+
fn test_next_when_dir_disappears() {
546+
let mut tree = StatusTree::default();
547+
tree.update(&string_vec_to_status(&["a/b", "c", "d"]))
548+
.unwrap();
549+
tree.selection = Some(1);
550+
assert_eq!(
551+
tree.selected_item().unwrap().info.full_path,
552+
"a/b"
553+
);
554+
555+
tree.update(&string_vec_to_status(&["c", "d"])).unwrap();
556+
assert_eq!(tree.selected_item().unwrap().info.full_path, "c");
557+
}
558+
559+
#[test]
560+
fn test_next_when_last_dir_disappears() {
561+
let mut tree = StatusTree::default();
562+
tree.update(&string_vec_to_status(&["a", "b", "c"]))
563+
.unwrap();
564+
tree.selection = Some(2);
565+
assert_eq!(tree.selected_item().unwrap().info.full_path, "c");
566+
567+
tree.update(&string_vec_to_status(&["a", "b"])).unwrap();
568+
assert_eq!(tree.selected_item().unwrap().info.full_path, "b");
569+
}
570+
548571
#[test]
549572
fn test_keep_collapsed_states() {
550573
let mut res = StatusTree::default();

0 commit comments

Comments
 (0)