mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 22:24:14 +00:00
Support function's completion snippet
Note that `detail` was replced with `function_signature` to avoid calling `from` on FunctionSignature twice. I didn't add new tests because the current ones seem enough.
This commit is contained in:
parent
437329d3f5
commit
fb34a5ba06
2 changed files with 22 additions and 14 deletions
|
@ -38,7 +38,7 @@ mod tests {
|
||||||
label: "quux(…)",
|
label: "quux(…)",
|
||||||
source_range: [91; 91),
|
source_range: [91; 91),
|
||||||
delete: [91; 91),
|
delete: [91; 91),
|
||||||
insert: "quux($0)",
|
insert: "quux(${1:x})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "quux",
|
lookup: "quux",
|
||||||
detail: "fn quux(x: i32)",
|
detail: "fn quux(x: i32)",
|
||||||
|
|
|
@ -9,7 +9,7 @@ use crate::completion::{
|
||||||
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::display::{const_label, function_label, macro_label, type_label};
|
use crate::display::{const_label, macro_label, type_label, FunctionSignature};
|
||||||
|
|
||||||
impl Completions {
|
impl Completions {
|
||||||
pub(crate) fn add_field(
|
pub(crate) fn add_field(
|
||||||
|
@ -198,7 +198,7 @@ impl Completions {
|
||||||
|
|
||||||
let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
|
let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
|
||||||
let ast_node = func.source(ctx.db).value;
|
let ast_node = func.source(ctx.db).value;
|
||||||
let detail = function_label(&ast_node);
|
let function_signature = FunctionSignature::from(&ast_node);
|
||||||
|
|
||||||
let mut builder =
|
let mut builder =
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
|
||||||
|
@ -209,7 +209,7 @@ impl Completions {
|
||||||
})
|
})
|
||||||
.set_documentation(func.docs(ctx.db))
|
.set_documentation(func.docs(ctx.db))
|
||||||
.set_deprecated(is_deprecated(func, ctx.db))
|
.set_deprecated(is_deprecated(func, ctx.db))
|
||||||
.detail(detail);
|
.detail(function_signature.to_string());
|
||||||
|
|
||||||
// Add `<>` for generic types
|
// Add `<>` for generic types
|
||||||
if ctx.use_item_syntax.is_none()
|
if ctx.use_item_syntax.is_none()
|
||||||
|
@ -217,10 +217,18 @@ impl Completions {
|
||||||
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
|
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
|
||||||
{
|
{
|
||||||
tested_by!(inserts_parens_for_function_calls);
|
tested_by!(inserts_parens_for_function_calls);
|
||||||
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
|
|
||||||
|
let (snippet, label) =
|
||||||
|
if params.is_empty() || has_self_param && params.len() == 1 {
|
||||||
(format!("{}()$0", name), format!("{}()", name))
|
(format!("{}()$0", name), format!("{}()", name))
|
||||||
} else {
|
} else {
|
||||||
(format!("{}($0)", name), format!("{}(…)", name))
|
let function_params_snippet =
|
||||||
|
join(function_signature.parameter_names.iter().enumerate().map(
|
||||||
|
|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name),
|
||||||
|
))
|
||||||
|
.separator(", ")
|
||||||
|
.to_string();
|
||||||
|
(format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name))
|
||||||
};
|
};
|
||||||
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
|
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
|
||||||
}
|
}
|
||||||
|
@ -486,7 +494,7 @@ mod tests {
|
||||||
label: "with_args(…)",
|
label: "with_args(…)",
|
||||||
source_range: [80; 85),
|
source_range: [80; 85),
|
||||||
delete: [80; 85),
|
delete: [80; 85),
|
||||||
insert: "with_args($0)",
|
insert: "with_args(${1:x}, ${2:y})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "with_args",
|
lookup: "with_args",
|
||||||
detail: "fn with_args(x: i32, y: String)",
|
detail: "fn with_args(x: i32, y: String)",
|
||||||
|
@ -630,7 +638,7 @@ mod tests {
|
||||||
label: "foo(…)",
|
label: "foo(…)",
|
||||||
source_range: [61; 63),
|
source_range: [61; 63),
|
||||||
delete: [61; 63),
|
delete: [61; 63),
|
||||||
insert: "foo($0)",
|
insert: "foo(${1:xs})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "foo",
|
lookup: "foo",
|
||||||
detail: "fn foo(xs: Ve)",
|
detail: "fn foo(xs: Ve)",
|
||||||
|
@ -659,7 +667,7 @@ mod tests {
|
||||||
label: "foo(…)",
|
label: "foo(…)",
|
||||||
source_range: [64; 66),
|
source_range: [64; 66),
|
||||||
delete: [64; 66),
|
delete: [64; 66),
|
||||||
insert: "foo($0)",
|
insert: "foo(${1:xs})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "foo",
|
lookup: "foo",
|
||||||
detail: "fn foo(xs: Ve)",
|
detail: "fn foo(xs: Ve)",
|
||||||
|
@ -687,7 +695,7 @@ mod tests {
|
||||||
label: "foo(…)",
|
label: "foo(…)",
|
||||||
source_range: [68; 70),
|
source_range: [68; 70),
|
||||||
delete: [68; 70),
|
delete: [68; 70),
|
||||||
insert: "foo($0)",
|
insert: "foo(${1:xs})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "foo",
|
lookup: "foo",
|
||||||
detail: "fn foo(xs: Ve)",
|
detail: "fn foo(xs: Ve)",
|
||||||
|
@ -715,7 +723,7 @@ mod tests {
|
||||||
label: "foo(…)",
|
label: "foo(…)",
|
||||||
source_range: [61; 63),
|
source_range: [61; 63),
|
||||||
delete: [61; 63),
|
delete: [61; 63),
|
||||||
insert: "foo($0)",
|
insert: "foo(${1:xs})$0",
|
||||||
kind: Function,
|
kind: Function,
|
||||||
lookup: "foo",
|
lookup: "foo",
|
||||||
detail: "fn foo(xs: Ve<i128>)",
|
detail: "fn foo(xs: Ve<i128>)",
|
||||||
|
|
Loading…
Reference in a new issue