|
1 |
| -use hir::{HasSource, HirDisplay, Module, TypeInfo}; |
2 |
| -use ide_db::{base_db::FileId, helpers::SnippetCap}; |
| 1 | +use hir::{HasSource, HirDisplay, Module, ModuleDef, Semantics, TypeInfo}; |
| 2 | +use ide_db::{ |
| 3 | + base_db::FileId, |
| 4 | + defs::{Definition, NameRefClass}, |
| 5 | + helpers::SnippetCap, |
| 6 | + RootDatabase, |
| 7 | +}; |
3 | 8 | use rustc_hash::{FxHashMap, FxHashSet};
|
4 | 9 | use stdx::to_lower_snake_case;
|
5 | 10 | use syntax::{
|
@@ -438,7 +443,7 @@ fn fn_args(
|
438 | 443 | let mut arg_names = Vec::new();
|
439 | 444 | let mut arg_types = Vec::new();
|
440 | 445 | for arg in call.arg_list()?.args() {
|
441 |
| - arg_names.push(fn_arg_name(&arg)); |
| 446 | + arg_names.push(fn_arg_name(&ctx.sema, &arg)); |
442 | 447 | arg_types.push(match fn_arg_type(ctx, target_module, &arg) {
|
443 | 448 | Some(ty) => {
|
444 | 449 | if !ty.is_empty() && ty.starts_with('&') {
|
@@ -503,12 +508,18 @@ fn deduplicate_arg_names(arg_names: &mut Vec<String>) {
|
503 | 508 | }
|
504 | 509 | }
|
505 | 510 |
|
506 |
| -fn fn_arg_name(arg_expr: &ast::Expr) -> String { |
| 511 | +fn fn_arg_name(sema: &Semantics<RootDatabase>, arg_expr: &ast::Expr) -> String { |
507 | 512 | let name = (|| match arg_expr {
|
508 |
| - ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(&cast_expr.expr()?)), |
| 513 | + ast::Expr::CastExpr(cast_expr) => Some(fn_arg_name(sema, &cast_expr.expr()?)), |
509 | 514 | expr => {
|
510 |
| - let s = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?.to_string(); |
511 |
| - Some(to_lower_snake_case(&s)) |
| 515 | + let name_ref = expr.syntax().descendants().filter_map(ast::NameRef::cast).last()?; |
| 516 | + if let Some(NameRefClass::Definition(Definition::ModuleDef( |
| 517 | + ModuleDef::Const(_) | ModuleDef::Static(_), |
| 518 | + ))) = NameRefClass::classify(sema, &name_ref) |
| 519 | + { |
| 520 | + return Some(name_ref.to_string().to_lowercase()); |
| 521 | + }; |
| 522 | + Some(to_lower_snake_case(&name_ref.to_string())) |
512 | 523 | }
|
513 | 524 | })();
|
514 | 525 | match name {
|
@@ -1683,6 +1694,75 @@ fn main() {
|
1683 | 1694 | fn foo(arg0: ()) ${0:-> _} {
|
1684 | 1695 | todo!()
|
1685 | 1696 | }
|
| 1697 | +", |
| 1698 | + ) |
| 1699 | + } |
| 1700 | + |
| 1701 | + #[test] |
| 1702 | + fn add_function_with_const_arg() { |
| 1703 | + check_assist( |
| 1704 | + generate_function, |
| 1705 | + r" |
| 1706 | +const VALUE: usize = 0; |
| 1707 | +fn main() { |
| 1708 | + foo$0(VALUE); |
| 1709 | +} |
| 1710 | +", |
| 1711 | + r" |
| 1712 | +const VALUE: usize = 0; |
| 1713 | +fn main() { |
| 1714 | + foo(VALUE); |
| 1715 | +} |
| 1716 | +
|
| 1717 | +fn foo(value: usize) ${0:-> _} { |
| 1718 | + todo!() |
| 1719 | +} |
| 1720 | +", |
| 1721 | + ) |
| 1722 | + } |
| 1723 | + |
| 1724 | + #[test] |
| 1725 | + fn add_function_with_static_arg() { |
| 1726 | + check_assist( |
| 1727 | + generate_function, |
| 1728 | + r" |
| 1729 | +static VALUE: usize = 0; |
| 1730 | +fn main() { |
| 1731 | + foo$0(VALUE); |
| 1732 | +} |
| 1733 | +", |
| 1734 | + r" |
| 1735 | +static VALUE: usize = 0; |
| 1736 | +fn main() { |
| 1737 | + foo(VALUE); |
| 1738 | +} |
| 1739 | +
|
| 1740 | +fn foo(value: usize) ${0:-> _} { |
| 1741 | + todo!() |
| 1742 | +} |
| 1743 | +", |
| 1744 | + ) |
| 1745 | + } |
| 1746 | + |
| 1747 | + #[test] |
| 1748 | + fn add_function_with_static_mut_arg() { |
| 1749 | + check_assist( |
| 1750 | + generate_function, |
| 1751 | + r" |
| 1752 | +static mut VALUE: usize = 0; |
| 1753 | +fn main() { |
| 1754 | + foo$0(VALUE); |
| 1755 | +} |
| 1756 | +", |
| 1757 | + r" |
| 1758 | +static mut VALUE: usize = 0; |
| 1759 | +fn main() { |
| 1760 | + foo(VALUE); |
| 1761 | +} |
| 1762 | +
|
| 1763 | +fn foo(value: usize) ${0:-> _} { |
| 1764 | + todo!() |
| 1765 | +} |
1686 | 1766 | ",
|
1687 | 1767 | )
|
1688 | 1768 | }
|
|
0 commit comments