aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-03-04 12:01:16 +0000
committerAleksey Kladov <[email protected]>2020-03-04 12:01:16 +0000
commit7d2ddfd3c85b9c6505f09497ba94c98e8c78e822 (patch)
tree1b48a875bdbcf12507ff99d8d4bd1ff468ae8ff6 /crates
parent66ec6bdfb0fbb8e99b58f3c184ef5012354e6d92 (diff)
Skip self param when completing methods
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_ide/src/completion/presentation.rs58
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(