Clean up extract_module

This commit is contained in:
iDawer 2022-03-28 16:31:18 +05:00
parent 9093941ed0
commit c2d12906b0

View file

@ -104,7 +104,7 @@ pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<(
let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx); let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, ctx); let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, ctx);
module.body_items = module.change_visibility(record_fields)?; module.body_items = module.change_visibility(record_fields);
if module.body_items.len() == 0 { if module.body_items.len() == 0 {
return None; return None;
} }
@ -114,6 +114,8 @@ pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<(
"Extract Module", "Extract Module",
module.text_range, module.text_range,
|builder| { |builder| {
module.body_items = module.change_visibility(record_fields);
let mut body_items: Vec<String> = Vec::new(); let mut body_items: Vec<String> = Vec::new();
let mut items_to_be_processed: Vec<ast::Item> = module.body_items.clone(); let mut items_to_be_processed: Vec<ast::Item> = module.body_items.clone();
let mut new_item_indent = old_item_indent + 1; let mut new_item_indent = old_item_indent + 1;
@ -393,7 +395,7 @@ impl Module {
None None
} }
fn change_visibility(&self, record_fields: Vec<SyntaxNode>) -> Option<Vec<ast::Item>> { fn change_visibility(&self, record_fields: Vec<SyntaxNode>) -> Vec<ast::Item> {
let (body_items, mut replacements, record_field_parents, impls) = let (body_items, mut replacements, record_field_parents, impls) =
get_replacements_for_visibilty_change(self.body_items.clone(), false); get_replacements_for_visibilty_change(self.body_items.clone(), false);
@ -428,7 +430,7 @@ impl Module {
add_change_vis(vis, syntax.first_child_or_token()); add_change_vis(vis, syntax.first_child_or_token());
}); });
Some(body_items) body_items
} }
fn resolve_imports( fn resolve_imports(
@ -890,23 +892,13 @@ fn get_use_tree_paths_from_path(
Some(use_tree_str) Some(use_tree_str)
} }
fn add_change_vis( fn add_change_vis(vis: Option<ast::Visibility>, node_or_token_opt: Option<syntax::SyntaxElement>) {
vis: Option<ast::Visibility>, if vis.is_none() {
node_or_token_opt: Option<syntax::SyntaxElement>,
) -> Option<()> {
if let None = vis {
if let Some(node_or_token) = node_or_token_opt { if let Some(node_or_token) = node_or_token_opt {
let pub_crate_vis = make::visibility_pub_crate().clone_for_update(); let pub_crate_vis = make::visibility_pub_crate().clone_for_update();
if let Some(node) = node_or_token.as_node() { ted::insert(ted::Position::before(node_or_token), pub_crate_vis.syntax());
ted::insert(ted::Position::before(node), pub_crate_vis.syntax());
}
if let Some(token) = node_or_token.as_token() {
ted::insert(ted::Position::before(token), pub_crate_vis.syntax());
}
} }
} }
Some(())
} }
fn compare_hir_and_ast_module( fn compare_hir_and_ast_module(