Unnamed repository; edit this file 'description' to name the repository.
make changes to add_missing_impl_members to update changes to utils
| -rw-r--r-- | crates/ide-assists/src/handlers/add_missing_impl_members.rs | 24 | ||||
| -rw-r--r-- | crates/ide-assists/src/utils/gen_trait_fn_body.rs | 37 |
2 files changed, 29 insertions, 32 deletions
diff --git a/crates/ide-assists/src/handlers/add_missing_impl_members.rs b/crates/ide-assists/src/handlers/add_missing_impl_members.rs index afdced4215..3689dc24b3 100644 --- a/crates/ide-assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ide-assists/src/handlers/add_missing_impl_members.rs @@ -1,7 +1,7 @@ use hir::HasSource; use syntax::{ Edition, - ast::{self, AstNode, make}, + ast::{self, AstNode, syntax_factory::SyntaxFactory}, syntax_editor::{Position, SyntaxEditor}, }; @@ -9,8 +9,8 @@ use crate::{ AssistId, assist_context::{AssistContext, Assists}, utils::{ - DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl, filter_assoc_items, - gen_trait_fn_body, + DefaultMethods, IgnoreAssocItems, add_trait_assoc_items_to_impl_with_factory, + filter_assoc_items, gen_trait_fn_body, }, }; @@ -148,7 +148,9 @@ fn add_missing_impl_members_inner( let target = impl_def.syntax().text_range(); acc.add(AssistId::quick_fix(assist_id), label, target, |edit| { - let new_item = add_trait_assoc_items_to_impl( + let make = SyntaxFactory::with_mappings(); + let new_item = add_trait_assoc_items_to_impl_with_factory( + &make, &ctx.sema, ctx.config, &missing_items, @@ -164,6 +166,7 @@ fn add_missing_impl_members_inner( let mut first_new_item = if let DefaultMethods::No = mode && let ast::AssocItem::Fn(func) = &first_new_item && let Some(body) = try_gen_trait_body( + &make, ctx, func, trait_ref, @@ -189,10 +192,10 @@ fn add_missing_impl_members_inner( if let Some(assoc_item_list) = impl_def.assoc_item_list() { assoc_item_list.add_items(&mut editor, new_assoc_items); } else { - let assoc_item_list = make::assoc_item_list(Some(new_assoc_items)).clone_for_update(); + let assoc_item_list = make.assoc_item_list(new_assoc_items); editor.insert_all( Position::after(impl_def.syntax()), - vec![make::tokens::whitespace(" ").into(), assoc_item_list.syntax().clone().into()], + vec![make.whitespace(" ").into(), assoc_item_list.syntax().clone().into()], ); first_new_item = assoc_item_list.assoc_items().next(); } @@ -215,23 +218,24 @@ fn add_missing_impl_members_inner( editor.add_annotation(first_new_item.syntax(), tabstop); }; }; + editor.add_mappings(make.finish_with_mappings()); edit.add_file_edits(ctx.vfs_file_id(), editor); }) } fn try_gen_trait_body( + make: &SyntaxFactory, ctx: &AssistContext<'_>, func: &ast::Fn, trait_ref: hir::TraitRef<'_>, impl_def: &ast::Impl, edition: Edition, ) -> Option<ast::BlockExpr> { - let trait_path = make::ext::ident_path( - &trait_ref.trait_().name(ctx.db()).display(ctx.db(), edition).to_string(), - ); + let trait_path = + make.ident_path(&trait_ref.trait_().name(ctx.db()).display(ctx.db(), edition).to_string()); let hir_ty = ctx.sema.resolve_type(&impl_def.self_ty()?)?; let adt = hir_ty.as_adt()?.source(ctx.db())?; - gen_trait_fn_body(func, &trait_path, &adt.value, Some(trait_ref)) + gen_trait_fn_body(make, func, &trait_path, &adt.value, Some(trait_ref)) } #[cfg(test)] 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 f59e48a04f..b0d88737fe 100644 --- a/crates/ide-assists/src/utils/gen_trait_fn_body.rs +++ b/crates/ide-assists/src/utils/gen_trait_fn_body.rs @@ -1,7 +1,10 @@ //! This module contains functions to generate default trait impl function bodies where possible. use hir::TraitRef; -use syntax::ast::{self, AstNode, BinaryOp, CmpOp, HasName, LogicOp, edit::AstNodeEdit, syntax_factory::SyntaxFactory}; +use syntax::ast::{ + self, AstNode, BinaryOp, CmpOp, HasName, LogicOp, edit::AstNodeEdit, + syntax_factory::SyntaxFactory, +}; /// Generate custom trait bodies without default implementation where possible. /// @@ -94,8 +97,7 @@ fn gen_clone_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr } 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)).into(); + let tuple_expr = make.expr_call(struct_name, make.arg_list(fields)).into(); arms.push(make.match_arm(pat.into(), None, tuple_expr)); } @@ -197,8 +199,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr // => <expr>.finish() let method = make.name_ref("finish"); - let expr = - make.expr_method_call(expr, method, make.arg_list([])).into(); + let expr = make.expr_method_call(expr, method, make.arg_list([])).into(); // => MyStruct { fields.. } => f.debug_struct("MyStruct")...finish(), let pat_field_list = make.record_pat_field_list(pats, None); @@ -232,19 +233,16 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr // => <expr>.finish() let method = make.name_ref("finish"); - let expr= - make.expr_method_call(expr, method, make.arg_list([])).into(); + let expr = make.expr_method_call(expr, method, make.arg_list([])).into(); // => MyStruct (fields..) => f.debug_tuple("MyStruct")...finish(), let pat = make.tuple_struct_pat(variant_name.clone(), pats.into_iter()); arms.push(make.match_arm(pat.into(), None, expr)); } None => { - let fmt_string = - make.expr_literal(&(format!("\"{name}\""))).into(); - let args = make.token_tree_from_node( - make.arg_list([target, fmt_string]).syntax(), - ); + let fmt_string = make.expr_literal(&(format!("\"{name}\""))).into(); + let args = + make.token_tree_from_node(make.arg_list([target, fmt_string]).syntax()); let macro_name = make.ident_path("write"); let macro_call = make.expr_macro(macro_name, args); @@ -278,8 +276,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr 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(); + let f_name = make.expr_literal(&(format!("\"{name}\""))).into(); let f_path = make.expr_path(make.ident_path("self")); let f_path = make.expr_field(f_path, &format!("{name}")).into(); let f_path = make.expr_ref(f_path, false); @@ -298,9 +295,7 @@ fn gen_debug_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr let f_path = make.expr_field(f_path, &format!("{i}")).into(); let f_path = make.expr_ref(f_path, false); let method = make.name_ref("field"); - expr = make - .expr_method_call(expr, method, make.arg_list([f_path])) - .into(); + expr = make.expr_method_call(expr, method, make.arg_list([f_path])).into(); } expr } @@ -354,9 +349,8 @@ fn gen_default_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockEx make.record_expr(struct_name, fields).into() } }; - let body = make - .block_expr(None::<ast::Stmt>, Some(expr)) - .indent(ast::edit::IndentLevel(1)); + let body = + make.block_expr(None::<ast::Stmt>, Some(expr)).indent(ast::edit::IndentLevel(1)); Some(body) } } @@ -391,8 +385,7 @@ fn gen_hash_impl(make: &SyntaxFactory, adt: &ast::Adt) -> Option<ast::BlockExpr> let mut stmts = vec![]; for field in field_list.fields() { let base = make.expr_path(make.ident_path("self")); - let target = - make.expr_field(base, &field.name()?.to_string()).into(); + let target = make.expr_field(base, &field.name()?.to_string()).into(); stmts.push(gen_hash_call(target)); } make.block_expr(stmts, None).indent(ast::edit::IndentLevel(1)) |