diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-04 11:05:14 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-04 11:05:14 +0000 |
commit | 66ec6bdfb0fbb8e99b58f3c184ef5012354e6d92 (patch) | |
tree | 267326e974e2d2a0ba7c92525721632fe956a4a8 | |
parent | 79d05aacd82dc38f491a87c2f61dc38e8da6e81c (diff) | |
parent | fb34a5ba06a747a5705b9be36c75d26cece956e2 (diff) |
Merge #3432
3432: Feature/snippet completions r=matklad a=iTZAvishay
This PR implements #1705 and is based on #3430 to avoid future conflicts.
The completions are placing default values with the names of the parameters, demo:
![call_me_demo](https://user-images.githubusercontent.com/5567310/75828341-461ca400-5db4-11ea-88d8-88e59ac1a197.gif)
Co-authored-by: Avishay Matayev <[email protected]>
-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>)", |