diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-03-04 12:02:06 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2020-03-04 12:02:06 +0000 |
commit | 94189d0a1c8cf24b155fe7bad9b159bc17e997bb (patch) | |
tree | 0331d7b6399488ce8e4d9d78cb3a0a32b4cb371e /crates/ra_ide/src/completion/presentation.rs | |
parent | 02b02061b6cb121204a9bd931e7cd541dba2898f (diff) | |
parent | 7d2ddfd3c85b9c6505f09497ba94c98e8c78e822 (diff) |
Merge #3442
3442: Skip self param when completing methods r=matklad a=matklad
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide/src/completion/presentation.rs')
-rw-r--r-- | crates/ra_ide/src/completion/presentation.rs | 58 |
1 files changed, 45 insertions, 13 deletions
diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 39d2b1c19..dac232a85 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs | |||
@@ -218,18 +218,20 @@ impl Completions { | |||
218 | { | 218 | { |
219 | tested_by!(inserts_parens_for_function_calls); | 219 | tested_by!(inserts_parens_for_function_calls); |
220 | 220 | ||
221 | let (snippet, label) = | 221 | let (snippet, label) = if params.is_empty() || has_self_param && params.len() == 1 { |
222 | if params.is_empty() || has_self_param && params.len() == 1 { | 222 | (format!("{}()$0", name), format!("{}()", name)) |
223 | (format!("{}()$0", name), format!("{}()", name)) | 223 | } else { |
224 | } else { | 224 | let to_skip = if has_self_param { 1 } else { 0 }; |
225 | let function_params_snippet = | 225 | let function_params_snippet = |
226 | join(function_signature.parameter_names.iter().enumerate().map( | 226 | join( |
227 | function_signature.parameter_names.iter().skip(to_skip).enumerate().map( | ||
227 | |(index, param_name)| format!("${{{}:{}}}", index + 1, param_name), | 228 | |(index, param_name)| format!("${{{}:{}}}", index + 1, param_name), |
228 | )) | 229 | ), |
229 | .separator(", ") | 230 | ) |
230 | .to_string(); | 231 | .separator(", ") |
231 | (format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name)) | 232 | .to_string(); |
232 | }; | 233 | (format!("{}({})$0", name, function_params_snippet), format!("{}(…)", name)) |
234 | }; | ||
233 | builder = builder.lookup_by(name).label(label).insert_snippet(snippet); | 235 | builder = builder.lookup_by(name).label(label).insert_snippet(snippet); |
234 | } | 236 | } |
235 | 237 | ||
@@ -307,8 +309,8 @@ mod tests { | |||
307 | 309 | ||
308 | use crate::completion::{do_completion, CompletionItem, CompletionKind}; | 310 | use crate::completion::{do_completion, CompletionItem, CompletionKind}; |
309 | 311 | ||
310 | fn do_reference_completion(code: &str) -> Vec<CompletionItem> { | 312 | fn do_reference_completion(ra_fixture: &str) -> Vec<CompletionItem> { |
311 | do_completion(code, CompletionKind::Reference) | 313 | do_completion(ra_fixture, CompletionKind::Reference) |
312 | } | 314 | } |
313 | 315 | ||
314 | #[test] | 316 | #[test] |
@@ -531,6 +533,36 @@ mod tests { | |||
531 | } | 533 | } |
532 | 534 | ||
533 | #[test] | 535 | #[test] |
536 | fn parens_for_method_call() { | ||
537 | assert_debug_snapshot!( | ||
538 | do_reference_completion( | ||
539 | r" | ||
540 | struct S {} | ||
541 | impl S { | ||
542 | fn foo(&self, x: i32) {} | ||
543 | } | ||
544 | fn bar(s: &S) { | ||
545 | s.f<|> | ||
546 | } | ||
547 | " | ||
548 | ), | ||
549 | @r###" | ||
550 | [ | ||
551 | CompletionItem { | ||
552 | label: "foo(…)", | ||
553 | source_range: [171; 172), | ||
554 | delete: [171; 172), | ||
555 | insert: "foo(${1:x})$0", | ||
556 | kind: Method, | ||
557 | lookup: "foo", | ||
558 | detail: "fn foo(&self, x: i32)", | ||
559 | }, | ||
560 | ] | ||
561 | "### | ||
562 | ) | ||
563 | } | ||
564 | |||
565 | #[test] | ||
534 | fn dont_render_function_parens_in_use_item() { | 566 | fn dont_render_function_parens_in_use_item() { |
535 | assert_debug_snapshot!( | 567 | assert_debug_snapshot!( |
536 | do_reference_completion( | 568 | do_reference_completion( |