Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stop picker before looping #12528

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,10 @@ See the documentation page on [pickers](./pickers.md) for more info.
| ----- | ------------- |
| `Shift-Tab`, `Up`, `Ctrl-p` | Previous entry |
| `Tab`, `Down`, `Ctrl-n` | Next entry |
| `PageUp`, `Ctrl-u` | Page up |
| `PageDown`, `Ctrl-d` | Page down |
| `PageUp`, `Ctrl-b` | Page up |
| `PageDown`, `Ctrl-f` | Page down |
| `Ctrl-u` | Half page up |
| `Ctrl-d` | Half page down |
| `Home` | Go to first entry |
| `End` | Go to last entry |
| `Enter` | Open selected |
Expand Down
38 changes: 32 additions & 6 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,26 +440,46 @@ impl<T: 'static + Send + Sync, D: 'static + Send + Sync> Picker<T, D> {
return;
}

let last = len - 1;
match direction {
// Leave the cursor at the edge before and after looping
Direction::Forward => {
self.cursor = self.cursor.saturating_add(amount) % len;
if self.cursor == last {
self.cursor = 0
} else {
self.cursor = self.cursor.saturating_add(amount).min(last);
}
}
Direction::Backward => {
self.cursor = self.cursor.saturating_add(len).saturating_sub(amount) % len;
if self.cursor == 0 {
self.cursor = last
} else {
self.cursor = self.cursor.saturating_sub(amount);
}
}
}
}

/// Move the cursor down by exactly one page. After the last page comes the first page.
/// Move the cursor up by exactly one page. After the first page comes the last page.
pub fn page_up(&mut self) {
self.move_by(self.completion_height as u32, Direction::Backward);
}

/// Move the cursor up by exactly one page. After the first page comes the last page.
/// Move the cursor down by exactly one page. After the last page comes the first page.
pub fn page_down(&mut self) {
self.move_by(self.completion_height as u32, Direction::Forward);
}

/// Move the cursor up by half a page.
pub fn half_page_up(&mut self) {
self.move_by(self.completion_height as u32 / 2, Direction::Backward);
}

/// Move the cursor down by half a page.
pub fn half_page_down(&mut self) {
self.move_by(self.completion_height as u32 / 2, Direction::Forward);
}

/// Move the cursor to the first entry
pub fn to_start(&mut self) {
self.cursor = 0;
Expand Down Expand Up @@ -999,12 +1019,18 @@ impl<I: 'static + Send + Sync, D: 'static + Send + Sync> Component for Picker<I,
key!(Tab) | key!(Down) | ctrl!('n') => {
self.move_by(1, Direction::Forward);
}
key!(PageDown) | ctrl!('d') => {
key!(PageDown) | ctrl!('f') => {
self.page_down();
}
key!(PageUp) | ctrl!('u') => {
key!(PageUp) | ctrl!('b') => {
self.page_up();
}
ctrl!('d') => {
self.half_page_down();
}
ctrl!('u') => {
self.half_page_up();
}
key!(Home) => {
self.to_start();
}
Expand Down
Loading