@@ -23,6 +23,8 @@ pub struct Picker<T> {
23 matcher: Box<Matcher>,
23 matcher: Box<Matcher>,
24 /// (index, score)
24 /// (index, score)
25 matches: Vec<(usize, i64)>,
25 matches: Vec<(usize, i64)>,
26+ /// Filter over original options.
27+ filters: Vec<usize>, // could be optimized into bit but not worth it now
27 cursor: usize,
29 cursor: usize,
28 // pattern: String,
30 // pattern: String,
@@ -50,6 +52,7 @@ impl<T> Picker<T> {
51 matcher: Box::new(Matcher::default()),
53 matcher: Box::new(Matcher::default()),
52 matches: Vec::new(),
54 matches: Vec::new(),
53 cursor: 0,
56 cursor: 0,
55 format_fn: Box::new(format_fn),
58 format_fn: Box::new(format_fn),
@@ -68,6 +71,7 @@ impl<T> Picker<T> {
68 ref mut options,
71 ref mut options,
69 ref mut matcher,
72 ref mut matcher,
70 ref mut matches,
73 ref mut matches,
71 ref format_fn,
75 ref format_fn,
73 } = *self;
77 } = *self;
@@ -81,6 +85,10 @@ impl<T> Picker<T> {
82 .enumerate()
86 .enumerate()
83 .filter_map(|(index, option)| {
87 .filter_map(|(index, option)| {
88+ // filter options first before matching
89+ if !filters.is_empty() {
90+ filters.binary_search(&index).ok()?;
84 // TODO: maybe using format_fn isn't the best idea here
92 // TODO: maybe using format_fn isn't the best idea here
85 let text = (format_fn)(option);
93 let text = (format_fn)(option);
86 // TODO: using fuzzy_indices could give us the char idx for match highlighting
94 // TODO: using fuzzy_indices could give us the char idx for match highlighting
@@ -114,6 +122,14 @@ impl<T> Picker<T> {
114 .get(self.cursor)
122 .get(self.cursor)
115 .map(|(index, _score)| &self.options[*index])
123 .map(|(index, _score)| &self.options[*index])
126+ pub fn save_filter(&mut self) {
127+ self.filters.clear();
129+ .extend(self.matches.iter().map(|(index, _)| *index));
130+ self.filters.sort_unstable(); // used for binary search later
131+ self.prompt.clear();
119// process:
135// process:
@@ -205,6 +221,12 @@ impl<T: 'static> Component for Picker<T> {
206 return close_fn;
222 return close_fn;
225+ code: KeyCode::Char(' '),
226+ modifiers: KeyModifiers::CONTROL,
209 if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) {
231 if let EventResult::Consumed(_) = self.prompt.handle_event(event, cx) {
210 // TODO: recalculate only if pattern changed
232 // TODO: recalculate only if pattern changed