Skip to content

Commit

Permalink
Sort code action completions by importance
Browse files Browse the repository at this point in the history
This is inspired by a recent change in [Helix] that fixes sorting of
code actions.
We have the same problem; we use ":prompt -shell-script-candidates"
to show code actions.
For example, on this Rust file:

	fn main() {
	    let f: FnOnce(HashMap<i32, i32>);
	}

with the cursor on "HashMap", a ":lsp-code-actions" will offer two
code actions (from rust-analyzer):

	Extract type as type alias"
	Import `std::collections::HashMap`

The first one is a refactoring and the second one is a quickfix.

A proposed Kakoune patch allows users to define completion priorities.
Use it to fix the order of such code actions.

[Helix]: helix-editor/helix#4134
  • Loading branch information
krobelus committed Dec 22, 2022
1 parent 20a4101 commit 935dc03
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
19 changes: 16 additions & 3 deletions rc/lsp.kak
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,16 @@ define-command -hidden lsp-menu -params 1.. -docstring "Like menu but with promp
cases=
completion=
nl=$(printf '\n.'); nl=${nl%.}
priority=1
while [ $# -gt 0 ]; do
title=$1; shift
command=$1; shift
completion="${completion}${title}${nl}"
if $kak_opt_lsp_have_kakoune_feature_completion_priority; then
completion="${completion}$(printf %s "${title}" | sed 's/\\|/\\|/g')|$priority${nl}"
priority=$((priority + 1))
else
completion="${completion}${title}${nl}"
fi
cases="${cases}
$(shellquote "$title" s/¶/¶¶/g))
printf '%s\\n' $(shellquote "$command" s/¶/¶¶/g)
Expand All @@ -197,9 +203,9 @@ define-command -hidden lsp-menu -params 1.. -docstring "Like menu but with promp
esac
§" "$cases"
printf ' -menu -shell-script-candidates %%§
printf ' -menu %s -shell-script-candidates %%§
printf %%s %s
§\n' "$(shellquote "$completion")"
§\n' "$($kak_opt_lsp_have_kakoune_feature_completion_priority && printf %s -priority)" "$(shellquote "$completion")"
}
} catch %{
evaluate-commands %sh{
Expand Down Expand Up @@ -312,6 +318,13 @@ define-command -hidden lsp-completion -docstring "Request completions for the ma
lsp-did-change-and-then lsp-completion-request
}

declare-option -hidden bool lsp_have_kakoune_feature_completion_priority
declare-option -hidden completions lsp_have_kakoune_feature_completion_priority_tmp
try %{
set-option global lsp_have_kakoune_feature_completion_priority_tmp 1.1@0 insert_text|on_select|menu|1
set-option global lsp_have_kakoune_feature_completion_priority true
}

define-command -hidden lsp-completion-request -docstring "Request completions for the main cursor position" %{
try %{
# Fail if preceding character is a whitespace (by default; the trigger could be customized).
Expand Down
20 changes: 19 additions & 1 deletion src/language_features/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn editor_code_actions(
}
}

let actions = result
let mut actions = result
.into_iter()
.map(|c| match c {
CodeActionOrCommand::Command(_) => c,
Expand Down Expand Up @@ -145,6 +145,24 @@ fn editor_code_actions(
return;
}

actions.sort_by_key(|ca| {
let empty = CodeActionKind::EMPTY;
let kind = match ca {
CodeActionOrCommand::Command(_) => &empty,
CodeActionOrCommand::CodeAction(action) => action.kind.as_ref().unwrap_or(&empty),
};
// TODO These loosely follow what VSCode does, we should be more accurate.
match kind.as_str() {
"quickfix" => 0,
"refactor" => 1,
"refactor.extract" => 2,
"refactor.inline" => 3,
"refactor.rewrite" => 4,
"source" => 5,
"source.organizeImports" => 6,
_ => 7,
}
});
let titles_and_commands = actions
.iter()
.map(|c| {
Expand Down

0 comments on commit 935dc03

Please sign in to comment.