Skip to content

Commit 05a789c

Browse files
committed
refactor method generation assist
1 parent 9217f6d commit 05a789c

File tree

2 files changed

+54
-68
lines changed

2 files changed

+54
-68
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 54 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use syntax::{
66
ast::{
77
self,
88
edit::{AstNodeEdit, IndentLevel},
9-
make, ArgList, ArgListOwner, AstNode, ModuleItemOwner,
9+
make, ArgListOwner, AstNode, ModuleItemOwner,
1010
},
1111
SyntaxKind, SyntaxNode, TextSize,
1212
};
@@ -17,27 +17,6 @@ use crate::{
1717
AssistContext, AssistId, AssistKind, Assists,
1818
};
1919

20-
enum FuncExpr {
21-
Func(ast::CallExpr),
22-
Method(ast::MethodCallExpr),
23-
}
24-
25-
impl FuncExpr {
26-
fn arg_list(&self) -> Option<ArgList> {
27-
match self {
28-
FuncExpr::Func(fn_call) => fn_call.arg_list(),
29-
FuncExpr::Method(m_call) => m_call.arg_list(),
30-
}
31-
}
32-
33-
fn syntax(&self) -> &SyntaxNode {
34-
match self {
35-
FuncExpr::Func(fn_call) => fn_call.syntax(),
36-
FuncExpr::Method(m_call) => m_call.syntax(),
37-
}
38-
}
39-
}
40-
4120
// Assist: generate_function
4221
//
4322
// Adds a stub function with a signature matching the function under the cursor.
@@ -64,6 +43,31 @@ impl FuncExpr {
6443
//
6544
// ```
6645
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
46+
gen_fn(acc, ctx).or_else(|| gen_method(acc, ctx))
47+
}
48+
49+
enum FuncExpr {
50+
Func(ast::CallExpr),
51+
Method(ast::MethodCallExpr),
52+
}
53+
54+
impl FuncExpr {
55+
fn arg_list(&self) -> Option<ast::ArgList> {
56+
match self {
57+
FuncExpr::Func(fn_call) => fn_call.arg_list(),
58+
FuncExpr::Method(m_call) => m_call.arg_list(),
59+
}
60+
}
61+
62+
fn syntax(&self) -> &SyntaxNode {
63+
match self {
64+
FuncExpr::Func(fn_call) => fn_call.syntax(),
65+
FuncExpr::Method(m_call) => m_call.syntax(),
66+
}
67+
}
68+
}
69+
70+
fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
6771
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
6872
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
6973

@@ -100,7 +104,7 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
100104
)
101105
}
102106

103-
pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
107+
fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
104108
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
105109
let fn_name: ast::NameRef = ast::NameRef::cast(
106110
call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?,
@@ -1351,8 +1355,7 @@ fn bar(baz: ()) {}
13511355

13521356
#[test]
13531357
fn create_method_with_no_args() {
1354-
// FIXME: This is wrong, this should just work.
1355-
check_assist_not_applicable(
1358+
check_assist(
13561359
generate_function,
13571360
r#"
13581361
struct Foo;
@@ -1361,7 +1364,19 @@ impl Foo {
13611364
self.bar()$0;
13621365
}
13631366
}
1364-
"#,
1367+
"#,
1368+
r#"
1369+
struct Foo;
1370+
impl Foo {
1371+
fn foo(&self) {
1372+
self.bar();
1373+
}
1374+
1375+
fn bar(&self) ${0:-> ()} {
1376+
todo!()
1377+
}
1378+
}
1379+
"#,
13651380
)
13661381
}
13671382

@@ -1389,52 +1404,38 @@ async fn bar(arg: i32) ${0:-> ()} {
13891404
#[test]
13901405
fn create_method() {
13911406
check_assist(
1392-
generate_method,
1407+
generate_function,
13931408
r"
13941409
struct S;
1395-
1396-
fn foo() {
1397-
S.bar$0();
1398-
}
1399-
1410+
fn foo() {S.bar$0();}
14001411
",
14011412
r"
14021413
struct S;
1403-
1404-
fn foo() {
1405-
S.bar();
1406-
}
1414+
fn foo() {S.bar();}
14071415
impl S {
14081416
14091417
14101418
fn bar(&self) ${0:-> ()} {
14111419
todo!()
14121420
}
14131421
}
1414-
14151422
",
14161423
)
14171424
}
14181425

14191426
#[test]
14201427
fn create_method_within_an_impl() {
14211428
check_assist(
1422-
generate_method,
1429+
generate_function,
14231430
r"
14241431
struct S;
1425-
1426-
fn foo() {
1427-
S.bar$0();
1428-
}
1432+
fn foo() {S.bar$0();}
14291433
impl S {}
14301434
14311435
",
14321436
r"
14331437
struct S;
1434-
1435-
fn foo() {
1436-
S.bar();
1437-
}
1438+
fn foo() {S.bar();}
14381439
impl S {
14391440
fn bar(&self) ${0:-> ()} {
14401441
todo!()
@@ -1448,15 +1449,12 @@ impl S {
14481449
#[test]
14491450
fn create_method_from_different_module() {
14501451
check_assist(
1451-
generate_method,
1452+
generate_function,
14521453
r"
14531454
mod s {
14541455
pub struct S;
14551456
}
1456-
fn foo() {
1457-
s::S.bar$0();
1458-
}
1459-
1457+
fn foo() {s::S.bar$0();}
14601458
",
14611459
r"
14621460
mod s {
@@ -1469,18 +1467,15 @@ impl S {
14691467
}
14701468
}
14711469
}
1472-
fn foo() {
1473-
s::S.bar();
1474-
}
1475-
1470+
fn foo() {s::S.bar();}
14761471
",
14771472
)
14781473
}
14791474

14801475
#[test]
14811476
fn create_method_from_descendant_module() {
14821477
check_assist(
1483-
generate_method,
1478+
generate_function,
14841479
r"
14851480
struct S;
14861481
mod s {
@@ -1512,29 +1507,21 @@ fn bar(&self) ${0:-> ()} {
15121507
#[test]
15131508
fn create_method_with_cursor_anywhere_on_call_expresion() {
15141509
check_assist(
1515-
generate_method,
1510+
generate_function,
15161511
r"
15171512
struct S;
1518-
1519-
fn foo() {
1520-
$0S.bar();
1521-
}
1522-
1513+
fn foo() {$0S.bar();}
15231514
",
15241515
r"
15251516
struct S;
1526-
1527-
fn foo() {
1528-
S.bar();
1529-
}
1517+
fn foo() {S.bar();}
15301518
impl S {
15311519
15321520
15331521
fn bar(&self) ${0:-> ()} {
15341522
todo!()
15351523
}
15361524
}
1537-
15381525
",
15391526
)
15401527
}

crates/ide_assists/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ mod handlers {
151151
generate_enum_projection_method::generate_enum_try_into_method,
152152
generate_from_impl_for_enum::generate_from_impl_for_enum,
153153
generate_function::generate_function,
154-
generate_function::generate_method,
155154
generate_getter::generate_getter,
156155
generate_getter::generate_getter_mut,
157156
generate_impl::generate_impl,

0 commit comments

Comments
 (0)