diff --git a/book/src/keymap.md b/book/src/keymap.md index 65d01b86a31f1..b5a1ed777845b 100644 --- a/book/src/keymap.md +++ b/book/src/keymap.md @@ -167,3 +167,17 @@ This layer is a kludge of mappings I had under leader key in neovim. | b | Open buffer picker | | w | Enter window mode | | space | Keep primary selection TODO: it's here because space mode replaced it | + +# Picker + +Keys to use within picker. + +| Key | Description | +|-----|-------------| +| up, ctrl-p | Previous entry | +| down, ctrl-n | Next entry | +| ctrl-space | Filter options | +| enter | Open selected | +| ctrl-h | Open horizontally | +| ctrl-v | Open vertically | +| escape, ctrl-c | Close picker | diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 0674f4cc26d92..998e095ec9ca8 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -23,6 +23,8 @@ pub struct Picker { matcher: Box, /// (index, score) matches: Vec<(usize, i64)>, + /// Filter over original options. + filters: Vec, // could be optimized into bit but not worth it now cursor: usize, // pattern: String, @@ -50,6 +52,7 @@ impl Picker { options, matcher: Box::new(Matcher::default()), matches: Vec::new(), + filters: Vec::new(), cursor: 0, prompt, format_fn: Box::new(format_fn), @@ -68,6 +71,7 @@ impl Picker { ref mut options, ref mut matcher, ref mut matches, + ref filters, ref format_fn, .. } = *self; @@ -81,6 +85,10 @@ impl Picker { .iter() .enumerate() .filter_map(|(index, option)| { + // filter options first before matching + if !filters.is_empty() { + filters.binary_search(&index).ok()?; + } // TODO: maybe using format_fn isn't the best idea here let text = (format_fn)(option); // TODO: using fuzzy_indices could give us the char idx for match highlighting @@ -114,6 +122,14 @@ impl Picker { .get(self.cursor) .map(|(index, _score)| &self.options[*index]) } + + pub fn save_filter(&mut self) { + self.filters.clear(); + self.filters + .extend(self.matches.iter().map(|(index, _)| *index)); + self.filters.sort_unstable(); // used for binary search later + self.prompt.clear(); + } } // process: @@ -205,6 +221,12 @@ impl Component for Picker { } return close_fn; } + KeyEvent { + code: KeyCode::Char(' '), + modifiers: KeyModifiers::CONTROL, + } => { + self.save_filter(); + } _ => { if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) { // TODO: recalculate only if pattern changed diff --git a/helix-term/src/ui/prompt.rs b/helix-term/src/ui/prompt.rs index 8367b12c189b4..af8d6be8eba5b 100644 --- a/helix-term/src/ui/prompt.rs +++ b/helix-term/src/ui/prompt.rs @@ -85,6 +85,13 @@ impl Prompt { self.exit_selection(); } + pub fn clear(&mut self) { + self.line.clear(); + self.cursor = 0; + self.completion = (self.completion_fn)(&self.line); + self.exit_selection(); + } + pub fn change_completion_selection(&mut self, direction: CompletionDirection) { if self.completion.is_empty() { return;