Skip to content

Commit

Permalink
Sort completions according to LSP's sortText
Browse files Browse the repository at this point in the history
When typing "someobject." there may be many valid completions.
Some servers such as rust-analyzer send the sortText property
which prescribes a completion ordering, usually to put
commonly-used methods first.

Let's use the proposed Kakoune patch to implement this ordering.
Note that as soon as the user has typed a few characters, the fuzzy
matching score will dominate any difference in ordering.
  • Loading branch information
krobelus committed Nov 18, 2023
1 parent a1aaeff commit ab68ef4
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/language_features/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,17 @@ pub fn text_document_completion(meta: EditorMeta, params: EditorParams, ctx: &mu
);
}

fn sort_text(item: &CompletionItem) -> &str {
item.sort_text.as_ref().unwrap_or(&item.label)
}

fn editor_completion(
meta: EditorMeta,
params: TextDocumentCompletionParams,
results: Vec<(String, Option<CompletionResponse>)>,
ctx: &mut Context,
) {
let items = results
let mut items: Vec<(String, CompletionItem)> = results
.into_iter()
.flat_map(|(server_name, items)| {
let items = match items {
Expand All @@ -70,6 +74,11 @@ fn editor_completion(
})
.collect();

// TODO Group by server?
items.sort_by(|(_left_server, left), (_right_server, right)| {
sort_text(left).cmp(sort_text(right))
});

let version = meta.version;
ctx.completion_items = items;
ctx.completion_items_timestamp = version;
Expand Down

0 comments on commit ab68ef4

Please sign in to comment.