From 57658ade1b5c5f20eac2eca6d27ccc2465a2d06a Mon Sep 17 00:00:00 2001 From: LazyTanuki <43273245+lazytanuki@users.noreply.github.com> Date: Wed, 26 Oct 2022 22:39:15 +0200 Subject: [PATCH 1/2] feat(lsp): LSP preselected items appear first in completion menu --- helix-term/src/ui/completion.rs | 10 ++++++++++ helix-term/src/ui/menu.rs | 3 ++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index a21767f944f8..791a89fa2604 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -97,6 +97,16 @@ impl Completion { start_offset: usize, trigger_offset: usize, ) -> Self { + // Sort completion items according to their preselect status (given by the LSP server) + let mut items = items; + items.sort_by(|item1, item2| { + item2 + .preselect + .unwrap_or(false) + .cmp(&item1.preselect.unwrap_or(false)) + }); + + // Then create the menu let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| { fn item_to_transaction( doc: &Document, diff --git a/helix-term/src/ui/menu.rs b/helix-term/src/ui/menu.rs index 75769b905b2a..99c2473d6f40 100644 --- a/helix-term/src/ui/menu.rs +++ b/helix-term/src/ui/menu.rs @@ -108,7 +108,8 @@ impl Menu { .map(|score| (index, score)) }), ); - self.matches.sort_unstable_by_key(|(_, score)| -score); + // Order of equal elements needs to be preserved as LSP preselected items come in order of high to low priority + self.matches.sort_by_key(|(_, score)| -score); // reset cursor position self.cursor = None; From 5ac3ccbe7459b94016ee29af993250a58a4369e6 Mon Sep 17 00:00:00 2001 From: LazyTanuki <43273245+lazytanuki@users.noreply.github.com> Date: Thu, 27 Oct 2022 10:41:15 +0200 Subject: [PATCH 2/2] fix: shorter diff --- helix-term/src/ui/completion.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/completion.rs b/helix-term/src/ui/completion.rs index 791a89fa2604..de7c3232bb3d 100644 --- a/helix-term/src/ui/completion.rs +++ b/helix-term/src/ui/completion.rs @@ -92,19 +92,13 @@ impl Completion { pub fn new( editor: &Editor, - items: Vec, + mut items: Vec, offset_encoding: helix_lsp::OffsetEncoding, start_offset: usize, trigger_offset: usize, ) -> Self { // Sort completion items according to their preselect status (given by the LSP server) - let mut items = items; - items.sort_by(|item1, item2| { - item2 - .preselect - .unwrap_or(false) - .cmp(&item1.preselect.unwrap_or(false)) - }); + items.sort_by_key(|item| !item.preselect.unwrap_or(false)); // Then create the menu let menu = Menu::new(items, (), move |editor: &mut Editor, item, event| {