Skip to content

Commit 71b8fb7

Browse files
Merge #7894
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 <[email protected]>
2 parents cd60c4f + e29b53f commit 71b8fb7

File tree

1 file changed

+52
-4
lines changed

1 file changed

+52
-4
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use hir::HirDisplay;
22
use ide_db::{base_db::FileId, helpers::SnippetCap};
33
use rustc_hash::{FxHashMap, FxHashSet};
4+
use stdx::to_lower_snake_case;
45
use syntax::{
56
ast::{
67
self,
@@ -257,14 +258,15 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
257258
fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
258259
match fn_arg {
259260
ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
260-
_ => Some(
261-
fn_arg
261+
_ => {
262+
let s = fn_arg
262263
.syntax()
263264
.descendants()
264265
.filter(|d| ast::NameRef::can_cast(d.kind()))
265266
.last()?
266-
.to_string(),
267-
),
267+
.to_string();
268+
Some(to_lower_snake_case(&s))
269+
}
268270
}
269271
}
270272

@@ -447,6 +449,52 @@ mod baz {
447449
)
448450
}
449451

452+
#[test]
453+
fn add_function_with_upper_camel_case_arg() {
454+
check_assist(
455+
generate_function,
456+
r"
457+
struct BazBaz;
458+
fn foo() {
459+
bar$0(BazBaz);
460+
}
461+
",
462+
r"
463+
struct BazBaz;
464+
fn foo() {
465+
bar(BazBaz);
466+
}
467+
468+
fn bar(baz_baz: BazBaz) ${0:-> ()} {
469+
todo!()
470+
}
471+
",
472+
);
473+
}
474+
475+
#[test]
476+
fn add_function_with_upper_camel_case_arg_as_cast() {
477+
check_assist(
478+
generate_function,
479+
r"
480+
struct BazBaz;
481+
fn foo() {
482+
bar$0(&BazBaz as *const BazBaz);
483+
}
484+
",
485+
r"
486+
struct BazBaz;
487+
fn foo() {
488+
bar(&BazBaz as *const BazBaz);
489+
}
490+
491+
fn bar(baz_baz: *const BazBaz) ${0:-> ()} {
492+
todo!()
493+
}
494+
",
495+
);
496+
}
497+
450498
#[test]
451499
fn add_function_with_function_call_arg() {
452500
check_assist(

0 commit comments

Comments
 (0)