Skip to content

Commit 9217f6d

Browse files
committed
method gen assist usable in all of expression
1 parent 311dc5b commit 9217f6d

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

crates/ide_assists/src/handlers/generate_function.rs

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,10 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
101101
}
102102

103103
pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
104-
let fn_name: ast::NameRef = ctx.find_node_at_offset()?;
105104
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
105+
let fn_name: ast::NameRef = ast::NameRef::cast(
106+
call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?,
107+
)?;
106108
let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?;
107109

108110
let current_module =
@@ -1498,6 +1500,36 @@ mod s {
14981500
impl S {
14991501
15001502
1503+
fn bar(&self) ${0:-> ()} {
1504+
todo!()
1505+
}
1506+
}
1507+
1508+
",
1509+
)
1510+
}
1511+
1512+
#[test]
1513+
fn create_method_with_cursor_anywhere_on_call_expresion() {
1514+
check_assist(
1515+
generate_method,
1516+
r"
1517+
struct S;
1518+
1519+
fn foo() {
1520+
$0S.bar();
1521+
}
1522+
1523+
",
1524+
r"
1525+
struct S;
1526+
1527+
fn foo() {
1528+
S.bar();
1529+
}
1530+
impl S {
1531+
1532+
15011533
fn bar(&self) ${0:-> ()} {
15021534
todo!()
15031535
}

0 commit comments

Comments
 (0)