From e3ee61f5e81573a2e9415879c8740ac5d59eff06 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Sun, 26 Apr 2020 18:54:05 -0400 Subject: [PATCH] Filter out CodeActions if a server only support commands. --- crates/rust-analyzer/src/config.rs | 6 ++++++ crates/rust-analyzer/src/main_loop/handlers.rs | 16 ++++++++++++++++ .../rust-analyzer/tests/heavy_tests/support.rs | 6 +++++- 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index 74a63e32a30..177da94cc9b 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -70,6 +70,7 @@ pub struct ClientCapsConfig { pub location_link: bool, pub line_folding_only: bool, pub hierarchical_symbols: bool, + pub code_action_literals: bool, } impl Default for Config { @@ -221,6 +222,11 @@ impl Config { { self.client_caps.hierarchical_symbols = value } + if let Some(value) = + caps.code_action.as_ref().and_then(|it| Some(it.code_action_literal_support.is_some())) + { + self.client_caps.code_action_literals = value; + } self.completion.allow_snippets(false); if let Some(completion) = &caps.completion { if let Some(completion_item) = &completion.completion_item { diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 8db2dfa0c86..b4e53990659 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -812,6 +812,22 @@ pub fn handle_code_action( } } + // If the client only supports commands then filter the list + // and remove and actions that depend on edits. + if !world.config.client_caps.code_action_literals { + res = res + .into_iter() + .filter_map(|it| match it { + cmd @ lsp_types::CodeActionOrCommand::Command(_) => Some(cmd), + lsp_types::CodeActionOrCommand::CodeAction(action) => match action.command { + Some(cmd) if action.edit.is_none() => { + Some(lsp_types::CodeActionOrCommand::Command(cmd)) + } + _ => None, + }, + }) + .collect(); + } Ok(Some(res)) } diff --git a/crates/rust-analyzer/tests/heavy_tests/support.rs b/crates/rust-analyzer/tests/heavy_tests/support.rs index e4fe3411aa6..8d47ee4f64c 100644 --- a/crates/rust-analyzer/tests/heavy_tests/support.rs +++ b/crates/rust-analyzer/tests/heavy_tests/support.rs @@ -77,7 +77,11 @@ impl<'a> Project<'a> { let roots = self.roots.into_iter().map(|root| tmp_dir.path().join(root)).collect(); let mut config = Config { - client_caps: ClientCapsConfig { location_link: true, ..Default::default() }, + client_caps: ClientCapsConfig { + location_link: true, + code_action_literals: true, + ..Default::default() + }, with_sysroot: self.with_sysroot, ..Config::default() };