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 use ":prompt"
to 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 preserves completion input order (if the
query is empty). Use this to fix the order of above code actions.

[Helix]: helix-editor/helix#4134
  • Loading branch information
krobelus committed Nov 18, 2023
1 parent 2d6bc84 commit a1aaeff
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/language_features/code_action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ fn editor_code_actions(
return;
}

let actions: Vec<_> = results
let mut actions: Vec<_> = results
.into_iter()
.flat_map(|(server_name, cmd)| {
let cmd: Vec<_> = cmd
Expand Down Expand Up @@ -222,6 +222,25 @@ fn editor_code_actions(
return;
}

actions.sort_by_key(|(_server, ca)| {
// TODO Group by server?
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(|(server_name, c)| {
Expand Down

0 comments on commit a1aaeff

Please sign in to comment.