From 3577c44dee95f0b2b5367dd034f5188317be08eb Mon Sep 17 00:00:00 2001 From: Lukas Wirth Date: Fri, 13 May 2022 19:52:44 +0200 Subject: [PATCH] fix: Fix fill-arguments completions not working --- crates/ide-completion/src/config.rs | 9 +++++++-- crates/ide-completion/src/lib.rs | 2 +- crates/ide-completion/src/render.rs | 2 +- crates/ide-completion/src/render/function.rs | 9 +++++---- crates/ide-completion/src/tests.rs | 8 +++++--- crates/ide/src/lib.rs | 4 ++-- crates/rust-analyzer/src/config.rs | 19 ++++++++----------- .../src/integrated_benchmarks.rs | 8 +++----- 8 files changed, 32 insertions(+), 29 deletions(-) diff --git a/crates/ide-completion/src/config.rs b/crates/ide-completion/src/config.rs index 302836dd1e..80d6af2816 100644 --- a/crates/ide-completion/src/config.rs +++ b/crates/ide-completion/src/config.rs @@ -14,13 +14,18 @@ pub struct CompletionConfig { pub enable_imports_on_the_fly: bool, pub enable_self_on_the_fly: bool, pub enable_private_editable: bool, - pub add_call_parenthesis: bool, - pub add_call_argument_snippets: bool, + pub callable: Option, pub snippet_cap: Option, pub insert_use: InsertUseConfig, pub snippets: Vec, } +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum CallableSnippets { + FillArguments, + AddParentheses, +} + impl CompletionConfig { pub fn postfix_snippets(&self) -> impl Iterator { self.snippets diff --git a/crates/ide-completion/src/lib.rs b/crates/ide-completion/src/lib.rs index 7789c96707..991ab6a4b8 100644 --- a/crates/ide-completion/src/lib.rs +++ b/crates/ide-completion/src/lib.rs @@ -27,7 +27,7 @@ use text_edit::TextEdit; use crate::{completions::Completions, context::CompletionContext}; pub use crate::{ - config::CompletionConfig, + config::{CallableSnippets, CompletionConfig}, item::{ CompletionItem, CompletionItemKind, CompletionRelevance, CompletionRelevancePostfixMatch, }, diff --git a/crates/ide-completion/src/render.rs b/crates/ide-completion/src/render.rs index 88b6435c95..f509218385 100644 --- a/crates/ide-completion/src/render.rs +++ b/crates/ide-completion/src/render.rs @@ -287,7 +287,7 @@ fn render_resolution_simple_( let type_path_no_ty_args = matches!( ctx.completion.path_context(), Some(PathCompletionCtx { kind: PathKind::Type, has_type_args: false, .. }) - ) && ctx.completion.config.add_call_parenthesis; + ) && ctx.completion.config.callable.is_some(); if type_path_no_ty_args { if let Some(cap) = ctx.snippet_cap() { let has_non_default_type_params = match resolution { diff --git a/crates/ide-completion/src/render/function.rs b/crates/ide-completion/src/render/function.rs index 1ede314e87..c47696bc41 100644 --- a/crates/ide-completion/src/render/function.rs +++ b/crates/ide-completion/src/render/function.rs @@ -10,6 +10,7 @@ use crate::{ context::{CompletionContext, DotAccess, NameRefContext, PathCompletionCtx, PathKind}, item::{Builder, CompletionItem, CompletionItemKind, CompletionRelevance}, render::{compute_exact_name_match, compute_ref_match, compute_type_match, RenderContext}, + CallableSnippets, }; enum FuncKind { @@ -123,7 +124,7 @@ pub(super) fn add_call_parens<'b>( (format!("{}()$0", name), "()") } else { builder.trigger_call_info(); - let snippet = if ctx.config.add_call_argument_snippets { + let snippet = if let Some(CallableSnippets::FillArguments) = ctx.config.callable { let offset = if self_param.is_some() { 2 } else { 1 }; let function_params_snippet = params.iter().enumerate().format_with(", ", |(index, param), f| { @@ -191,7 +192,7 @@ fn ref_of_param(ctx: &CompletionContext, arg: &str, ty: &hir::Type) -> &'static } fn should_add_parens(ctx: &CompletionContext) -> bool { - if !ctx.config.add_call_parenthesis { + if ctx.config.callable.is_none() { return false; } @@ -288,7 +289,7 @@ fn params( mod tests { use crate::{ tests::{check_edit, check_edit_with_config, TEST_CONFIG}, - CompletionConfig, + CallableSnippets, CompletionConfig, }; #[test] @@ -404,7 +405,7 @@ fn main() { S::foo(${1:&self})$0 } fn suppress_arg_snippets() { cov_mark::check!(suppress_arg_snippets); check_edit_with_config( - CompletionConfig { add_call_argument_snippets: false, ..TEST_CONFIG }, + CompletionConfig { callable: Some(CallableSnippets::AddParentheses), ..TEST_CONFIG }, "with_args", r#" fn with_args(x: i32, y: String) {} diff --git a/crates/ide-completion/src/tests.rs b/crates/ide-completion/src/tests.rs index 0430aeea59..742368ce81 100644 --- a/crates/ide-completion/src/tests.rs +++ b/crates/ide-completion/src/tests.rs @@ -36,7 +36,10 @@ use stdx::{format_to, trim_indent}; use syntax::{AstNode, NodeOrToken, SyntaxElement}; use test_utils::assert_eq_text; -use crate::{resolve_completion_edits, CompletionConfig, CompletionItem, CompletionItemKind}; +use crate::{ + resolve_completion_edits, CallableSnippets, CompletionConfig, CompletionItem, + CompletionItemKind, +}; /// Lots of basic item definitions const BASE_ITEMS_FIXTURE: &str = r#" @@ -63,8 +66,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig { enable_imports_on_the_fly: true, enable_self_on_the_fly: true, enable_private_editable: true, - add_call_parenthesis: true, - add_call_argument_snippets: true, + callable: Some(CallableSnippets::FillArguments), snippet_cap: SnippetCap::new(true), insert_use: InsertUseConfig { granularity: ImportGranularity::Crate, diff --git a/crates/ide/src/lib.rs b/crates/ide/src/lib.rs index da50a4b8ee..e5160f99f3 100644 --- a/crates/ide/src/lib.rs +++ b/crates/ide/src/lib.rs @@ -102,8 +102,8 @@ pub use ide_assists::{ Assist, AssistConfig, AssistId, AssistKind, AssistResolveStrategy, SingleResolve, }; pub use ide_completion::{ - CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, Snippet, - SnippetScope, + CallableSnippets, CompletionConfig, CompletionItem, CompletionItemKind, CompletionRelevance, + Snippet, SnippetScope, }; pub use ide_db::{ base_db::{ diff --git a/crates/rust-analyzer/src/config.rs b/crates/rust-analyzer/src/config.rs index abc1541b53..c7158648d8 100644 --- a/crates/rust-analyzer/src/config.rs +++ b/crates/rust-analyzer/src/config.rs @@ -11,8 +11,9 @@ use std::{ffi::OsString, fmt, iter, path::PathBuf}; use flycheck::FlycheckConfig; use ide::{ - AssistConfig, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, HighlightRelatedConfig, - HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, Snippet, SnippetScope, + AssistConfig, CallableSnippets, CompletionConfig, DiagnosticsConfig, ExprFillDefaultMode, + HighlightRelatedConfig, HoverConfig, HoverDocFormat, InlayHintsConfig, JoinLinesConfig, + Snippet, SnippetScope, }; use ide_db::{ imports::insert_use::{ImportGranularity, InsertUseConfig, PrefixKind}, @@ -1029,14 +1030,10 @@ impl Config { && completion_item_edit_resolve(&self.caps), enable_self_on_the_fly: self.data.completion_autoself_enable, enable_private_editable: self.data.completion_privateEditable_enable, - add_call_parenthesis: matches!( - self.data.completion_callable_snippets, - Some(CallableCompletionDef::AddParentheses) - ), - add_call_argument_snippets: matches!( - self.data.completion_callable_snippets, - Some(CallableCompletionDef::FillArguments) - ), + callable: self.data.completion_callable_snippets.map(|it| match it { + CallableCompletionDef::FillArguments => CallableSnippets::FillArguments, + CallableCompletionDef::AddParentheses => CallableSnippets::AddParentheses, + }), insert_use: self.insert_use_config(), snippet_cap: SnippetCap::new(try_or_def!( self.caps @@ -1383,7 +1380,7 @@ enum ImportGranularityDef { Module, } -#[derive(Deserialize, Debug, Clone)] +#[derive(Deserialize, Debug, Copy, Clone)] #[serde(rename_all = "snake_case")] enum CallableCompletionDef { FillArguments, diff --git a/crates/rust-analyzer/src/integrated_benchmarks.rs b/crates/rust-analyzer/src/integrated_benchmarks.rs index 4e6acaa877..5f110274b2 100644 --- a/crates/rust-analyzer/src/integrated_benchmarks.rs +++ b/crates/rust-analyzer/src/integrated_benchmarks.rs @@ -12,7 +12,7 @@ use std::sync::Arc; -use ide::{Change, CompletionConfig, FilePosition, TextSize}; +use ide::{CallableSnippets, Change, CompletionConfig, FilePosition, TextSize}; use ide_db::{ imports::insert_use::{ImportGranularity, InsertUseConfig}, SnippetCap, @@ -135,8 +135,7 @@ fn integrated_completion_benchmark() { enable_imports_on_the_fly: true, enable_self_on_the_fly: true, enable_private_editable: true, - add_call_parenthesis: true, - add_call_argument_snippets: true, + callable: Some(CallableSnippets::FillArguments), snippet_cap: SnippetCap::new(true), insert_use: InsertUseConfig { granularity: ImportGranularity::Crate, @@ -173,8 +172,7 @@ fn integrated_completion_benchmark() { enable_imports_on_the_fly: true, enable_self_on_the_fly: true, enable_private_editable: true, - add_call_parenthesis: true, - add_call_argument_snippets: true, + callable: Some(CallableSnippets::FillArguments), snippet_cap: SnippetCap::new(true), insert_use: InsertUseConfig { granularity: ImportGranularity::Crate,