Skip to content

Commit 7937b80

Browse files
author
Stephan Dilly
authored
Fix 583 fix diff line selection (#585)
Preserve line selection after staging/unstaging/discard (closes #583)
1 parent e08f357 commit 7937b80

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- support for pushing tags ([#568](https://github.com/extrawurst/gitui/issues/568))
1818
- visualize *conflicted* files differently ([#576](https://github.com/extrawurst/gitui/issues/576))
1919

20+
### Fixed
21+
- keep diff line selection after staging/unstaging/discarding ([#583](https://github.com/extrawurst/gitui/issues/583))
22+
2023
## [0.12.0] - 2020-03-03
2124

2225
**pull support (ff-merge or conflict-free merge-commit)**

src/components/diff.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -169,20 +169,27 @@ impl DiffComponent {
169169
let hash = hash(&diff);
170170

171171
if self.current.hash != hash {
172+
let reset_selection = self.current.path != path;
173+
172174
self.current = Current {
173175
path,
174176
is_stage,
175177
hash,
176178
};
177179

178-
self.selected_hunk = Self::find_selected_hunk(
179-
&diff,
180-
self.selection.get_start(),
181-
);
182-
183180
self.diff = Some(diff);
184-
self.scroll_top.set(0);
185-
self.selection = Selection::Single(0);
181+
182+
if reset_selection {
183+
self.scroll_top.set(0);
184+
self.selection = Selection::Single(0);
185+
self.update_selection(0);
186+
} else {
187+
let old_selection = match self.selection {
188+
Selection::Single(line) => line,
189+
Selection::Multiple(start, _) => start,
190+
};
191+
self.update_selection(old_selection);
192+
}
186193
}
187194

188195
Ok(())
@@ -215,10 +222,15 @@ impl DiffComponent {
215222
}
216223
};
217224

218-
let new_start = cmp::min(max, new_start);
225+
self.update_selection(new_start);
226+
}
227+
}
219228

229+
fn update_selection(&mut self, new_start: usize) {
230+
if let Some(diff) = &self.diff {
231+
let max = diff.lines.saturating_sub(1) as usize;
232+
let new_start = cmp::min(max, new_start);
220233
self.selection = Selection::Single(new_start);
221-
222234
self.selected_hunk =
223235
Self::find_selected_hunk(diff, new_start);
224236
}

0 commit comments

Comments
 (0)