Skip to content

Commit 01815be

Browse files
committed
take "error" result from binary search into account
1 parent 844a208 commit 01815be

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
3636
* print slightly nicer errors when failing to create a directory [[@linkmauve](https://github.com/linkmauve)] (https://github.com/gitui-org/gitui/pull/2728)
3737
* 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))
3838
* disable blame and history popup keybinds for untracked files [[@kpbaks](https://github.com/kpbaks)] ([#2489](https://github.com/gitui-org/gitui/pull/2489))
39+
* 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))
3940

4041
## [0.27.0] - 2024-01-14
4142

src/components/utils/statustree.rs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,13 @@ impl StatusTree {
202202
return None;
203203
}
204204

205-
if let Ok(i) = self.tree.items().binary_search_by(|e| {
205+
let res = self.tree.items().binary_search_by(|e| {
206206
e.info.full_path.as_str().cmp(last_selection)
207-
}) {
208-
return Some(i);
207+
});
208+
match res {
209+
Ok(i) => Some(i),
210+
Err(i) => Some(cmp::min(i, self.tree.len() - 1)),
209211
}
210-
211-
Some(cmp::min(last_index, self.tree.len() - 1))
212212
}
213213

214214
fn selection_updown(
@@ -520,7 +520,7 @@ mod tests {
520520
res.update(&string_vec_to_status(&["a", "b"])).unwrap();
521521
res.selection = Some(1);
522522

523-
res.update(&string_vec_to_status(&["d", "c", "a"])).unwrap();
523+
res.update(&string_vec_to_status(&["a", "c", "d"])).unwrap();
524524
assert_eq!(res.selection, Some(1));
525525
}
526526

@@ -545,6 +545,21 @@ mod tests {
545545
assert_eq!(res.selection, Some(0));
546546
}
547547

548+
#[test]
549+
fn test_next_when_dir_disappears() {
550+
let mut res = StatusTree::default();
551+
res.update(&string_vec_to_status(&["a/b", "c", "d"]))
552+
.unwrap();
553+
res.selection = Some(1);
554+
assert_eq!(
555+
res.selected_item().unwrap().info.full_path,
556+
"a/b"
557+
);
558+
559+
res.update(&string_vec_to_status(&["c", "d"])).unwrap();
560+
assert_eq!(res.selected_item().unwrap().info.full_path, "c");
561+
}
562+
548563
#[test]
549564
fn test_keep_collapsed_states() {
550565
let mut res = StatusTree::default();

0 commit comments

Comments
 (0)