refactor method generation assist

This commit is contained in:
mahdi-frms 2021-08-09 20:37:07 +04:30
parent 9217f6dcf7
commit 05a789c09d
2 changed files with 54 additions and 68 deletions

View file

@ -6,7 +6,7 @@ use syntax::{
ast::{ ast::{
self, self,
edit::{AstNodeEdit, IndentLevel}, edit::{AstNodeEdit, IndentLevel},
make, ArgList, ArgListOwner, AstNode, ModuleItemOwner, make, ArgListOwner, AstNode, ModuleItemOwner,
}, },
SyntaxKind, SyntaxNode, TextSize, SyntaxKind, SyntaxNode, TextSize,
}; };
@ -17,27 +17,6 @@ use crate::{
AssistContext, AssistId, AssistKind, Assists, AssistContext, AssistId, AssistKind, Assists,
}; };
enum FuncExpr {
Func(ast::CallExpr),
Method(ast::MethodCallExpr),
}
impl FuncExpr {
fn arg_list(&self) -> Option<ArgList> {
match self {
FuncExpr::Func(fn_call) => fn_call.arg_list(),
FuncExpr::Method(m_call) => m_call.arg_list(),
}
}
fn syntax(&self) -> &SyntaxNode {
match self {
FuncExpr::Func(fn_call) => fn_call.syntax(),
FuncExpr::Method(m_call) => m_call.syntax(),
}
}
}
// Assist: generate_function // Assist: generate_function
// //
// Adds a stub function with a signature matching the function under the cursor. // Adds a stub function with a signature matching the function under the cursor.
@ -64,6 +43,31 @@ impl FuncExpr {
// //
// ``` // ```
pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
gen_fn(acc, ctx).or_else(|| gen_method(acc, ctx))
}
enum FuncExpr {
Func(ast::CallExpr),
Method(ast::MethodCallExpr),
}
impl FuncExpr {
fn arg_list(&self) -> Option<ast::ArgList> {
match self {
FuncExpr::Func(fn_call) => fn_call.arg_list(),
FuncExpr::Method(m_call) => m_call.arg_list(),
}
}
fn syntax(&self) -> &SyntaxNode {
match self {
FuncExpr::Func(fn_call) => fn_call.syntax(),
FuncExpr::Method(m_call) => m_call.syntax(),
}
}
}
fn gen_fn(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let path_expr: ast::PathExpr = ctx.find_node_at_offset()?; let path_expr: ast::PathExpr = ctx.find_node_at_offset()?;
let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?; let call = path_expr.syntax().parent().and_then(ast::CallExpr::cast)?;
@ -100,7 +104,7 @@ pub(crate) fn generate_function(acc: &mut Assists, ctx: &AssistContext) -> Optio
) )
} }
pub(crate) fn generate_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { fn gen_method(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let call: ast::MethodCallExpr = ctx.find_node_at_offset()?; let call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
let fn_name: ast::NameRef = ast::NameRef::cast( let fn_name: ast::NameRef = ast::NameRef::cast(
call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?, call.syntax().children().find(|child| child.kind() == SyntaxKind::NAME_REF)?,
@ -1351,8 +1355,7 @@ fn bar(baz: ()) {}
#[test] #[test]
fn create_method_with_no_args() { fn create_method_with_no_args() {
// FIXME: This is wrong, this should just work. check_assist(
check_assist_not_applicable(
generate_function, generate_function,
r#" r#"
struct Foo; struct Foo;
@ -1361,6 +1364,18 @@ impl Foo {
self.bar()$0; self.bar()$0;
} }
} }
"#,
r#"
struct Foo;
impl Foo {
fn foo(&self) {
self.bar();
}
fn bar(&self) ${0:-> ()} {
todo!()
}
}
"#, "#,
) )
} }
@ -1389,21 +1404,14 @@ async fn bar(arg: i32) ${0:-> ()} {
#[test] #[test]
fn create_method() { fn create_method() {
check_assist( check_assist(
generate_method, generate_function,
r" r"
struct S; struct S;
fn foo() {S.bar$0();}
fn foo() {
S.bar$0();
}
", ",
r" r"
struct S; struct S;
fn foo() {S.bar();}
fn foo() {
S.bar();
}
impl S { impl S {
@ -1411,7 +1419,6 @@ fn bar(&self) ${0:-> ()} {
todo!() todo!()
} }
} }
", ",
) )
} }
@ -1419,22 +1426,16 @@ fn bar(&self) ${0:-> ()} {
#[test] #[test]
fn create_method_within_an_impl() { fn create_method_within_an_impl() {
check_assist( check_assist(
generate_method, generate_function,
r" r"
struct S; struct S;
fn foo() {S.bar$0();}
fn foo() {
S.bar$0();
}
impl S {} impl S {}
", ",
r" r"
struct S; struct S;
fn foo() {S.bar();}
fn foo() {
S.bar();
}
impl S { impl S {
fn bar(&self) ${0:-> ()} { fn bar(&self) ${0:-> ()} {
todo!() todo!()
@ -1448,15 +1449,12 @@ impl S {
#[test] #[test]
fn create_method_from_different_module() { fn create_method_from_different_module() {
check_assist( check_assist(
generate_method, generate_function,
r" r"
mod s { mod s {
pub struct S; pub struct S;
} }
fn foo() { fn foo() {s::S.bar$0();}
s::S.bar$0();
}
", ",
r" r"
mod s { mod s {
@ -1469,10 +1467,7 @@ impl S {
} }
} }
} }
fn foo() { fn foo() {s::S.bar();}
s::S.bar();
}
", ",
) )
} }
@ -1480,7 +1475,7 @@ fn foo() {
#[test] #[test]
fn create_method_from_descendant_module() { fn create_method_from_descendant_module() {
check_assist( check_assist(
generate_method, generate_function,
r" r"
struct S; struct S;
mod s { mod s {
@ -1512,21 +1507,14 @@ fn bar(&self) ${0:-> ()} {
#[test] #[test]
fn create_method_with_cursor_anywhere_on_call_expresion() { fn create_method_with_cursor_anywhere_on_call_expresion() {
check_assist( check_assist(
generate_method, generate_function,
r" r"
struct S; struct S;
fn foo() {$0S.bar();}
fn foo() {
$0S.bar();
}
", ",
r" r"
struct S; struct S;
fn foo() {S.bar();}
fn foo() {
S.bar();
}
impl S { impl S {
@ -1534,7 +1522,6 @@ fn bar(&self) ${0:-> ()} {
todo!() todo!()
} }
} }
", ",
) )
} }

View file

@ -151,7 +151,6 @@ mod handlers {
generate_enum_projection_method::generate_enum_try_into_method, generate_enum_projection_method::generate_enum_try_into_method,
generate_from_impl_for_enum::generate_from_impl_for_enum, generate_from_impl_for_enum::generate_from_impl_for_enum,
generate_function::generate_function, generate_function::generate_function,
generate_function::generate_method,
generate_getter::generate_getter, generate_getter::generate_getter,
generate_getter::generate_getter_mut, generate_getter::generate_getter_mut,
generate_impl::generate_impl, generate_impl::generate_impl,