about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDropDemBits <r3usrlnd@gmail.com>2023-07-06 22:31:28 -0400
committerDropDemBits <r3usrlnd@gmail.com>2023-07-07 20:08:32 -0400
commit2eb506462ef2baa7e6e63c0ef1ae7af1a998c750 (patch)
tree229b8146c1acff2a5dc72170378aaf5738ef5a1d
parent09a3bd58995079111dd59c0bd677521cceb28581 (diff)
downloadrust-2eb506462ef2baa7e6e63c0ef1ae7af1a998c750.tar.gz
rust-2eb506462ef2baa7e6e63c0ef1ae7af1a998c750.zip
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.
-rw-r--r--crates/ide-assists/src/handlers/generate_delegate_methods.rs46
1 files changed, 15 insertions, 31 deletions
diff --git a/crates/ide-assists/src/handlers/generate_delegate_methods.rs b/crates/ide-assists/src/handlers/generate_delegate_methods.rs
index f0b9b01672a..31fc69562c9 100644
--- a/crates/ide-assists/src/handlers/generate_delegate_methods.rs
+++ b/crates/ide-assists/src/handlers/generate_delegate_methods.rs
@@ -141,29 +141,10 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
                 )
                 .clone_for_update();
 
-                // Create or update an impl block, attach the function to it,
-                // then insert into our code.
-                match impl_def {
-                    Some(impl_def) => {
-                        // Remember where in our source our `impl` block lives.
-                        let impl_def = edit.make_mut(impl_def);
-
-                        // Fixup function indentation.
-                        f.reindent_to(impl_def.indent_level() + 1);
-
-                        // Attach the function to the impl block.
-                        let assoc_items = impl_def.get_or_create_assoc_item_list();
-                        assoc_items.add_item(f.clone().into());
-
-                        // Update the impl block.
-                        ted::replace(impl_def.syntax(), impl_def.syntax());
-
-                        if let Some(cap) = ctx.config.snippet_cap {
-                            edit.add_tabstop_before(cap, f);
-                        }
-                    }
+                // Get the impl to update, or create one if we need to.
+                let impl_def = match impl_def {
+                    Some(impl_def) => edit.make_mut(impl_def),
                     None => {
-                        // Attach the function to the impl block
                         let name = &strukt_name.to_string();
                         let params = strukt.generic_param_list();
                         let ty_params = params.clone();
@@ -178,12 +159,6 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
                         )
                         .clone_for_update();
 
-                        // Fixup function indentation.
-                        f.reindent_to(impl_def.indent_level() + 1);
-
-                        let assoc_items = impl_def.get_or_create_assoc_item_list();
-                        assoc_items.add_item(f.clone().into());
-
                         // Fixup impl_def indentation
                         let indent = strukt.indent_level();
                         impl_def.reindent_to(indent);
@@ -198,10 +173,19 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
                             ],
                         );
 
-                        if let Some(cap) = ctx.config.snippet_cap {
-                            edit.add_tabstop_before(cap, f)
-                        }
+                        impl_def
                     }
+                };
+
+                // Fixup function indentation.
+                // FIXME: Should really be handled by `AssocItemList::add_item`
+                f.reindent_to(impl_def.indent_level() + 1);
+
+                let assoc_items = impl_def.get_or_create_assoc_item_list();
+                assoc_items.add_item(f.clone().into());
+
+                if let Some(cap) = ctx.config.snippet_cap {
+                    edit.add_tabstop_before(cap, f)
                 }
             },
         )?;