@@ -1,6 +1,6 @@
1use helix_lsp::{
1use helix_lsp::{
3− lsp::{self, DiagnosticSeverity, NumberOrString},
3+ lsp::{self, CodeAction, CodeActionOrCommand, DiagnosticSeverity, NumberOrString},
4 util::{diagnostic_to_lsp_diagnostic, lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
4 util::{diagnostic_to_lsp_diagnostic, lsp_pos_to_pos, lsp_range_to_range, range_to_lsp_range},
5 OffsetEncoding,
5 OffsetEncoding,
@@ -18,7 +18,7 @@ use crate::{
21−use std::{borrow::Cow, collections::BTreeMap, path::PathBuf, sync::Arc};
21+use std::{borrow::Cow, cmp::Ordering, collections::BTreeMap, path::PathBuf, sync::Arc};
23/// Gets the language server that is attached to a document, and
23/// Gets the language server that is attached to a document, and
24/// if it's not active displays a status message. Using this macro
24/// if it's not active displays a status message. Using this macro
@@ -211,7 +211,6 @@ fn sym_picker(
211 Ok(path) => path,
211 Ok(path) => path,
212 Err(_) => {
212 Err(_) => {
213 let err = format!("unable to convert URI to filepath: {}", uri);
213 let err = format!("unable to convert URI to filepath: {}", uri);
214− log::error!("{}", err);
215 cx.editor.set_error(err);
214 cx.editor.set_error(err);
@@ -421,6 +420,63 @@ impl ui::menu::Item for lsp::CodeActionOrCommand {
423+/// Determines the category of the `CodeAction` using the `CodeAction::kind` field.
424+/// Returns a number that represent these categories.
425+/// Categories with a lower number should be displayed first.
428+/// While the `kind` field is defined as open ended in the LSP spec (any value may be used)
429+/// in practice a closed set of common values (mostly suggested in the LSP spec) are used.
430+/// VSCode displays each of these categories seperatly (seperated by a heading in the codeactions picker)
431+/// to make them easier to navigate. Helix does not display these headings to the user.
432+/// However it does sort code actions by their categories to achieve the same order as the VScode picker,
433+/// just without the headings.
435+/// The order used here is modeled after the [vscode sourcecode](https://github.com/microsoft/vscode/blob/eaec601dd69aeb4abb63b9601a6f44308c8d8c6e/src/vs/editor/contrib/codeAction/browser/codeActionWidget.ts>)
436+fn action_category(action: &CodeActionOrCommand) -> u32 {
437+ if let CodeActionOrCommand::CodeAction(CodeAction {
438+ kind: Some(kind), ..
441+ let mut components = kind.as_str().split('.');
442+ match components.next() {
443+ Some("quickfix") => 0,
444+ Some("refactor") => match components.next() {
445+ Some("extract") => 1,
446+ Some("inline") => 2,
447+ Some("rewrite") => 3,
449+ Some("surround") => 5,
452+ Some("source") => 6,
460+fn action_prefered(action: &CodeActionOrCommand) -> bool {
463+ CodeActionOrCommand::CodeAction(CodeAction {
464+ is_preferred: Some(true),
470+fn action_fixes_diagnostics(action: &CodeActionOrCommand) -> bool {
473+ CodeActionOrCommand::CodeAction(CodeAction {
474+ diagnostics: Some(diagnostics),
476+ }) if !diagnostics.is_empty()
424pub fn code_action(cx: &mut Context) {
480pub fn code_action(cx: &mut Context) {
425 let (view, doc) = current!(cx.editor);
481 let (view, doc) = current!(cx.editor);
@@ -457,37 +513,53 @@ pub fn code_action(cx: &mut Context) {
457 None => return,
513 None => return,
516+ // remove disabled code actions
517+ actions.retain(|action| {
520+ CodeActionOrCommand::Command(_)
521+ | CodeActionOrCommand::CodeAction(CodeAction { disabled: None, .. })
460 if actions.is_empty() {
525 if actions.is_empty() {
461 editor.set_status("No code actions available");
526 editor.set_status("No code actions available");
465− // sort by CodeActionKind
530+ // Sort codeactions into a useful order. This behaviour is only partially described in the LSP spec.
466− // this ensures that the most relevant codeactions (quickfix) show up first
531+ // Many details are modeled after vscode because langauge servers are usually tested against it.
467− // while more situational commands (like refactors) show up later
532+ // VScode sorts the codeaction two times:
468− // this behaviour is modeled after the behaviour of vscode (https://github.com/microsoft/vscode/blob/eaec601dd69aeb4abb63b9601a6f44308c8d8c6e/src/vs/editor/contrib/codeAction/browser/codeActionWidget.ts)
533+ //
469−
534+ // First the codeactions that fix some diagnostics are moved to the front.
470− actions.sort_by_key(|action| match &action {
535+ // If both codeactions fix some diagnostics (or both fix none) the codeaction
471− lsp::CodeActionOrCommand::CodeAction(lsp::CodeAction {
536+ // that is marked with `is_preffered` is shown first. The codeactions are then shown in seperate
472− kind: Some(kind), ..
537+ // submenus that only contain a certain category (see `action_category`) of actions.
474− let mut components = kind.as_str().split('.');
539+ // Below this done in in a single sorting step
475−
540+ actions.sort_by(|action1, action2| {
476− match components.next() {
541+ // sort actions by category
477− Some("quickfix") => 0,
542+ let order = action_category(action1).cmp(&action_category(action2));
478− Some("refactor") => match components.next() {
543+ if order != Ordering::Equal {
479− Some("extract") => 1,
544+ return order;
480− Some("inline") => 2,
481− Some("rewrite") => 3,
483− Some("surround") => 5,
486− Some("source") => 6,
490− _ => 7,
546+ // within the categories sort by relevancy.
547+ // Modeled after the `codeActionsComparator` function in vscode:
548+ // https://github.com/microsoft/vscode/blob/eaec601dd69aeb4abb63b9601a6f44308c8d8c6e/src/vs/editor/contrib/codeAction/browser/codeAction.ts
550+ // if one code action fixes a diagnostic but the other one doesn't show it first
551+ let order = action_fixes_diagnostics(action1)
552+ .cmp(&action_fixes_diagnostics(action2))
554+ if order != Ordering::Equal {
558+ // if one of the codeactions is marked as prefered show it first
559+ // otherwise keep the original LSP sorting
560+ action_prefered(action1)
561+ .cmp(&action_prefered(action2))
493 let mut picker = ui::Menu::new(actions, (), move |editor, code_action, event| {
565 let mut picker = ui::Menu::new(actions, (), move |editor, code_action, event| {