internal: migrate extract_module to SyntaxEditor#21868
Conversation
There was a problem hiding this comment.
Please migrate remaining make::* usages to SyntaxFactory APIs
| continue; | ||
| } | ||
|
|
||
| let mut editor = SyntaxEditor::new(body_item.syntax().clone()); |
There was a problem hiding this comment.
Is this new SyntaxEditor instance and .finish() call on it necessary? Couldn't it be done by passing the mutable reference of existing one from the assist entrance function as a parameter of this function instead?
There was a problem hiding this comment.
the items here are detached clone_subtree() nodes, so the editor from the entry point can't reach them. I can use SyntaxFactory::without_mappings() locally to get rid of the .clone_for_update() calls instead, does that work?
There was a problem hiding this comment.
Hmm, yeah, I guess it could be still uplifted to the root SyntaxEditor but that would require more involved refactoring, so I think this is fine as is for now
| }) | ||
| .collect(); | ||
| if !replacements.is_empty() { | ||
| let mut editor = SyntaxEditor::new(entry.syntax().clone()); |
| @@ -18,8 +18,10 @@ use syntax::{ | |||
| self, HasVisibility, | |||
| edit::{AstNodeEdit, IndentLevel}, | |||
| make, | |||
There was a problem hiding this comment.
Kindly replace use of make with SyntaxFactory
| let assoc_item_list = make::assoc_item_list(Some(assoc_items)).clone_for_update(); | ||
| let impl_ = impl_.reset_indent(); | ||
| ted::replace(impl_.get_or_create_assoc_item_list().syntax(), assoc_item_list.syntax()); | ||
| let impl_detached = ast::Impl::cast(impl_.syntax().clone_subtree()).unwrap(); |
|
|
||
| let (mut replacements, record_field_parents, impls) = | ||
| get_replacements_for_visibility_change(&mut self.body_items, false); | ||
| get_replacements_for_visibility_change(&mut self.body_items, true); |
There was a problem hiding this comment.
this was a workaround because of the local SyntaxEditor approach, was an incorrect change
|
|
||
| let mut editor = SyntaxEditor::new(body_item.syntax().clone()); | ||
| for target in insert_targets { | ||
| let pub_crate_vis = make::visibility_pub_crate().clone_for_update(); |
| Some(( | ||
| seg.syntax().parent()?, | ||
| make::path_from_text(&format!("{mod_name}::{seg}")) | ||
| .clone_for_update(), |
There was a problem hiding this comment.
i missed that this should use SyntaxFactory, will replace it
|
@itang06 What's the status of this? |
i addressed the unwrap issue. waiting for ShoyuVanilla's response to my question about using |
11ff3da to
fbf2dfa
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
fbf2dfa to
a9d2246
Compare
This comment has been minimized.
This comment has been minimized.
| .entry(use_.syntax().text_range().start()) | ||
| .or_insert_with(|| use_.clone_subtree().clone_for_update()); | ||
| for seg in use_ | ||
| .or_insert_with(|| use_.clone_subtree()); |
There was a problem hiding this comment.
remove clone_subtree
| let assoc_item_list = make::assoc_item_list(Some(assoc_items)).clone_for_update(); | ||
| let impl_ = impl_.reset_indent(); | ||
| ted::replace(impl_.get_or_create_assoc_item_list().syntax(), assoc_item_list.syntax()); | ||
| let impl_detached = ast::Impl::cast(impl_.syntax().clone_subtree()).unwrap(); |
| ], | ||
| ); | ||
| } | ||
| *body_item = ast::Item::cast(editor.finish().new_root().clone_subtree()).unwrap(); |
There was a problem hiding this comment.
| *body_item = ast::Item::cast(editor.finish().new_root().clone_subtree()).unwrap(); | |
| *body_item = ast::Item::cast(editor.finish().new_root()).unwrap(); |
| for item in &mut self.body_items { | ||
| *item = ast::Item::cast(item.reset_indent().syntax().clone_subtree()).unwrap(); | ||
| } |
There was a problem hiding this comment.
No need to call clone_subtree here anymore. SyntaxEditor manages this internally
There was a problem hiding this comment.
i tried removing the clone_subtree() calls but found that 3 of the 4 calls require using the detached root returned by SyntaxEditor::new() instead of just dropping clone_subtree(). i think the 4th at line 436 has to stay because replacements is collected from body_items immediately after. is there something i'm missing or is there a different approach you had in mind?
|
@itang06 What's the status of this? |
waiting on feedback from Shourya742 regarding the approach for removing |
|
Hey @itang06, can you rebase the changes onto the latest |
|
Can you also please squash the commits, seems like most are changing the same file. |
|
Hey @itang06 are you still working on this? |
|
Yes, still working on it. i'll have an update soon. |
|
Make sure to remove/update ted variant of |
74c1a1e to
de6c326
Compare
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
|
@Shourya742 i found that after rebasing. removing the |
de6c326 to
7b0b9c7
Compare
Yes |
f828748 to
5b44119
Compare
| _ => true, | ||
| } | ||
| }); | ||
| let make = SyntaxFactory::without_mappings(); |
There was a problem hiding this comment.
You don't need to initialize the without_mappining here, you can get it from the editor and have mapped changes stored in the factory.
There was a problem hiding this comment.
done, using editor.make() in a scoped block so it's dropped before editor.finish()
| let (_, root) = SyntaxEditor::with_ast_node(&use_); | ||
| root | ||
| }); | ||
| let make = SyntaxFactory::without_mappings(); |
There was a problem hiding this comment.
We don't need this
There was a problem hiding this comment.
we can use the make via the editor
There was a problem hiding this comment.
done, simplified to use_.clone()
| @@ -741,7 +788,6 @@ | |||
|
|
|||
| fn get_replacements_for_visibility_change( | |||
| items: &mut [ast::Item], | |||
There was a problem hiding this comment.
Since we are not longer mutating it.
| items: &mut [ast::Item], | |
| items: &[ast::Item], |
|
|
||
| let (mut replacements, record_field_parents, impls) = | ||
| get_replacements_for_visibility_change(&mut self.body_items, false); | ||
| get_replacements_for_visibility_change(&mut self.body_items); |
There was a problem hiding this comment.
We dont need mut here now.
|
|
||
| let (mut impl_item_replacements, _, _) = | ||
| get_replacements_for_visibility_change(&mut impl_items, true); | ||
| get_replacements_for_visibility_change(&mut impl_items); |
There was a problem hiding this comment.
We don't need mut here now.
| @@ -186,6 +187,7 @@ | |||
| parent_impl: &Option<ast::Impl>, | |||
| Module { name, body_items, use_items }: &Module, | |||
| ) -> ast::Module { | |||
| let make = SyntaxFactory::without_mappings(); | |||
There was a problem hiding this comment.
We can pass factory to generate_module_def method from extract_module
There was a problem hiding this comment.
done. also did the same for make_use_stmt_of_node_with_super and change_visibility which had the same issue
| imports_to_remove | ||
| } | ||
|
|
||
| fn process_def_in_sel( |
There was a problem hiding this comment.
We can pass factory to process_def_in_sel
There was a problem hiding this comment.
done, threaded it through resolve_imports
5b44119 to
563cdc6
Compare
|
Pinging @ChayimFriedman2, for a final look. |
Part of #18285
Migrates all
ted::calls incrates/ide-assists/src/handlers/extract_module.rsto the newSyntaxEditor/SyntaxFactoryabstractionChanges:
generate_module_def: replacedted::withSyntaxEditor/SyntaxFactoryexpand_and_group_usages_file_wise: replacedted::replacewithSyntaxEditorchange_visibility: replacedted::insert(viaadd_change_vishelper) withSyntaxEditor::insert_all; removes theadd_change_visfunction entirelyAI assistance was used for this contribution