From e29b53f1e6ba067725c0cbbf01c8f0aa7e09913b Mon Sep 17 00:00:00 2001 From: Josh Mcguigan Date: Sat, 6 Mar 2021 09:53:21 -0800 Subject: generate function assist convert arg names to lower snake case --- .../ide_assists/src/handlers/generate_function.rs | 56 ++++++++++++++++++++-- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'crates/ide_assists') diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index 959824981..3870b7e75 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs @@ -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) { fn fn_arg_name(fn_arg: &ast::Expr) -> Option { 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( -- cgit v1.2.3