Skip to content

Commit

Permalink
feat: support 'n'/'p' key to move to the next/prev hunk.
Browse files Browse the repository at this point in the history
  • Loading branch information
hamflx committed Jun 17, 2023
1 parent ff48840 commit 4cd425f
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
65 changes: 64 additions & 1 deletion src/components/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,48 @@ impl DiffComponent {
Ok(())
}

fn calc_hunk_move_target(
&self,
direction: isize,
) -> Option<usize> {
let diff = self.diff.as_ref()?;
if diff.hunks.is_empty() {
return None;
}
let max = diff.hunks.len() as isize - 1;
let target_index = self
.selected_hunk
.map(|i| {
std::cmp::max(
0,
std::cmp::min(max, i as isize + direction),
)
})
.unwrap_or(0) as usize;
Some(target_index)
}

fn diff_hunk_move_up_down(&mut self, direction: isize) {
let diff = match &self.diff {
Some(diff) => diff,
None => return,
};
let target_index = self.calc_hunk_move_target(direction);
// return if selected_hunk not change
if self.selected_hunk == target_index {
return;
}
if let Some(target_index) = target_index {
let lines = diff
.hunks
.iter()
.take(target_index)
.fold(0, |sum, hunk| sum + hunk.lines.len());
self.selection = Selection::Single(lines);
self.selected_hunk = Some(target_index);
}
}

const fn is_stage(&self) -> bool {
self.current.is_stage
}
Expand Down Expand Up @@ -710,7 +752,16 @@ impl Component for DiffComponent {
self.can_scroll(),
self.focused(),
));

out.push(CommandInfo::new(
strings::commands::diff_hunk_next(&self.key_config),
self.calc_hunk_move_target(1) != self.selected_hunk,
self.focused(),
));
out.push(CommandInfo::new(
strings::commands::diff_hunk_prev(&self.key_config),
self.calc_hunk_move_target(-1) != self.selected_hunk,
self.focused(),
));
out.push(
CommandInfo::new(
strings::commands::diff_home_end(&self.key_config),
Expand Down Expand Up @@ -815,6 +866,18 @@ impl Component for DiffComponent {
self.horizontal_scroll
.move_right(HorizontalScrollType::Left);
Ok(EventState::Consumed)
} else if key_match(
e,
self.key_config.keys.diff_hunk_next,
) {
self.diff_hunk_move_up_down(1);
Ok(EventState::Consumed)
} else if key_match(
e,
self.key_config.keys.diff_hunk_prev,
) {
self.diff_hunk_move_up_down(-1);
Ok(EventState::Consumed)
} else if key_match(
e,
self.key_config.keys.stage_unstage_item,
Expand Down
4 changes: 4 additions & 0 deletions src/keys/key_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ pub struct KeysList {
pub pull: GituiKeyEvent,
pub abort_merge: GituiKeyEvent,
pub undo_commit: GituiKeyEvent,
pub diff_hunk_next: GituiKeyEvent,
pub diff_hunk_prev: GituiKeyEvent,
pub stage_unstage_item: GituiKeyEvent,
pub tag_annotate: GituiKeyEvent,
pub view_submodules: GituiKeyEvent,
Expand Down Expand Up @@ -193,6 +195,8 @@ impl Default for KeysList {
open_file_tree: GituiKeyEvent::new(KeyCode::Char('F'), KeyModifiers::SHIFT),
file_find: GituiKeyEvent::new(KeyCode::Char('f'), KeyModifiers::empty()),
branch_find: GituiKeyEvent::new(KeyCode::Char('f'), KeyModifiers::empty()),
diff_hunk_next: GituiKeyEvent::new(KeyCode::Char('n'), KeyModifiers::empty()),
diff_hunk_prev: GituiKeyEvent::new(KeyCode::Char('p'), KeyModifiers::empty()),
stage_unstage_item: GituiKeyEvent::new(KeyCode::Enter, KeyModifiers::empty()),
tag_annotate: GituiKeyEvent::new(KeyCode::Char('a'), KeyModifiers::CONTROL),
view_submodules: GituiKeyEvent::new(KeyCode::Char('S'), KeyModifiers::SHIFT),
Expand Down
24 changes: 24 additions & 0 deletions src/strings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,30 @@ pub mod commands {
CMD_GROUP_LOG,
)
}
pub fn diff_hunk_next(
key_config: &SharedKeyConfig,
) -> CommandText {
CommandText::new(
format!(
"Next hunk [{}]",
key_config.get_hint(key_config.keys.diff_hunk_next),
),
"move cursor to next hunk",
CMD_GROUP_DIFF,
)
}
pub fn diff_hunk_prev(
key_config: &SharedKeyConfig,
) -> CommandText {
CommandText::new(
format!(
"Prev hunk [{}]",
key_config.get_hint(key_config.keys.diff_hunk_prev),
),
"move cursor to prev hunk",
CMD_GROUP_DIFF,
)
}
pub fn diff_home_end(
key_config: &SharedKeyConfig,
) -> CommandText {
Expand Down

0 comments on commit 4cd425f

Please sign in to comment.