diff options
Diffstat (limited to 'src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs')
| -rw-r--r-- | src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs b/src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs index 4693bdc047f..2fe517fa8cd 100644 --- a/src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs +++ b/src/tools/rust-analyzer/crates/ide-completion/src/render/function.rs @@ -1,12 +1,13 @@ //! Renderer for function calls. -use hir::{db::HirDatabase, AsAssocItem, HirDisplay}; +use hir::{AsAssocItem, HirDisplay, db::HirDatabase}; use ide_db::{SnippetCap, SymbolKind}; use itertools::Itertools; use stdx::{format_to, to_lower_snake_case}; -use syntax::{format_smolstr, AstNode, SmolStr, ToSmolStr}; +use syntax::{AstNode, SmolStr, ToSmolStr, format_smolstr}; use crate::{ + CallableSnippets, context::{ CompleteSemicolon, CompletionContext, DotAccess, DotAccessKind, PathCompletionCtx, PathKind, }, @@ -15,9 +16,8 @@ use crate::{ CompletionRelevanceReturnType, CompletionRelevanceTraitInfo, }, render::{ - compute_exact_name_match, compute_ref_match, compute_type_match, match_types, RenderContext, + RenderContext, compute_exact_name_match, compute_ref_match, compute_type_match, match_types, }, - CallableSnippets, }; #[derive(Debug)] @@ -293,11 +293,7 @@ fn ref_of_param(ctx: &CompletionContext<'_>, arg: &str, ty: &hir::Type) -> &'sta for (name, local) in ctx.locals.iter().sorted_by_key(|&(k, _)| k.clone()) { if name.as_str() == arg { return if local.ty(ctx.db) == derefed_ty { - if ty.is_mutable_reference() { - "&mut " - } else { - "&" - } + if ty.is_mutable_reference() { "&mut " } else { "&" } } else { "" }; @@ -324,7 +320,9 @@ fn detail(ctx: &CompletionContext<'_>, func: hir::Function) -> String { format_to!(detail, "unsafe "); } - format_to!(detail, "fn({})", params_display(ctx, func)); + detail.push_str("fn("); + params_display(ctx, &mut detail, func); + detail.push(')'); if !ret_ty.is_unit() { format_to!(detail, " -> {}", ret_ty.display(ctx.db, ctx.display_target)); } @@ -346,24 +344,28 @@ fn detail_full(ctx: &CompletionContext<'_>, func: hir::Function) -> String { detail } -fn params_display(ctx: &CompletionContext<'_>, func: hir::Function) -> String { +fn params_display(ctx: &CompletionContext<'_>, detail: &mut String, func: hir::Function) { if let Some(self_param) = func.self_param(ctx.db) { + format_to!(detail, "{}", self_param.display(ctx.db, ctx.display_target)); let assoc_fn_params = func.assoc_fn_params(ctx.db); let params = assoc_fn_params .iter() .skip(1) // skip the self param because we are manually handling that .map(|p| p.ty().display(ctx.db, ctx.display_target)); - format!( - "{}{}", - self_param.display(ctx.db, ctx.display_target), - params.format_with("", |display, f| { - f(&", ")?; - f(&display) - }) - ) + for param in params { + format_to!(detail, ", {}", param); + } } else { let assoc_fn_params = func.assoc_fn_params(ctx.db); - assoc_fn_params.iter().map(|p| p.ty().display(ctx.db, ctx.display_target)).join(", ") + format_to!( + detail, + "{}", + assoc_fn_params.iter().map(|p| p.ty().display(ctx.db, ctx.display_target)).format(", ") + ); + } + + if func.is_varargs(ctx.db) { + detail.push_str(", ..."); } } @@ -398,8 +400,8 @@ fn params( #[cfg(test)] mod tests { use crate::{ - tests::{check_edit, check_edit_with_config, TEST_CONFIG}, CallableSnippets, CompletionConfig, + tests::{TEST_CONFIG, check_edit, check_edit_with_config}, }; #[test] |
