aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers
diff options
context:
space:
mode:
authorJosh Mcguigan <[email protected]>2021-03-06 17:53:21 +0000
committerJosh Mcguigan <[email protected]>2021-03-06 17:53:21 +0000
commite29b53f1e6ba067725c0cbbf01c8f0aa7e09913b (patch)
tree6643987173e3748feda4f7cbedb329adb7e1768d /crates/ide_assists/src/handlers
parentcd60c4f76ce21bac16ca90d94b29096739c9c551 (diff)
generate function assist convert arg names to lower snake case
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r--crates/ide_assists/src/handlers/generate_function.rs56
1 files changed, 52 insertions, 4 deletions
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 @@
1use hir::HirDisplay; 1use hir::HirDisplay;
2use ide_db::{base_db::FileId, helpers::SnippetCap}; 2use ide_db::{base_db::FileId, helpers::SnippetCap};
3use rustc_hash::{FxHashMap, FxHashSet}; 3use rustc_hash::{FxHashMap, FxHashSet};
4use stdx::to_lower_snake_case;
4use syntax::{ 5use syntax::{
5 ast::{ 6 ast::{
6 self, 7 self,
@@ -257,14 +258,15 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
257fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> { 258fn fn_arg_name(fn_arg: &ast::Expr) -> Option<String> {
258 match fn_arg { 259 match fn_arg {
259 ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?), 260 ast::Expr::CastExpr(cast_expr) => fn_arg_name(&cast_expr.expr()?),
260 _ => Some( 261 _ => {
261 fn_arg 262 let s = fn_arg
262 .syntax() 263 .syntax()
263 .descendants() 264 .descendants()
264 .filter(|d| ast::NameRef::can_cast(d.kind())) 265 .filter(|d| ast::NameRef::can_cast(d.kind()))
265 .last()? 266 .last()?
266 .to_string(), 267 .to_string();
267 ), 268 Some(to_lower_snake_case(&s))
269 }
268 } 270 }
269} 271}
270 272
@@ -448,6 +450,52 @@ mod baz {
448 } 450 }
449 451
450 #[test] 452 #[test]
453 fn add_function_with_upper_camel_case_arg() {
454 check_assist(
455 generate_function,
456 r"
457struct BazBaz;
458fn foo() {
459 bar$0(BazBaz);
460}
461",
462 r"
463struct BazBaz;
464fn foo() {
465 bar(BazBaz);
466}
467
468fn 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"
480struct BazBaz;
481fn foo() {
482 bar$0(&BazBaz as *const BazBaz);
483}
484",
485 r"
486struct BazBaz;
487fn foo() {
488 bar(&BazBaz as *const BazBaz);
489}
490
491fn bar(baz_baz: *const BazBaz) ${0:-> ()} {
492 todo!()
493}
494",
495 );
496 }
497
498 #[test]
451 fn add_function_with_function_call_arg() { 499 fn add_function_with_function_call_arg() {
452 check_assist( 500 check_assist(
453 generate_function, 501 generate_function,