Skip to content

Commit f9ea9c5

Browse files
committed
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.
1 parent e55fc28 commit f9ea9c5

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

crates/ra_ide/src/completion/complete_scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ mod tests {
3838
label: "quux(…)",
3939
source_range: [91; 91),
4040
delete: [91; 91),
41-
insert: "quux($0)",
41+
insert: "quux(${1:x})$0",
4242
kind: Function,
4343
lookup: "quux",
4444
detail: "fn quux(x: i32)",

crates/ra_ide/src/completion/presentation.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::completion::{
99
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
1010
};
1111

12-
use crate::display::{const_label, function_label, macro_label, type_label};
12+
use crate::display::{const_label, macro_label, type_label, FunctionSignature};
1313

1414
impl Completions {
1515
pub(crate) fn add_field(
@@ -198,7 +198,7 @@ impl Completions {
198198

199199
let name = name.unwrap_or_else(|| func.name(ctx.db).to_string());
200200
let ast_node = func.source(ctx.db).value;
201-
let detail = function_label(&ast_node);
201+
let function_signature = FunctionSignature::from(&ast_node);
202202

203203
let mut builder =
204204
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone())
@@ -209,19 +209,27 @@ impl Completions {
209209
})
210210
.set_documentation(func.docs(ctx.db))
211211
.set_deprecated(is_deprecated(func, ctx.db))
212-
.detail(detail);
212+
.detail(function_signature.to_string());
213213

214214
// Add `<>` for generic types
215215
if ctx.use_item_syntax.is_none()
216216
&& !ctx.is_call
217217
&& ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis")
218218
{
219219
tested_by!(inserts_parens_for_function_calls);
220-
let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 {
221-
(format!("{}()$0", name), format!("{}()", name))
222-
} else {
223-
(format!("{}($0)", name), format!("{}(…)", name))
224-
};
220+
221+
let (snippet, label) =
222+
if params.is_empty() || has_self_param && params.len() == 1 {
223+
(format!("{}()$0", name), format!("{}()", name))
224+
} else {
225+
let function_params_snippet =
226+
join(function_signature.parameter_names.iter().enumerate().map(
227+
|(index, param_name)| format!("${{{}:{}}}", index + 1, param_name),
228+
))
229+
.separator(", ")
230+
.to_string();
231+
(format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name))
232+
};
225233
builder = builder.lookup_by(name).label(label).insert_snippet(snippet);
226234
}
227235

@@ -486,7 +494,7 @@ mod tests {
486494
label: "with_args(…)",
487495
source_range: [80; 85),
488496
delete: [80; 85),
489-
insert: "with_args($0)",
497+
insert: "with_args(${1:x}, ${2:y})$0",
490498
kind: Function,
491499
lookup: "with_args",
492500
detail: "fn with_args(x: i32, y: String)",
@@ -630,7 +638,7 @@ mod tests {
630638
label: "foo(…)",
631639
source_range: [61; 63),
632640
delete: [61; 63),
633-
insert: "foo($0)",
641+
insert: "foo(${1:xs})$0",
634642
kind: Function,
635643
lookup: "foo",
636644
detail: "fn foo(xs: Ve)",
@@ -659,7 +667,7 @@ mod tests {
659667
label: "foo(…)",
660668
source_range: [64; 66),
661669
delete: [64; 66),
662-
insert: "foo($0)",
670+
insert: "foo(${1:xs})$0",
663671
kind: Function,
664672
lookup: "foo",
665673
detail: "fn foo(xs: Ve)",
@@ -687,7 +695,7 @@ mod tests {
687695
label: "foo(…)",
688696
source_range: [68; 70),
689697
delete: [68; 70),
690-
insert: "foo($0)",
698+
insert: "foo(${1:xs})$0",
691699
kind: Function,
692700
lookup: "foo",
693701
detail: "fn foo(xs: Ve)",
@@ -715,7 +723,7 @@ mod tests {
715723
label: "foo(…)",
716724
source_range: [61; 63),
717725
delete: [61; 63),
718-
insert: "foo($0)",
726+
insert: "foo(${1:xs})$0",
719727
kind: Function,
720728
lookup: "foo",
721729
detail: "fn foo(xs: Ve<i128>)",

0 commit comments

Comments
 (0)