on master help sign in
ui: completion: Use sort_text to sort the completions
f917b5aBlaž Hrastnik
helix-term/src/ui/completion.rs+4
@@ -14,6 +14,10 @@ use helix_lsp::{lsp, util};
14use lsp::CompletionItem;
14use lsp::CompletionItem;
15
15
16impl menu::Item for CompletionItem {
16impl menu::Item for CompletionItem {
17+ fn sort_text(&self) -> &str {
18+ self.filter_text.as_ref().unwrap_or(&self.label).as_str()
19+ }
20+
17 fn filter_text(&self) -> &str {
21 fn filter_text(&self) -> &str {
18 self.filter_text.as_ref().unwrap_or(&self.label).as_str()
22 self.filter_text.as_ref().unwrap_or(&self.label).as_str()
19 }
23 }
helix-term/src/ui/menu.rs+11−16
@@ -11,7 +11,7 @@ use helix_view::{graphics::Rect, Editor};
11use tui::layout::Constraint;
11use tui::layout::Constraint;
12
12
13pub trait Item {
13pub trait Item {
14− // TODO: sort_text
14+ fn sort_text(&self) -> &str;
15 fn filter_text(&self) -> &str;
15 fn filter_text(&self) -> &str;
16
16
17 fn label(&self) -> &str;
17 fn label(&self) -> &str;
@@ -64,24 +64,21 @@ impl<T: Item> Menu<T> {
64 let Self {
64 let Self {
65 ref mut matcher,
65 ref mut matcher,
66 ref mut matches,
66 ref mut matches,
67+ ref options,
67 ..
68 ..
68 } = *self;
69 } = *self;
69
70
70 // reuse the matches allocation
71 // reuse the matches allocation
71 matches.clear();
72 matches.clear();
72− matches.extend(
73− self.options
74− .iter()
75− .enumerate()
76− .filter_map(|(index, option)| {
73+ matches.extend(options.iter().enumerate().filter_map(|(index, option)| {
77− let text = option.filter_text();
74+ let text = option.filter_text();
78− // TODO: using fuzzy_indices could give us the char idx for match highlighting
75+ // TODO: using fuzzy_indices could give us the char idx for match highlighting
79− matcher
76+ matcher
80− .fuzzy_match(text, pattern)
77+ .fuzzy_match(text, pattern)
81− .map(|score| (index, score))
78+ .map(|score| (index, score))
82− }),
83− );
79+ }));
84− matches.sort_unstable_by_key(|(_, score)| -score);
80+ // matches.sort_unstable_by_key(|(_, score)| -score);
81+ matches.sort_unstable_by_key(|(index, _score)| options[*index].sort_text());
85
82
86 // reset cursor position
83 // reset cursor position
87 self.cursor = None;
84 self.cursor = None;
@@ -223,8 +220,6 @@ impl<T: Item + 'static> Component for Menu<T> {
223 EventResult::Ignored
220 EventResult::Ignored
224 }
221 }
225
222
226− // TODO: completion sorting
227−
228 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
223 fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
229 let n = self
224 let n = self
230 .options
225 .options