@@ -6,7 +6,7 @@ use syntax::{
6
6
ast:: {
7
7
self ,
8
8
edit:: { AstNodeEdit , IndentLevel } ,
9
- make, ArgList , ArgListOwner , AstNode , ModuleItemOwner ,
9
+ make, ArgListOwner , AstNode , ModuleItemOwner ,
10
10
} ,
11
11
SyntaxKind , SyntaxNode , TextSize ,
12
12
} ;
@@ -17,27 +17,6 @@ use crate::{
17
17
AssistContext , AssistId , AssistKind , Assists ,
18
18
} ;
19
19
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
-
41
20
// Assist: generate_function
42
21
//
43
22
// Adds a stub function with a signature matching the function under the cursor.
@@ -64,6 +43,31 @@ impl FuncExpr {
64
43
//
65
44
// ```
66
45
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 < ( ) > {
67
71
let path_expr: ast:: PathExpr = ctx. find_node_at_offset ( ) ?;
68
72
let call = path_expr. syntax ( ) . parent ( ) . and_then ( ast:: CallExpr :: cast) ?;
69
73
@@ -100,7 +104,7 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
100
104
)
101
105
}
102
106
103
- pub ( crate ) fn generate_method ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
107
+ fn gen_method ( acc : & mut Assists , ctx : & AssistContext ) -> Option < ( ) > {
104
108
let call: ast:: MethodCallExpr = ctx. find_node_at_offset ( ) ?;
105
109
let fn_name: ast:: NameRef = ast:: NameRef :: cast (
106
110
call. syntax ( ) . children ( ) . find ( |child| child. kind ( ) == SyntaxKind :: NAME_REF ) ?,
@@ -1351,8 +1355,7 @@ fn bar(baz: ()) {}
1351
1355
1352
1356
#[ test]
1353
1357
fn create_method_with_no_args ( ) {
1354
- // FIXME: This is wrong, this should just work.
1355
- check_assist_not_applicable (
1358
+ check_assist (
1356
1359
generate_function,
1357
1360
r#"
1358
1361
struct Foo;
@@ -1361,7 +1364,19 @@ impl Foo {
1361
1364
self.bar()$0;
1362
1365
}
1363
1366
}
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
+ "# ,
1365
1380
)
1366
1381
}
1367
1382
@@ -1389,52 +1404,38 @@ async fn bar(arg: i32) ${0:-> ()} {
1389
1404
#[ test]
1390
1405
fn create_method ( ) {
1391
1406
check_assist (
1392
- generate_method ,
1407
+ generate_function ,
1393
1408
r"
1394
1409
struct S;
1395
-
1396
- fn foo() {
1397
- S.bar$0();
1398
- }
1399
-
1410
+ fn foo() {S.bar$0();}
1400
1411
" ,
1401
1412
r"
1402
1413
struct S;
1403
-
1404
- fn foo() {
1405
- S.bar();
1406
- }
1414
+ fn foo() {S.bar();}
1407
1415
impl S {
1408
1416
1409
1417
1410
1418
fn bar(&self) ${0:-> ()} {
1411
1419
todo!()
1412
1420
}
1413
1421
}
1414
-
1415
1422
" ,
1416
1423
)
1417
1424
}
1418
1425
1419
1426
#[ test]
1420
1427
fn create_method_within_an_impl ( ) {
1421
1428
check_assist (
1422
- generate_method ,
1429
+ generate_function ,
1423
1430
r"
1424
1431
struct S;
1425
-
1426
- fn foo() {
1427
- S.bar$0();
1428
- }
1432
+ fn foo() {S.bar$0();}
1429
1433
impl S {}
1430
1434
1431
1435
" ,
1432
1436
r"
1433
1437
struct S;
1434
-
1435
- fn foo() {
1436
- S.bar();
1437
- }
1438
+ fn foo() {S.bar();}
1438
1439
impl S {
1439
1440
fn bar(&self) ${0:-> ()} {
1440
1441
todo!()
@@ -1448,15 +1449,12 @@ impl S {
1448
1449
#[ test]
1449
1450
fn create_method_from_different_module ( ) {
1450
1451
check_assist (
1451
- generate_method ,
1452
+ generate_function ,
1452
1453
r"
1453
1454
mod s {
1454
1455
pub struct S;
1455
1456
}
1456
- fn foo() {
1457
- s::S.bar$0();
1458
- }
1459
-
1457
+ fn foo() {s::S.bar$0();}
1460
1458
" ,
1461
1459
r"
1462
1460
mod s {
@@ -1469,18 +1467,15 @@ impl S {
1469
1467
}
1470
1468
}
1471
1469
}
1472
- fn foo() {
1473
- s::S.bar();
1474
- }
1475
-
1470
+ fn foo() {s::S.bar();}
1476
1471
" ,
1477
1472
)
1478
1473
}
1479
1474
1480
1475
#[ test]
1481
1476
fn create_method_from_descendant_module ( ) {
1482
1477
check_assist (
1483
- generate_method ,
1478
+ generate_function ,
1484
1479
r"
1485
1480
struct S;
1486
1481
mod s {
@@ -1512,29 +1507,21 @@ fn bar(&self) ${0:-> ()} {
1512
1507
#[ test]
1513
1508
fn create_method_with_cursor_anywhere_on_call_expresion ( ) {
1514
1509
check_assist (
1515
- generate_method ,
1510
+ generate_function ,
1516
1511
r"
1517
1512
struct S;
1518
-
1519
- fn foo() {
1520
- $0S.bar();
1521
- }
1522
-
1513
+ fn foo() {$0S.bar();}
1523
1514
" ,
1524
1515
r"
1525
1516
struct S;
1526
-
1527
- fn foo() {
1528
- S.bar();
1529
- }
1517
+ fn foo() {S.bar();}
1530
1518
impl S {
1531
1519
1532
1520
1533
1521
fn bar(&self) ${0:-> ()} {
1534
1522
todo!()
1535
1523
}
1536
1524
}
1537
-
1538
1525
" ,
1539
1526
)
1540
1527
}
0 commit comments