diff options
author | Blaž Hrastnik | 2021-08-10 02:19:07 +0000 |
---|---|---|
committer | Blaž Hrastnik | 2021-08-11 01:56:32 +0000 |
commit | f917b5a441ff3ae582358b6939ffbf889f4aa530 (patch) | |
tree | e5d3cd829dd22ab63573782d391d29ca2981153e /helix-term | |
parent | dde2be93956f82622c85171bd03dab1b6864318c (diff) |
ui: completion: Use sort_text to sort the completions
Diffstat (limited to 'helix-term')
-rw-r--r-- | helix-term/src/ui/completion.rs | 4 | ||||
-rw-r--r-- | helix-term/src/ui/menu.rs | 27 |
2 files changed, 15 insertions, 16 deletions
diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 2725d53d..f359e7ec 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -14,6 +14,10 @@ use helix_lsp::{lsp, util}; use lsp::CompletionItem; impl menu::Item for CompletionItem { + fn sort_text(&self) -> &str { + self.filter_text.as_ref().unwrap_or(&self.label).as_str() + } + fn filter_text(&self) -> &str { self.filter_text.as_ref().unwrap_or(&self.label).as_str() } diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 1e1c5427..26eff1d8 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -11,7 +11,7 @@ use helix_view::{graphics::Rect, Editor}; use tui::layout::Constraint; pub trait Item { - // TODO: sort_text + fn sort_text(&self) -> &str; fn filter_text(&self) -> &str; fn label(&self) -> &str; @@ -64,24 +64,21 @@ impl<T: Item> Menu<T> { let Self { ref mut matcher, ref mut matches, + ref options, .. } = *self; // reuse the matches allocation matches.clear(); - matches.extend( - self.options - .iter() - .enumerate() - .filter_map(|(index, option)| { - let text = option.filter_text(); - // TODO: using fuzzy_indices could give us the char idx for match highlighting - matcher - .fuzzy_match(text, pattern) - .map(|score| (index, score)) - }), - ); - matches.sort_unstable_by_key(|(_, score)| -score); + matches.extend(options.iter().enumerate().filter_map(|(index, option)| { + let text = option.filter_text(); + // TODO: using fuzzy_indices could give us the char idx for match highlighting + matcher + .fuzzy_match(text, pattern) + .map(|score| (index, score)) + })); + // matches.sort_unstable_by_key(|(_, score)| -score); + matches.sort_unstable_by_key(|(index, _score)| options[*index].sort_text()); // reset cursor position self.cursor = None; @@ -223,8 +220,6 @@ impl<T: Item + 'static> Component for Menu<T> { EventResult::Ignored } - // TODO: completion sorting - fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> { let n = self .options |