7894: generate_function assist: convert arg names to lower snake case r=Veykril a=JoshMcguigan

This PR fixes one of the points listed by @TimoFreiberg in #3639. Specifically that all generated argument names should be converted to lower snake case. 

```rust
struct BazBaz;

fn foo() {
    bar$0(BazBaz);
    // ^ when triggering the assist here, you get the output below
}

// BEFORE
fn bar(BazBaz: BazBaz) ${0:-> ()} {
    todo!()
}

// AFTER
fn bar(baz_baz: BazBaz) ${0:-> ()} {
    todo!()
}
```

Co-authored-by: Josh Mcguigan <joshmcg88@gmail.com>
This commit is contained in:
bors[bot] 2021-03-06 18:44:34 +00:00 committed by GitHub
commit 71b8fb7c57
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,6 +1,7 @@
use hir::HirDisplay;
use ide_db::{base_db::FileId, helpers::SnippetCap};
use rustc_hash::{FxHashMap, FxHashSet};
use stdx::to_lower_snake_case;
use syntax::{
ast::{
self,
@ -257,14 +258,15 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
match fn_arg {
ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
_ => Some(
fn_arg
_ => {
let s = fn_arg
.syntax()
.descendants()
.filter(|d| ast::NameRef::can_cast(d.kind()))
.last()?
.to_string(),
),
.to_string();
Some(to_lower_snake_case(&s))
}
}
}
@ -447,6 +449,52 @@ mod baz {
)
}
#[test]
fn add_function_with_upper_camel_case_arg() {
check_assist(
generate_function,
r"
struct BazBaz;
fn foo() {
bar$0(BazBaz);
}
",
r"
struct BazBaz;
fn foo() {
bar(BazBaz);
}
fn bar(baz_baz: BazBaz) ${0:-> ()} {
todo!()
}
",
);
}
#[test]
fn add_function_with_upper_camel_case_arg_as_cast() {
check_assist(
generate_function,
r"
struct BazBaz;
fn foo() {
bar$0(&BazBaz as *const BazBaz);
}
",
r"
struct BazBaz;
fn foo() {
bar(&BazBaz as *const BazBaz);
}
fn bar(baz_baz: *const BazBaz) ${0:-> ()} {
todo!()
}
",
);
}
#[test]
fn add_function_with_function_call_arg() {
check_assist(