Skip to content

Commit

Permalink
Fix 583 fix diff line selection (#585)
Browse files Browse the repository at this point in the history
Preserve line selection after staging/unstaging/discard (closes #583)
  • Loading branch information
Stephan Dilly authored Mar 12, 2021
1 parent e08f357 commit 7937b80
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- support for pushing tags ([#568](https://github.com/extrawurst/gitui/issues/568))
- visualize *conflicted* files differently ([#576](https://github.com/extrawurst/gitui/issues/576))

### Fixed
- keep diff line selection after staging/unstaging/discarding ([#583](https://github.com/extrawurst/gitui/issues/583))

## [0.12.0] - 2020-03-03

**pull support (ff-merge or conflict-free merge-commit)**
Expand Down
30 changes: 21 additions & 9 deletions src/components/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,20 +169,27 @@ impl DiffComponent {
let hash = hash(&diff);

if self.current.hash != hash {
let reset_selection = self.current.path != path;

self.current = Current {
path,
is_stage,
hash,
};

self.selected_hunk = Self::find_selected_hunk(
&diff,
self.selection.get_start(),
);

self.diff = Some(diff);
self.scroll_top.set(0);
self.selection = Selection::Single(0);

if reset_selection {
self.scroll_top.set(0);
self.selection = Selection::Single(0);
self.update_selection(0);
} else {
let old_selection = match self.selection {
Selection::Single(line) => line,
Selection::Multiple(start, _) => start,
};
self.update_selection(old_selection);
}
}

Ok(())
Expand Down Expand Up @@ -215,10 +222,15 @@ impl DiffComponent {
}
};

let new_start = cmp::min(max, new_start);
self.update_selection(new_start);
}
}

fn update_selection(&mut self, new_start: usize) {
if let Some(diff) = &self.diff {
let max = diff.lines.saturating_sub(1) as usize;
let new_start = cmp::min(max, new_start);
self.selection = Selection::Single(new_start);

self.selected_hunk =
Self::find_selected_hunk(diff, new_start);
}
Expand Down

0 comments on commit 7937b80

Please sign in to comment.