From 6c676909dd94f48ab1e85427071b508f104f360f Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Sun, 24 May 2020 18:09:42 +0900 Subject: Strip leading underscore --- crates/ra_ide/src/display/function_signature.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index 9572debd8..4f054398a 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -187,6 +187,14 @@ impl FunctionSignature { impl From<&'_ ast::FnDef> for FunctionSignature { fn from(node: &ast::FnDef) -> FunctionSignature { + fn strip_leading_underscore(name: String) -> String { + if name.starts_with("_") { + name.get(1..).unwrap_or_default().to_string() + } else { + name + } + } + fn param_list(node: &ast::FnDef) -> (bool, Vec, Vec) { let mut res = vec![]; let mut res_types = vec![]; @@ -230,7 +238,7 @@ impl From<&'_ ast::FnDef> for FunctionSignature { param_list .params() .map(|param| { - Some( + Some(strip_leading_underscore( param .pat()? .syntax() @@ -238,7 +246,7 @@ impl From<&'_ ast::FnDef> for FunctionSignature { .find_map(ast::Name::cast)? .text() .to_string(), - ) + )) }) .map(|param| param.unwrap_or_default()), ); -- cgit v1.2.3 From 4d13691ad192c20b2fb0f349582622cb5230da99 Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Sun, 24 May 2020 22:39:09 +0900 Subject: Reflect test case --- crates/ra_ide/src/completion/presentation.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 440ffa31d..6b29f58d5 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -641,7 +641,7 @@ mod tests { assert_debug_snapshot!( do_reference_completion( r" - fn with_args(x: i32, y: String) {} + fn with_args(x: i32, y: String, _z: bool) {} fn main() { with_<|> } " ), @@ -649,8 +649,8 @@ mod tests { [ CompletionItem { label: "main()", - source_range: 80..85, - delete: 80..85, + source_range: 90..95, + delete: 90..95, insert: "main()$0", kind: Function, lookup: "main", @@ -658,12 +658,12 @@ mod tests { }, CompletionItem { label: "with_args(…)", - source_range: 80..85, - delete: 80..85, - insert: "with_args(${1:x}, ${2:y})$0", + source_range: 90..95, + delete: 90..95, + insert: "with_args(${1:x}, ${2:y}, ${3:z})$0", kind: Function, lookup: "with_args", - detail: "fn with_args(x: i32, y: String)", + detail: "fn with_args(x: i32, y: String, _z: bool)", trigger_call_info: true, }, ] -- cgit v1.2.3 From 378bfc3c8f1c63f44f972e11c9de00f9733b13c0 Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Sun, 24 May 2020 23:01:26 +0900 Subject: Separate assertions --- crates/ra_ide/src/completion/presentation.rs | 72 +++++++++++++++++++++++++--- 1 file changed, 65 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 6b29f58d5..5708ff291 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -641,7 +641,7 @@ mod tests { assert_debug_snapshot!( do_reference_completion( r" - fn with_args(x: i32, y: String, _z: bool) {} + fn with_args(x: i32, y: String) {} fn main() { with_<|> } " ), @@ -649,8 +649,8 @@ mod tests { [ CompletionItem { label: "main()", - source_range: 90..95, - delete: 90..95, + source_range: 80..85, + delete: 80..85, insert: "main()$0", kind: Function, lookup: "main", @@ -658,12 +658,43 @@ mod tests { }, CompletionItem { label: "with_args(…)", - source_range: 90..95, - delete: 90..95, - insert: "with_args(${1:x}, ${2:y}, ${3:z})$0", + source_range: 80..85, + delete: 80..85, + insert: "with_args(${1:x}, ${2:y})$0", kind: Function, lookup: "with_args", - detail: "fn with_args(x: i32, y: String, _z: bool)", + detail: "fn with_args(x: i32, y: String)", + trigger_call_info: true, + }, + ] + "### + ); + assert_debug_snapshot!( + do_reference_completion( + r" + fn with_ignored_args(_foo: i32, b_a_r_: bool) {} + fn main() { with_<|> } + " + ), + @r###" + [ + CompletionItem { + label: "main()", + source_range: 94..99, + delete: 94..99, + insert: "main()$0", + kind: Function, + lookup: "main", + detail: "fn main()", + }, + CompletionItem { + label: "with_ignored_args(…)", + source_range: 94..99, + delete: 94..99, + insert: "with_ignored_args(${1:foo}, ${2:b_a_r_})$0", + kind: Function, + lookup: "with_ignored_args", + detail: "fn with_ignored_args(_foo: i32, b_a_r_: bool)", trigger_call_info: true, }, ] @@ -695,6 +726,33 @@ mod tests { ] "### ); + assert_debug_snapshot!( + do_reference_completion( + r" + struct S {} + impl S { + fn foo_ignored_args(&self, _a: bool, b: i32) {} + } + fn bar(s: &S) { + s.f<|> + } + " + ), + @r###" + [ + CompletionItem { + label: "foo_ignored_args(…)", + source_range: 194..195, + delete: 194..195, + insert: "foo_ignored_args(${1:a}, ${2:b})$0", + kind: Method, + lookup: "foo_ignored_args", + detail: "fn foo_ignored_args(&self, _a: bool, b: i32)", + trigger_call_info: true, + }, + ] + "### + ); } #[test] -- cgit v1.2.3 From 408d04764cd00a9f24fce15198ec26d7c74fb4d6 Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Sun, 24 May 2020 23:43:44 +0900 Subject: Use built-in method --- crates/ra_ide/src/display/function_signature.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index 4f054398a..fabc78f14 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -187,14 +187,6 @@ impl FunctionSignature { impl From<&'_ ast::FnDef> for FunctionSignature { fn from(node: &ast::FnDef) -> FunctionSignature { - fn strip_leading_underscore(name: String) -> String { - if name.starts_with("_") { - name.get(1..).unwrap_or_default().to_string() - } else { - name - } - } - fn param_list(node: &ast::FnDef) -> (bool, Vec, Vec) { let mut res = vec![]; let mut res_types = vec![]; @@ -238,15 +230,17 @@ impl From<&'_ ast::FnDef> for FunctionSignature { param_list .params() .map(|param| { - Some(strip_leading_underscore( + Some( param .pat()? .syntax() .descendants() .find_map(ast::Name::cast)? .text() - .to_string(), - )) + .to_string() + .trim_start_matches('_') + .into(), + ) }) .map(|param| param.unwrap_or_default()), ); -- cgit v1.2.3 From 41d0f7f24ef924440ee710e134c01d6e47056c3f Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Mon, 25 May 2020 00:14:56 +0900 Subject: Fix test to consider multiple underscores --- crates/ra_ide/src/completion/presentation.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index 5708ff291..e59f730ff 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -672,7 +672,7 @@ mod tests { assert_debug_snapshot!( do_reference_completion( r" - fn with_ignored_args(_foo: i32, b_a_r_: bool) {} + fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String) {} fn main() { with_<|> } " ), @@ -680,8 +680,8 @@ mod tests { [ CompletionItem { label: "main()", - source_range: 94..99, - delete: 94..99, + source_range: 110..115, + delete: 110..115, insert: "main()$0", kind: Function, lookup: "main", @@ -689,12 +689,12 @@ mod tests { }, CompletionItem { label: "with_ignored_args(…)", - source_range: 94..99, - delete: 94..99, - insert: "with_ignored_args(${1:foo}, ${2:b_a_r_})$0", + source_range: 110..115, + delete: 110..115, + insert: "with_ignored_args(${1:foo}, ${2:bar}, ${3:ho_ge_})$0", kind: Function, lookup: "with_ignored_args", - detail: "fn with_ignored_args(_foo: i32, b_a_r_: bool)", + detail: "fn with_ignored_args(_foo: i32, ___bar: bool, ho_ge_: String)", trigger_call_info: true, }, ] -- cgit v1.2.3 From fd83f469e9643c7a9da2d9e4c796bd89c441458d Mon Sep 17 00:00:00 2001 From: Yuki Kodama Date: Tue, 26 May 2020 01:05:50 +0900 Subject: Trim at presentation layer --- crates/ra_ide/src/completion/presentation.rs | 2 +- crates/ra_ide/src/display/function_signature.rs | 4 +--- 2 files changed, 2 insertions(+), 4 deletions(-) (limited to 'crates/ra_ide/src') diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index e59f730ff..61565c84f 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -211,7 +211,7 @@ impl Completions { .parameter_names .iter() .skip(if function_signature.has_self_param { 1 } else { 0 }) - .cloned() + .map(|name| name.trim_start_matches('_').into()) .collect(); builder = builder.add_call_parens(ctx, name, Params::Named(params)); diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index fabc78f14..9572debd8 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -237,9 +237,7 @@ impl From<&'_ ast::FnDef> for FunctionSignature { .descendants() .find_map(ast::Name::cast)? .text() - .to_string() - .trim_start_matches('_') - .into(), + .to_string(), ) }) .map(|param| param.unwrap_or_default()), -- cgit v1.2.3