Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/utils/gen_trait_fn_body.rs')
| -rw-r--r-- | crates/ide-assists/src/utils/gen_trait_fn_body.rs | 62 |
1 files changed, 35 insertions, 27 deletions
diff --git a/crates/ide-assists/src/utils/gen_trait_fn_body.rs b/crates/ide-assists/src/utils/gen_trait_fn_body.rs index 7a9bdfe1ec..4ea56dc46a 100644 --- a/crates/ide-assists/src/utils/gen_trait_fn_body.rs +++ b/crates/ide-assists/src/utils/gen_trait_fn_body.rs @@ -2,7 +2,7 @@ use hir::TraitRef; use syntax::{ - ast::{self, edit::AstNodeEdit, make, AstNode, BinaryOp, CmpOp, HasName, LogicOp}, + ast::{self, AstNode, BinaryOp, CmpOp, HasName, LogicOp, edit::AstNodeEdit, make}, ted, }; @@ -35,7 +35,7 @@ fn gen_clone_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { stdx::always!(func.name().is_some_and(|name| name.text() == "clone")); fn gen_clone_call(target: ast::Expr) -> ast::Expr { let method = make::name_ref("clone"); - make::expr_method_call(target, method, make::arg_list(None)) + make::expr_method_call(target, method, make::arg_list(None)).into() } let expr = match adt { // `Clone` cannot be derived for unions, so no default impl can be provided. @@ -83,7 +83,8 @@ fn gen_clone_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { } let pat = make::tuple_struct_pat(variant_name.clone(), pats.into_iter()); let struct_name = make::expr_path(variant_name); - let tuple_expr = make::expr_call(struct_name, make::arg_list(fields)); + let tuple_expr = + make::expr_call(struct_name, make::arg_list(fields)).into(); arms.push(make::match_arm(pat.into(), None, tuple_expr)); } @@ -126,7 +127,7 @@ fn gen_clone_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { fields.push(gen_clone_call(target)); } let struct_name = make::expr_path(make::ext::ident_path("Self")); - make::expr_call(struct_name, make::arg_list(fields)) + make::expr_call(struct_name, make::arg_list(fields)).into() } // => Self { } None => { @@ -165,7 +166,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let method = make::name_ref("debug_struct"); let struct_name = format!("\"{name}\""); let args = make::arg_list(Some(make::expr_literal(&struct_name).into())); - let mut expr = make::expr_method_call(target, method, args); + let mut expr = make::expr_method_call(target, method, args).into(); let mut pats = vec![]; for field in list.fields() { @@ -181,12 +182,13 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let path = &format!("{field_name}"); let path = make::expr_path(make::ext::ident_path(path)); let args = make::arg_list(vec![name, path]); - expr = make::expr_method_call(expr, method_name, args); + expr = make::expr_method_call(expr, method_name, args).into(); } // => <expr>.finish() let method = make::name_ref("finish"); - let expr = make::expr_method_call(expr, method, make::arg_list(None)); + let expr = + make::expr_method_call(expr, method, make::arg_list(None)).into(); // => MyStruct { fields.. } => f.debug_struct("MyStruct")...finish(), let pat = make::record_pat(variant_name.clone(), pats.into_iter()); @@ -198,7 +200,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let method = make::name_ref("debug_tuple"); let struct_name = format!("\"{name}\""); let args = make::arg_list(Some(make::expr_literal(&struct_name).into())); - let mut expr = make::expr_method_call(target, method, args); + let mut expr = make::expr_method_call(target, method, args).into(); let mut pats = vec![]; for (i, _) in list.fields().enumerate() { @@ -214,12 +216,13 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let field_path = &name.to_string(); let field_path = make::expr_path(make::ext::ident_path(field_path)); let args = make::arg_list(vec![field_path]); - expr = make::expr_method_call(expr, method_name, args); + expr = make::expr_method_call(expr, method_name, args).into(); } // => <expr>.finish() let method = make::name_ref("finish"); - let expr = make::expr_method_call(expr, method, make::arg_list(None)); + let expr = + make::expr_method_call(expr, method, make::arg_list(None)).into(); // => MyStruct (fields..) => f.debug_tuple("MyStruct")...finish(), let pat = make::tuple_struct_pat(variant_name.clone(), pats.into_iter()); @@ -227,12 +230,14 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { } None => { let fmt_string = make::expr_literal(&(format!("\"{name}\""))).into(); - let args = make::arg_list([target, fmt_string]); - let macro_name = make::expr_path(make::ext::ident_path("write")); - let macro_call = make::expr_macro_call(macro_name, args); + let args = make::ext::token_tree_from_node( + make::arg_list([target, fmt_string]).syntax(), + ); + let macro_name = make::ext::ident_path("write"); + let macro_call = make::expr_macro(macro_name, args); let variant_name = make::path_pat(variant_name); - arms.push(make::match_arm(variant_name, None, macro_call)); + arms.push(make::match_arm(variant_name, None, macro_call.into())); } } } @@ -254,12 +259,12 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let expr = match strukt.field_list() { // => f.debug_struct("Name").finish() - None => make::expr_method_call(target, make::name_ref("debug_struct"), args), + None => make::expr_method_call(target, make::name_ref("debug_struct"), args).into(), // => f.debug_struct("Name").field("foo", &self.foo).finish() Some(ast::FieldList::RecordFieldList(field_list)) => { let method = make::name_ref("debug_struct"); - let mut expr = make::expr_method_call(target, method, args); + let mut expr = make::expr_method_call(target, method, args).into(); for field in field_list.fields() { let name = field.name()?; let f_name = make::expr_literal(&(format!("\"{name}\""))).into(); @@ -267,7 +272,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let f_path = make::expr_ref(f_path, false); let f_path = make::expr_field(f_path, &format!("{name}")); let args = make::arg_list([f_name, f_path]); - expr = make::expr_method_call(expr, make::name_ref("field"), args); + expr = make::expr_method_call(expr, make::name_ref("field"), args).into(); } expr } @@ -275,20 +280,21 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { // => f.debug_tuple("Name").field(self.0).finish() Some(ast::FieldList::TupleFieldList(field_list)) => { let method = make::name_ref("debug_tuple"); - let mut expr = make::expr_method_call(target, method, args); + let mut expr = make::expr_method_call(target, method, args).into(); for (i, _) in field_list.fields().enumerate() { let f_path = make::expr_path(make::ext::ident_path("self")); let f_path = make::expr_ref(f_path, false); let f_path = make::expr_field(f_path, &format!("{i}")); let method = make::name_ref("field"); - expr = make::expr_method_call(expr, method, make::arg_list(Some(f_path))); + expr = make::expr_method_call(expr, method, make::arg_list(Some(f_path))) + .into(); } expr } }; let method = make::name_ref("finish"); - let expr = make::expr_method_call(expr, method, make::arg_list(None)); + let expr = make::expr_method_call(expr, method, make::arg_list(None)).into(); let body = make::block_expr(None, Some(expr)).indent(ast::edit::IndentLevel(1)); ted::replace(func.body()?.syntax(), body.clone_for_update().syntax()); Some(()) @@ -300,7 +306,7 @@ fn gen_debug_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { fn gen_default_call() -> Option<ast::Expr> { let fn_name = make::ext::path_from_idents(["Default", "default"])?; - Some(make::expr_call(make::expr_path(fn_name), make::arg_list(None))) + Some(make::expr_call(make::expr_path(fn_name), make::arg_list(None)).into()) } match adt { // `Debug` cannot be derived for unions, so no default impl can be provided. @@ -327,7 +333,7 @@ fn gen_default_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { .fields() .map(|_| gen_default_call()) .collect::<Option<Vec<ast::Expr>>>()?; - make::expr_call(struct_name, make::arg_list(fields)) + make::expr_call(struct_name, make::arg_list(fields)).into() } None => { let struct_name = make::ext::ident_path("Self"); @@ -348,7 +354,7 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { fn gen_hash_call(target: ast::Expr) -> ast::Stmt { let method = make::name_ref("hash"); let arg = make::expr_path(make::ext::ident_path("state")); - let expr = make::expr_method_call(target, method, make::arg_list(Some(arg))); + let expr = make::expr_method_call(target, method, make::arg_list(Some(arg))).into(); make::expr_stmt(expr).into() } @@ -361,7 +367,7 @@ fn gen_hash_impl(adt: &ast::Adt, func: &ast::Fn) -> Option<()> { let fn_name = make_discriminant()?; let arg = make::expr_path(make::ext::ident_path("self")); - let fn_call = make::expr_call(fn_name, make::arg_list(Some(arg))); + let fn_call = make::expr_call(fn_name, make::arg_list(Some(arg))).into(); let stmt = gen_hash_call(fn_call); make::block_expr(Some(stmt), None).indent(ast::edit::IndentLevel(1)) @@ -444,9 +450,11 @@ fn gen_partial_eq(adt: &ast::Adt, func: &ast::Fn, trait_ref: Option<TraitRef>) - ast::Adt::Enum(enum_) => { // => std::mem::discriminant(self) == std::mem::discriminant(other) let lhs_name = make::expr_path(make::ext::ident_path("self")); - let lhs = make::expr_call(make_discriminant()?, make::arg_list(Some(lhs_name.clone()))); + let lhs = make::expr_call(make_discriminant()?, make::arg_list(Some(lhs_name.clone()))) + .into(); let rhs_name = make::expr_path(make::ext::ident_path("other")); - let rhs = make::expr_call(make_discriminant()?, make::arg_list(Some(rhs_name.clone()))); + let rhs = make::expr_call(make_discriminant()?, make::arg_list(Some(rhs_name.clone()))) + .into(); let eq_check = make::expr_bin_op(lhs, BinaryOp::CmpOp(CmpOp::Eq { negated: false }), rhs); @@ -613,7 +621,7 @@ fn gen_partial_ord(adt: &ast::Adt, func: &ast::Fn, trait_ref: Option<TraitRef>) fn gen_partial_cmp_call(lhs: ast::Expr, rhs: ast::Expr) -> ast::Expr { let rhs = make::expr_ref(rhs, false); let method = make::name_ref("partial_cmp"); - make::expr_method_call(lhs, method, make::arg_list(Some(rhs))) + make::expr_method_call(lhs, method, make::arg_list(Some(rhs))).into() } // Check that self type and rhs type match. We don't know how to implement the method |