mirror of https://github.com/helix-editor/helix
ui: completion: Use sort_text to sort the completions
parent
dde2be9395
commit
f917b5a441
|
@ -14,6 +14,10 @@ use helix_lsp::{lsp, util};
|
||||||
use lsp::CompletionItem;
|
use lsp::CompletionItem;
|
||||||
|
|
||||||
impl menu::Item for 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 {
|
fn filter_text(&self) -> &str {
|
||||||
self.filter_text.as_ref().unwrap_or(&self.label).as_str()
|
self.filter_text.as_ref().unwrap_or(&self.label).as_str()
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ use helix_view::{graphics::Rect, Editor};
|
||||||
use tui::layout::Constraint;
|
use tui::layout::Constraint;
|
||||||
|
|
||||||
pub trait Item {
|
pub trait Item {
|
||||||
// TODO: sort_text
|
fn sort_text(&self) -> &str;
|
||||||
fn filter_text(&self) -> &str;
|
fn filter_text(&self) -> &str;
|
||||||
|
|
||||||
fn label(&self) -> &str;
|
fn label(&self) -> &str;
|
||||||
|
@ -64,24 +64,21 @@ impl<T: Item> Menu<T> {
|
||||||
let Self {
|
let Self {
|
||||||
ref mut matcher,
|
ref mut matcher,
|
||||||
ref mut matches,
|
ref mut matches,
|
||||||
|
ref options,
|
||||||
..
|
..
|
||||||
} = *self;
|
} = *self;
|
||||||
|
|
||||||
// reuse the matches allocation
|
// reuse the matches allocation
|
||||||
matches.clear();
|
matches.clear();
|
||||||
matches.extend(
|
matches.extend(options.iter().enumerate().filter_map(|(index, option)| {
|
||||||
self.options
|
|
||||||
.iter()
|
|
||||||
.enumerate()
|
|
||||||
.filter_map(|(index, option)| {
|
|
||||||
let text = option.filter_text();
|
let text = option.filter_text();
|
||||||
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
// TODO: using fuzzy_indices could give us the char idx for match highlighting
|
||||||
matcher
|
matcher
|
||||||
.fuzzy_match(text, pattern)
|
.fuzzy_match(text, pattern)
|
||||||
.map(|score| (index, score))
|
.map(|score| (index, score))
|
||||||
}),
|
}));
|
||||||
);
|
// matches.sort_unstable_by_key(|(_, score)| -score);
|
||||||
matches.sort_unstable_by_key(|(_, score)| -score);
|
matches.sort_unstable_by_key(|(index, _score)| options[*index].sort_text());
|
||||||
|
|
||||||
// reset cursor position
|
// reset cursor position
|
||||||
self.cursor = None;
|
self.cursor = None;
|
||||||
|
@ -223,8 +220,6 @@ impl<T: Item + 'static> Component for Menu<T> {
|
||||||
EventResult::Ignored
|
EventResult::Ignored
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: completion sorting
|
|
||||||
|
|
||||||
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
fn required_size(&mut self, viewport: (u16, u16)) -> Option<(u16, u16)> {
|
||||||
let n = self
|
let n = self
|
||||||
.options
|
.options
|
||||||
|
|
Loading…
Reference in New Issue