From e3ee61f5e81573a2e9415879c8740ac5d59eff06 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Sun, 26 Apr 2020 18:54:05 -0400 Subject: 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 ++++++++++++++++ crates/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 74a63e32a..177da94cc 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 8db2dfa0c..b4e539906 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 e4fe3411a..8d47ee4f6 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() }; -- cgit v1.2.3 From e75565c73f935b7b4ee4182e632dfab210b26cff Mon Sep 17 00:00:00 2001 From: kjeremy Date: Sun, 26 Apr 2020 18:57:33 -0400 Subject: Advertise support for all the builtin CodeActionKinds Even thought we don't return all of these we eventually will so might as well advertise now. --- crates/rust-analyzer/src/caps.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index e22ab8402..38780726f 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs @@ -3,13 +3,13 @@ use crate::semantic_tokens; use lsp_types::{ - CallHierarchyServerCapability, CodeActionProviderCapability, CodeLensOptions, - CompletionOptions, DocumentOnTypeFormattingOptions, FoldingRangeProviderCapability, - ImplementationProviderCapability, RenameOptions, RenameProviderCapability, SaveOptions, - SelectionRangeProviderCapability, SemanticTokensDocumentProvider, SemanticTokensLegend, - SemanticTokensOptions, ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, - TextDocumentSyncKind, TextDocumentSyncOptions, TypeDefinitionProviderCapability, - WorkDoneProgressOptions, + CallHierarchyServerCapability, CodeActionOptions, CodeActionProviderCapability, + CodeLensOptions, CompletionOptions, DocumentOnTypeFormattingOptions, + FoldingRangeProviderCapability, ImplementationProviderCapability, RenameOptions, + RenameProviderCapability, SaveOptions, SelectionRangeProviderCapability, + SemanticTokensDocumentProvider, SemanticTokensLegend, SemanticTokensOptions, + ServerCapabilities, SignatureHelpOptions, TextDocumentSyncCapability, TextDocumentSyncKind, + TextDocumentSyncOptions, TypeDefinitionProviderCapability, WorkDoneProgressOptions, }; pub fn server_capabilities() -> ServerCapabilities { @@ -40,7 +40,19 @@ pub fn server_capabilities() -> ServerCapabilities { document_highlight_provider: Some(true), document_symbol_provider: Some(true), workspace_symbol_provider: Some(true), - code_action_provider: Some(CodeActionProviderCapability::Simple(true)), + code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions { + code_action_kinds: Some(vec![ + "".to_string(), + lsp_types::code_action_kind::QUICKFIX.to_string(), + lsp_types::code_action_kind::REFACTOR.to_string(), + lsp_types::code_action_kind::REFACTOR_EXTRACT.to_string(), + lsp_types::code_action_kind::REFACTOR_INLINE.to_string(), + lsp_types::code_action_kind::REFACTOR_REWRITE.to_string(), + lsp_types::code_action_kind::SOURCE.to_string(), + lsp_types::code_action_kind::SOURCE_ORGANIZE_IMPORTS.to_string(), + ]), + work_done_progress_options: Default::default(), + })), code_lens_provider: Some(CodeLensOptions { resolve_provider: Some(true) }), document_formatting_provider: Some(true), document_range_formatting_provider: None, -- cgit v1.2.3 From ee1628dba051e691a4ced756ab3cb360543609fa Mon Sep 17 00:00:00 2001 From: kjeremy Date: Sun, 26 Apr 2020 18:58:15 -0400 Subject: Mark most assists as the base "refactor" type Most of them area. We will separate them out later but this gets them to show up in the "refactor" menu of vscode. --- crates/rust-analyzer/src/main_loop/handlers.rs | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index b4e539906..647bdc426 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -19,8 +19,7 @@ use lsp_types::{ TextEdit, Url, WorkspaceEdit, }; use ra_ide::{ - Assist, AssistId, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, - SearchScope, + Assist, FileId, FilePosition, FileRange, Query, RangeInfo, Runnable, RunnableKind, SearchScope, }; use ra_prof::profile; use ra_syntax::{AstNode, SyntaxKind, TextRange, TextSize}; @@ -702,15 +701,9 @@ fn create_single_code_action(assist: Assist, world: &WorldSnapshot) -> Result Some("refactor.extract.variable".to_string()), - AssistId("add_custom_impl") => Some("refactor.rewrite.add_custom_impl".to_string()), - _ => None, - }; - Ok(CodeAction { title, - kind, + kind: Some("refactor".to_owned()), diagnostics: None, edit: None, command: Some(command), -- cgit v1.2.3 From 99826dab153feffc8fe0d80600d9e812824d67b2 Mon Sep 17 00:00:00 2001 From: kjeremy Date: Fri, 1 May 2020 14:12:31 -0400 Subject: Address comments --- crates/rust-analyzer/src/caps.rs | 3 ++- crates/rust-analyzer/src/main_loop/handlers.rs | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/crates/rust-analyzer/src/caps.rs b/crates/rust-analyzer/src/caps.rs index 38780726f..c0d320926 100644 --- a/crates/rust-analyzer/src/caps.rs +++ b/crates/rust-analyzer/src/caps.rs @@ -41,8 +41,9 @@ pub fn server_capabilities() -> ServerCapabilities { document_symbol_provider: Some(true), workspace_symbol_provider: Some(true), code_action_provider: Some(CodeActionProviderCapability::Options(CodeActionOptions { + // Advertise support for all built-in CodeActionKinds code_action_kinds: Some(vec![ - "".to_string(), + String::new(), lsp_types::code_action_kind::QUICKFIX.to_string(), lsp_types::code_action_kind::REFACTOR.to_string(), lsp_types::code_action_kind::REFACTOR_EXTRACT.to_string(), diff --git a/crates/rust-analyzer/src/main_loop/handlers.rs b/crates/rust-analyzer/src/main_loop/handlers.rs index 647bdc426..0f623949e 100644 --- a/crates/rust-analyzer/src/main_loop/handlers.rs +++ b/crates/rust-analyzer/src/main_loop/handlers.rs @@ -703,7 +703,7 @@ fn create_single_code_action(assist: Assist, world: &WorldSnapshot) -> Result