Auto merge of #12245 - Veykril:compl-snip, r=Veykril

fix: Fix fill-arguments completions not working

Fixes https://github.com/rust-lang/rust-analyzer/issues/12243
This commit is contained in:
bors 2022-05-13 17:53:23 +00:00
commit a123f8dd66
8 changed files with 32 additions and 29 deletions

View file

@ -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<CallableSnippets>,
pub snippet_cap: Option<SnippetCap>,
pub insert_use: InsertUseConfig,
pub snippets: Vec<Snippet>,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum CallableSnippets {
FillArguments,
AddParentheses,
}
impl CompletionConfig {
pub fn postfix_snippets(&self) -> impl Iterator<Item = (&str, &Snippet)> {
self.snippets

View file

@ -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,
},

View file

@ -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 {

View file

@ -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) {}

View file

@ -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,

View file

@ -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::{

View file

@ -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,

View file

@ -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,