Skip to content

generate_function assist: convert arg names to lower snake case #7894

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 6, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 52 additions & 4 deletions crates/ide_assists/src/handlers/generate_function.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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))
}
}
}

Expand Down Expand Up @@ -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(
Expand Down