Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs')
-rw-r--r--crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs65
1 files changed, 34 insertions, 31 deletions
diff --git a/crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs b/crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs
index fccc04770e..0bb90f187c 100644
--- a/crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs
+++ b/crates/ide-assists/src/handlers/generate_blanket_trait_impl.rs
@@ -13,7 +13,7 @@ use syntax::{
AstNode,
ast::{
self, AssocItem, GenericParam, HasAttrs, HasGenericParams, HasName, HasTypeBounds,
- HasVisibility, edit::AstNodeEdit, make,
+ HasVisibility, edit::AstNodeEdit, syntax_factory::SyntaxFactory,
},
syntax_editor::Position,
};
@@ -73,24 +73,25 @@ pub(crate) fn generate_blanket_trait_impl(
"Generate blanket trait implementation",
name.syntax().text_range(),
|builder| {
- let mut edit = builder.make_editor(traitd.syntax());
- let namety = make::ty_path(make::path_from_text(&name.text()));
+ let editor = builder.make_editor(traitd.syntax());
+ let make = editor.make();
+ let namety = make.ty_path(make.path_from_text(&name.text()));
let trait_where_clause = traitd.where_clause().map(|it| it.reset_indent());
- let bounds = traitd.type_bound_list().and_then(exlucde_sized);
+ let bounds = traitd.type_bound_list().and_then(|list| exclude_sized(make, list));
let is_unsafe = traitd.unsafe_token().is_some();
- let thisname = this_name(&traitd);
- let thisty = make::ty_path(make::path_from_text(&thisname.text()));
+ let thisname = this_name(make, &traitd);
+ let thisty = make.ty_path(make.path_from_text(&thisname.text()));
let indent = traitd.indent_level();
- let gendecl = make::generic_param_list([GenericParam::TypeParam(make::type_param(
+ let gendecl = make.generic_param_list([GenericParam::TypeParam(make.type_param(
thisname.clone(),
- apply_sized(has_sized(&traitd, &ctx.sema), bounds),
+ apply_sized(make, has_sized(&traitd, &ctx.sema), bounds),
))]);
let trait_gen_args =
traitd.generic_param_list().map(|param_list| param_list.to_generic_args());
- let impl_ = make::impl_trait(
+ let impl_ = make.impl_trait(
cfg_attrs(&traitd),
is_unsafe,
traitd.generic_param_list(),
@@ -98,20 +99,19 @@ pub(crate) fn generate_blanket_trait_impl(
Some(gendecl),
None,
false,
- namety,
- thisty.clone(),
+ namety.into(),
+ thisty.into(),
trait_where_clause,
None,
None,
- )
- .clone_for_update();
+ );
if let Some(trait_assoc_list) = traitd.assoc_item_list() {
- let assoc_item_list = impl_.get_or_create_assoc_item_list();
+ let assoc_item_list = impl_.get_or_create_assoc_item_list_with_editor(&editor);
for item in trait_assoc_list.assoc_items() {
let item = match item {
ast::AssocItem::Fn(method) if method.body().is_none() => {
- todo_fn(&method, ctx.config).into()
+ todo_fn(make, &method, ctx.config).into()
}
ast::AssocItem::Const(_) | ast::AssocItem::TypeAlias(_) => item,
_ => continue,
@@ -122,10 +122,10 @@ pub(crate) fn generate_blanket_trait_impl(
let impl_ = impl_.indent(indent);
- edit.insert_all(
+ editor.insert_all(
Position::after(traitd.syntax()),
vec![
- make::tokens::whitespace(&format!("\n\n{indent}")).into(),
+ make.whitespace(&format!("\n\n{indent}")).into(),
impl_.syntax().clone().into(),
],
);
@@ -135,8 +135,7 @@ pub(crate) fn generate_blanket_trait_impl(
{
builder.add_tabstop_before(cap, self_ty);
}
-
- builder.add_file_edits(ctx.vfs_file_id(), edit);
+ builder.add_file_edits(ctx.vfs_file_id(), editor);
},
);
@@ -212,22 +211,26 @@ fn where_clause_sized(where_clause: Option<ast::WhereClause>) -> Option<bool> {
})
}
-fn apply_sized(has_sized: bool, bounds: Option<ast::TypeBoundList>) -> Option<ast::TypeBoundList> {
+fn apply_sized(
+ make: &SyntaxFactory,
+ has_sized: bool,
+ bounds: Option<ast::TypeBoundList>,
+) -> Option<ast::TypeBoundList> {
if has_sized {
return bounds;
}
let bounds = bounds
.into_iter()
.flat_map(|bounds| bounds.bounds())
- .chain([make::type_bound_text("?Sized")]);
- make::type_bound_list(bounds)
+ .chain([make.type_bound_text("?Sized")]);
+ make.type_bound_list(bounds)
}
-fn exlucde_sized(bounds: ast::TypeBoundList) -> Option<ast::TypeBoundList> {
- make::type_bound_list(bounds.bounds().filter(|bound| !ty_bound_is(bound, "Sized")))
+fn exclude_sized(make: &SyntaxFactory, bounds: ast::TypeBoundList) -> Option<ast::TypeBoundList> {
+ make.type_bound_list(bounds.bounds().filter(|bound| !ty_bound_is(bound, "Sized")))
}
-fn this_name(traitd: &ast::Trait) -> ast::Name {
+fn this_name(make: &SyntaxFactory, traitd: &ast::Trait) -> ast::Name {
let has_iter = find_bound("Iterator", traitd.type_bound_list()).is_some();
let params = traitd
@@ -245,7 +248,7 @@ fn this_name(traitd: &ast::Trait) -> ast::Name {
let mut name_gen =
suggest_name::NameGenerator::new_with_names(params.iter().map(String::as_str));
- make::name(&name_gen.suggest_name(if has_iter { "I" } else { "T" }))
+ make.name(&name_gen.suggest_name(if has_iter { "I" } else { "T" }))
}
fn find_bound(s: &str, bounds: Option<ast::TypeBoundList>) -> Option<ast::TypeBound> {
@@ -260,16 +263,16 @@ fn ty_bound_is(bound: &ast::TypeBound, s: &str) -> bool {
.is_some_and(|name| name.text() == s))
}
-fn todo_fn(f: &ast::Fn, config: &AssistConfig) -> ast::Fn {
- let params = f.param_list().unwrap_or_else(|| make::param_list(None, None));
- make::fn_(
+fn todo_fn(make: &SyntaxFactory, f: &ast::Fn, config: &AssistConfig) -> ast::Fn {
+ let params = f.param_list().unwrap_or_else(|| make.param_list(None, None));
+ make.fn_(
cfg_attrs(f),
f.visibility(),
- f.name().unwrap_or_else(|| make::name("unnamed")),
+ f.name().unwrap_or_else(|| make.name("unnamed")),
f.generic_param_list(),
f.where_clause(),
params,
- make::block_expr(None, Some(crate::utils::expr_fill_default(config))),
+ make.block_expr(None, Some(crate::utils::expr_fill_default(config))),
f.ret_type(),
f.async_token().is_some(),
f.const_token().is_some(),