Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 2eb5064

Browse files
committed
refactor: simplify generate_delegate_method
Can actually split out adding the functions from getting the impl to update or create thanks to being able to refer to the impl ast node. FIXME Context: Unfortunately we can't adjust the indentation of the newly added function inside of `ast::AssocItemList::add_item` since for some reason the `todo!()` placeholder generated by `add_missing_impl_members` and `replace_derive_with_manual_impl` gets indented weirdly.
1 parent 09a3bd5 commit 2eb5064

File tree

1 file changed

+15
-31
lines changed

1 file changed

+15
-31
lines changed

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,29 +141,10 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
141141
)
142142
.clone_for_update();
143143

144-
// Create or update an impl block, attach the function to it,
145-
// then insert into our code.
146-
match impl_def {
147-
Some(impl_def) => {
148-
// Remember where in our source our `impl` block lives.
149-
let impl_def = edit.make_mut(impl_def);
150-
151-
// Fixup function indentation.
152-
f.reindent_to(impl_def.indent_level() + 1);
153-
154-
// Attach the function to the impl block.
155-
let assoc_items = impl_def.get_or_create_assoc_item_list();
156-
assoc_items.add_item(f.clone().into());
157-
158-
// Update the impl block.
159-
ted::replace(impl_def.syntax(), impl_def.syntax());
160-
161-
if let Some(cap) = ctx.config.snippet_cap {
162-
edit.add_tabstop_before(cap, f);
163-
}
164-
}
144+
// Get the impl to update, or create one if we need to.
145+
let impl_def = match impl_def {
146+
Some(impl_def) => edit.make_mut(impl_def),
165147
None => {
166-
// Attach the function to the impl block
167148
let name = &strukt_name.to_string();
168149
let params = strukt.generic_param_list();
169150
let ty_params = params.clone();
@@ -178,12 +159,6 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
178159
)
179160
.clone_for_update();
180161

181-
// Fixup function indentation.
182-
f.reindent_to(impl_def.indent_level() + 1);
183-
184-
let assoc_items = impl_def.get_or_create_assoc_item_list();
185-
assoc_items.add_item(f.clone().into());
186-
187162
// Fixup impl_def indentation
188163
let indent = strukt.indent_level();
189164
impl_def.reindent_to(indent);
@@ -198,10 +173,19 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
198173
],
199174
);
200175

201-
if let Some(cap) = ctx.config.snippet_cap {
202-
edit.add_tabstop_before(cap, f)
203-
}
176+
impl_def
204177
}
178+
};
179+
180+
// Fixup function indentation.
181+
// FIXME: Should really be handled by `AssocItemList::add_item`
182+
f.reindent_to(impl_def.indent_level() + 1);
183+
184+
let assoc_items = impl_def.get_or_create_assoc_item_list();
185+
assoc_items.add_item(f.clone().into());
186+
187+
if let Some(cap) = ctx.config.snippet_cap {
188+
edit.add_tabstop_before(cap, f)
205189
}
206190
},
207191
)?;

0 commit comments

Comments
 (0)