Skip to content

Commit

Permalink
fix selection disappearing in tree sometimes (closes #120)
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan Dilly committed Jun 12, 2020
1 parent 43905cf commit b80df36
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- reset file inside folder failed when running `gitui` in a subfolder too ([#118](https://github.com/extrawurst/gitui/issues/118))
- selection could disappear into collapsed folder ([#120](https://github.com/extrawurst/gitui/issues/120))

## [0.6.0] - 2020-06-09

Expand Down
41 changes: 41 additions & 0 deletions src/components/utils/statustree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,26 @@ impl StatusTree {

self.update_visibility(None, 0, true);

//NOTE: now that visibility is set we can make sure selection is visible
if let Some(idx) = self.selection {
self.selection = Some(self.find_visible_idx(idx));
}

Ok(())
}

fn find_visible_idx(&self, mut idx: usize) -> usize {
while idx > 0 {
if self.is_visible_index(idx) {
break;
}

idx -= 1;
}

idx
}

///
pub fn move_selection(&mut self, dir: MoveSelection) -> bool {
if let Some(selection) = self.selection {
Expand Down Expand Up @@ -394,6 +411,30 @@ mod tests {
assert_eq!(res.selection, Some(1));
}

#[test]
fn test_keep_selected_index_if_not_collapsed() {
let mut res = StatusTree::default();
res.update(&string_vec_to_status(&["a/b", "c"])).unwrap();

res.collapse("a/b", 0);

res.selection = Some(2);

res.update(&string_vec_to_status(&["a/b"])).unwrap();
assert_eq!(
get_visibles(&res),
vec![
true, //
false, //
]
);
assert_eq!(
res.is_visible_index(res.selection.unwrap()),
true
);
assert_eq!(res.selection, Some(0));
}

#[test]
fn test_keep_collapsed_states() {
let mut res = StatusTree::default();
Expand Down

0 comments on commit b80df36

Please sign in to comment.