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

Add PageUp, PageDown, Ctrl-u, Ctrl-d, Home, End keyboard shortcuts to file picker #1612

Merged
merged 6 commits into from
Feb 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 4 additions & 0 deletions book/src/keymap.md
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,11 @@ Keys to use within picker. Remapping currently not supported.
| Key | Description |
| ----- | ------------- |
| `Up`, `Ctrl-k`, `Ctrl-p` | Previous entry |
| `PageUp`, `Ctrl-u` | Page up |
| `Down`, `Ctrl-j`, `Ctrl-n` | Next entry |
| `PageDown`, `Ctrl-d` | Page down |
| `Home` | Go to first entry |
| `End` | Go to last entry |
| `Ctrl-space` | Filter options |
| `Enter` | Open selected |
| `Ctrl-s` | Open horizontally |
Expand Down
55 changes: 55 additions & 0 deletions helix-term/src/ui/picker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,9 @@ pub struct Picker<T> {
/// Filter over original options.
filters: Vec<usize>, // could be optimized into bit but not worth it now

/// Current height of the completions box
completion_height: u16,

cursor: usize,
// pattern: String,
prompt: Prompt,
Expand Down Expand Up @@ -310,6 +313,7 @@ impl<T> Picker<T> {
truncate_start: true,
format_fn: Box::new(format_fn),
callback_fn: Box::new(callback_fn),
completion_height: 0,
};

// TODO: scoring on empty input should just use a fastpath
Expand Down Expand Up @@ -364,6 +368,43 @@ impl<T> Picker<T> {
self.cursor = pos;
}

pub fn page_up(&mut self) {
if self.matches.is_empty() {
return;
}
let len = self.matches.len();
let pos = ((self.cursor + len.saturating_sub(self.completion_height as usize)) % len) % len;
Aloso marked this conversation as resolved.
Show resolved Hide resolved
self.cursor = pos;
}

pub fn page_down(&mut self) {
if self.matches.is_empty() {
return;
}
let len = self.matches.len();
let pos = (self.cursor + self.completion_height as usize) % len;
self.cursor = pos;
}

pub fn to_start(&mut self) {
if self.matches.is_empty() {
return;
}
self.cursor = 0;
}

pub fn to_end(&mut self) {
if self.matches.is_empty() {
return;
}
self.cursor = self.matches.len() - 1;
Aloso marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn set_height(&mut self, new_height: u16) {
// subtract borders and input line
self.completion_height = new_height - 4;
Aloso marked this conversation as resolved.
Show resolved Hide resolved
}

pub fn selection(&self) -> Option<&T> {
self.matches
.get(self.cursor)
Expand Down Expand Up @@ -422,6 +463,18 @@ impl<T: 'static> Component for Picker<T> {
key!(Tab) | key!(Down) | ctrl!('n') | ctrl!('j') => {
self.move_down();
}
key!(PageDown) | ctrl!('d') => {
self.page_down();
}
key!(PageUp) | ctrl!('u') => {
self.page_up();
}
key!(Home) => {
self.to_start();
}
key!(End) => {
self.to_end();
}
key!(Esc) | ctrl!('c') => {
return close_fn;
}
Expand Down Expand Up @@ -464,6 +517,8 @@ impl<T: 'static> Component for Picker<T> {
area
};

self.set_height(area.height);
Aloso marked this conversation as resolved.
Show resolved Hide resolved

let text_style = cx.editor.theme.get("ui.text");

// -- Render the frame:
Expand Down