From 9217f6dcf73ee9dd9fae8888ed33ada96ec49110 Mon Sep 17 00:00:00 2001 From: mahdi-frms Date: Mon, 9 Aug 2021 19:21:15 +0430 Subject: [PATCH] method gen assist usable in all of expression --- .../src/handlers/generate_function.rs | 34 ++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/crates/ide_assists/src/handlers/generate_function.rs b/crates/ide_assists/src/handlers/generate_function.rs index 7321c1f38e..39756611dd 100644 --- a/crates/ide_assists/src/handlers/generate_function.rs +++ b/crates/ide_assists/src/handlers/generate_function.rs @@ -101,8 +101,10 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio } pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { - let fn_name: ast::NameRef = ctx.find_node_at_offset()?; let call: ast::MethodCallExpr = ctx.find_node_at_offset()?; + let fn_name: ast::NameRef = ast::NameRef::cast( + call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?, + )?; let ty = ctx.sema.type_of_expr(&call.receiver()?)?.original().strip_references().as_adt()?; let current_module = @@ -1498,6 +1500,36 @@ mod s { impl S { +fn bar(&self) ${0:-> ()} { + todo!() +} +} + +", + ) + } + + #[test] + fn create_method_with_cursor_anywhere_on_call_expresion() { + check_assist( + generate_method, + r" +struct S; + +fn foo() { + $0S.bar(); +} + +", + r" +struct S; + +fn foo() { + S.bar(); +} +impl S { + + fn bar(&self) ${0:-> ()} { todo!() }