Skip to content

Commit 311dc5b

Browse files
committed
add test for method generation assist
1 parent 3c31f38 commit 311dc5b

File tree

1 file changed

+124
-1
lines changed

1 file changed

+124
-1
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 124 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<
139139
builder.edit_file(function_template.file);
140140
let mut new_fn = function_template.to_string(ctx.config.snippet_cap);
141141
if impl_.is_none() {
142-
new_fn = format!("\nimpl {} {{\n {}\n}}", ty.name(ctx.sema.db), new_fn,);
142+
new_fn = format!("\nimpl {} {{\n{}\n}}", ty.name(ctx.sema.db), new_fn,);
143143
}
144144
match ctx.config.snippet_cap {
145145
Some(cap) => builder.insert_snippet(cap, function_template.insert_offset, new_fn),
@@ -1380,6 +1380,129 @@ fn foo() {
13801380
async fn bar(arg: i32) ${0:-> ()} {
13811381
todo!()
13821382
}
1383+
",
1384+
)
1385+
}
1386+
1387+
#[test]
1388+
fn create_method() {
1389+
check_assist(
1390+
generate_method,
1391+
r"
1392+
struct S;
1393+
1394+
fn foo() {
1395+
S.bar$0();
1396+
}
1397+
1398+
",
1399+
r"
1400+
struct S;
1401+
1402+
fn foo() {
1403+
S.bar();
1404+
}
1405+
impl S {
1406+
1407+
1408+
fn bar(&self) ${0:-> ()} {
1409+
todo!()
1410+
}
1411+
}
1412+
1413+
",
1414+
)
1415+
}
1416+
1417+
#[test]
1418+
fn create_method_within_an_impl() {
1419+
check_assist(
1420+
generate_method,
1421+
r"
1422+
struct S;
1423+
1424+
fn foo() {
1425+
S.bar$0();
1426+
}
1427+
impl S {}
1428+
1429+
",
1430+
r"
1431+
struct S;
1432+
1433+
fn foo() {
1434+
S.bar();
1435+
}
1436+
impl S {
1437+
fn bar(&self) ${0:-> ()} {
1438+
todo!()
1439+
}
1440+
}
1441+
1442+
",
1443+
)
1444+
}
1445+
1446+
#[test]
1447+
fn create_method_from_different_module() {
1448+
check_assist(
1449+
generate_method,
1450+
r"
1451+
mod s {
1452+
pub struct S;
1453+
}
1454+
fn foo() {
1455+
s::S.bar$0();
1456+
}
1457+
1458+
",
1459+
r"
1460+
mod s {
1461+
pub struct S;
1462+
impl S {
1463+
1464+
1465+
pub(crate) fn bar(&self) ${0:-> ()} {
1466+
todo!()
1467+
}
1468+
}
1469+
}
1470+
fn foo() {
1471+
s::S.bar();
1472+
}
1473+
1474+
",
1475+
)
1476+
}
1477+
1478+
#[test]
1479+
fn create_method_from_descendant_module() {
1480+
check_assist(
1481+
generate_method,
1482+
r"
1483+
struct S;
1484+
mod s {
1485+
fn foo() {
1486+
super::S.bar$0();
1487+
}
1488+
}
1489+
1490+
",
1491+
r"
1492+
struct S;
1493+
mod s {
1494+
fn foo() {
1495+
super::S.bar();
1496+
}
1497+
}
1498+
impl S {
1499+
1500+
1501+
fn bar(&self) ${0:-> ()} {
1502+
todo!()
1503+
}
1504+
}
1505+
13831506
",
13841507
)
13851508
}

0 commit comments

Comments
 (0)