diff options
author | Avishay Matayev <[email protected]> | 2020-03-03 21:02:51 +0000 |
---|---|---|
committer | Avishay Matayev <[email protected]> | 2020-03-04 09:03:32 +0000 |
commit | fb34a5ba06a747a5705b9be36c75d26cece956e2 (patch) | |
tree | 67c52affa3a73a7774605d70d84d51300da2fe1a /crates/ra_ide/src/completion | |
parent | 437329d3f5b7bb5b703b93c75a97d349eb77d6c7 (diff) |
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.
Diffstat (limited to 'crates/ra_ide/src/completion')
-rw-r--r-- | crates/ra_ide/src/completion/complete_scope.rs | 2 | ||||
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 34 |
2 files changed, 22 insertions, 14 deletions
diff --git a/crates/ra_ide/src/completion/complete_scope.rs b/crates/ra_ide/src/completion/complete_scope.rs index aad016d4a..2b9a0e556 100644 --- a/crates/ra_ide/src/completion/complete_scope.rs +++ b/crates/ra_ide/src/completion/complete_scope.rs | |||
@@ -38,7 +38,7 @@ mod tests { | |||
38 | label: "quux(…)", | 38 | label: "quux(…)", |
39 | source_range: [91; 91), | 39 | source_range: [91; 91), |
40 | delete: [91; 91), | 40 | delete: [91; 91), |
41 | insert: "quux($0)", | 41 | insert: "quux(${1:x})$0", |
42 | kind: Function, | 42 | kind: Function, |
43 | lookup: "quux", | 43 | lookup: "quux", |
44 | detail: "fn quux(x: i32)", | 44 | detail: "fn quux(x: i32)", |
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index ae9344a44..39d2b1c19 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs | |||
@@ -9,7 +9,7 @@ use crate::completion::{ | |||
9 | CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, | 9 | CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, |
10 | }; | 10 | }; |
11 | 11 | ||
12 | use crate::display::{const_label, function_label, macro_label, type_label}; | 12 | use crate::display::{const_label, macro_label, type_label, FunctionSignature}; |
13 | 13 | ||
14 | impl Completions { | 14 | impl Completions { |
15 | pub(crate) fn add_field( | 15 | pub(crate) fn add_field( |
@@ -198,7 +198,7 @@ impl Completions { | |||
198 | 198 | ||
199 | let name = name.unwrap_or_else(|| func.name(ctx.db).to_string()); | 199 | let name = name.unwrap_or_else(|| func.name(ctx.db).to_string()); |
200 | let ast_node = func.source(ctx.db).value; | 200 | let ast_node = func.source(ctx.db).value; |
201 | let detail = function_label(&ast_node); | 201 | let function_signature = FunctionSignature::from(&ast_node); |
202 | 202 | ||
203 | let mut builder = | 203 | let mut builder = |
204 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) | 204 | CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.clone()) |
@@ -209,7 +209,7 @@ impl Completions { | |||
209 | }) | 209 | }) |
210 | .set_documentation(func.docs(ctx.db)) | 210 | .set_documentation(func.docs(ctx.db)) |
211 | .set_deprecated(is_deprecated(func, ctx.db)) | 211 | .set_deprecated(is_deprecated(func, ctx.db)) |
212 | .detail(detail); | 212 | .detail(function_signature.to_string()); |
213 | 213 | ||
214 | // Add `<>` for generic types | 214 | // Add `<>` for generic types |
215 | if ctx.use_item_syntax.is_none() | 215 | if ctx.use_item_syntax.is_none() |
@@ -217,11 +217,19 @@ impl Completions { | |||
217 | && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") | 217 | && ctx.db.feature_flags.get("completion.insertion.add-call-parenthesis") |
218 | { | 218 | { |
219 | tested_by!(inserts_parens_for_function_calls); | 219 | tested_by!(inserts_parens_for_function_calls); |
220 | let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 { | 220 | |
221 | (format!("{}()$0", name), format!("{}()", name)) | 221 | let (snippet, label) = |
222 | } else { | 222 | if params.is_empty() || has_self_param && params.len() == 1 { |
223 | (format!("{}($0)", name), format!("{}(…)", name)) | 223 | (format!("{}()$0", name), format!("{}()", name)) |
224 | }; | 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 | }; | ||
225 | builder = builder.lookup_by(name).label(label).insert_snippet(snippet); | 233 | builder = builder.lookup_by(name).label(label).insert_snippet(snippet); |
226 | } | 234 | } |
227 | 235 | ||
@@ -486,7 +494,7 @@ mod tests { | |||
486 | label: "with_args(…)", | 494 | label: "with_args(…)", |
487 | source_range: [80; 85), | 495 | source_range: [80; 85), |
488 | delete: [80; 85), | 496 | delete: [80; 85), |
489 | insert: "with_args($0)", | 497 | insert: "with_args(${1:x}, ${2:y})$0", |
490 | kind: Function, | 498 | kind: Function, |
491 | lookup: "with_args", | 499 | lookup: "with_args", |
492 | detail: "fn with_args(x: i32, y: String)", | 500 | detail: "fn with_args(x: i32, y: String)", |
@@ -630,7 +638,7 @@ mod tests { | |||
630 | label: "foo(…)", | 638 | label: "foo(…)", |
631 | source_range: [61; 63), | 639 | source_range: [61; 63), |
632 | delete: [61; 63), | 640 | delete: [61; 63), |
633 | insert: "foo($0)", | 641 | insert: "foo(${1:xs})$0", |
634 | kind: Function, | 642 | kind: Function, |
635 | lookup: "foo", | 643 | lookup: "foo", |
636 | detail: "fn foo(xs: Ve)", | 644 | detail: "fn foo(xs: Ve)", |
@@ -659,7 +667,7 @@ mod tests { | |||
659 | label: "foo(…)", | 667 | label: "foo(…)", |
660 | source_range: [64; 66), | 668 | source_range: [64; 66), |
661 | delete: [64; 66), | 669 | delete: [64; 66), |
662 | insert: "foo($0)", | 670 | insert: "foo(${1:xs})$0", |
663 | kind: Function, | 671 | kind: Function, |
664 | lookup: "foo", | 672 | lookup: "foo", |
665 | detail: "fn foo(xs: Ve)", | 673 | detail: "fn foo(xs: Ve)", |
@@ -687,7 +695,7 @@ mod tests { | |||
687 | label: "foo(…)", | 695 | label: "foo(…)", |
688 | source_range: [68; 70), | 696 | source_range: [68; 70), |
689 | delete: [68; 70), | 697 | delete: [68; 70), |
690 | insert: "foo($0)", | 698 | insert: "foo(${1:xs})$0", |
691 | kind: Function, | 699 | kind: Function, |
692 | lookup: "foo", | 700 | lookup: "foo", |
693 | detail: "fn foo(xs: Ve)", | 701 | detail: "fn foo(xs: Ve)", |
@@ -715,7 +723,7 @@ mod tests { | |||
715 | label: "foo(…)", | 723 | label: "foo(…)", |
716 | source_range: [61; 63), | 724 | source_range: [61; 63), |
717 | delete: [61; 63), | 725 | delete: [61; 63), |
718 | insert: "foo($0)", | 726 | insert: "foo(${1:xs})$0", |
719 | kind: Function, | 727 | kind: Function, |
720 | lookup: "foo", | 728 | lookup: "foo", |
721 | detail: "fn foo(xs: Ve<i128>)", | 729 | detail: "fn foo(xs: Ve<i128>)", |