Skip to content

Commit 1e71ac2

Browse files
committed
extract_module: Resolve imports lazily
1 parent 031bdf2 commit 1e71ac2

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

crates/ide_assists/src/handlers/extract_module.rs

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -81,38 +81,34 @@ pub(crate) fn extract_module(acc: &mut Assists, ctx: &AssistContext) -> Option<(
8181
}
8282

8383
let mut module = extract_target(&node, ctx.selection_trimmed())?;
84-
if module.body_items.len() == 0 {
84+
if module.body_items.is_empty() {
8585
return None;
8686
}
8787

8888
let old_item_indent = module.body_items[0].indent_level();
8989

90-
//This takes place in three steps:
91-
//
92-
//- Firstly, we will update the references(usages) e.g. converting a
93-
// function call bar() to modname::bar(), and similarly for other items
94-
//
95-
//- Secondly, changing the visibility of each item inside the newly selected module
96-
// i.e. making a fn a() {} to pub(crate) fn a() {}
97-
//
98-
//- Thirdly, resolving all the imports this includes removing paths from imports
99-
// outside the module, shifting/cloning them inside new module, or shifting the imports, or making
100-
// new import statemnts
101-
102-
//We are getting item usages and record_fields together, record_fields
103-
//for change_visibility and usages for first point mentioned above in the process
104-
let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
105-
106-
let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, ctx);
107-
if module.body_items.len() == 0 {
108-
return None;
109-
}
110-
11190
acc.add(
11291
AssistId("extract_module", AssistKind::RefactorExtract),
11392
"Extract Module",
11493
module.text_range,
11594
|builder| {
95+
//This takes place in three steps:
96+
//
97+
//- Firstly, we will update the references(usages) e.g. converting a
98+
// function call bar() to modname::bar(), and similarly for other items
99+
//
100+
//- Secondly, changing the visibility of each item inside the newly selected module
101+
// i.e. making a fn a() {} to pub(crate) fn a() {}
102+
//
103+
//- Thirdly, resolving all the imports this includes removing paths from imports
104+
// outside the module, shifting/cloning them inside new module, or shifting the imports, or making
105+
// new import statemnts
106+
107+
//We are getting item usages and record_fields together, record_fields
108+
//for change_visibility and usages for first point mentioned above in the process
109+
let (usages_to_be_processed, record_fields) = module.get_usages_and_record_fields(ctx);
110+
111+
let import_paths_to_be_removed = module.resolve_imports(curr_parent_module, ctx);
116112
module.change_visibility(record_fields);
117113

118114
let mut body_items: Vec<String> = Vec::new();
@@ -221,19 +217,13 @@ fn extract_target(node: &SyntaxNode, selection_range: TextRange) -> Option<Modul
221217

222218
let mut body_items: Vec<ast::Item> = node
223219
.children()
224-
.filter_map(|child| {
225-
if selection_range.contains_range(child.text_range()) {
226-
let child_kind = child.kind();
227-
if let Some(item) = ast::Item::cast(child) {
228-
if ast::Use::can_cast(child_kind) {
229-
use_items.push(item);
230-
} else {
231-
return Some(item);
232-
}
233-
}
234-
return None;
220+
.filter(|child| selection_range.contains_range(child.text_range()))
221+
.filter_map(|child| match ast::Item::cast(child) {
222+
Some(it @ ast::Item::Use(_)) => {
223+
use_items.push(it);
224+
None
235225
}
236-
None
226+
item => item,
237227
})
238228
.collect();
239229

@@ -368,9 +358,7 @@ impl Module {
368358
source_file: &SourceFile,
369359
FileReference { range, name, .. }: FileReference,
370360
) -> Option<(TextRange, String)> {
371-
let path: Option<ast::Path> = find_node_at_range(source_file.syntax(), range);
372-
373-
let path = path?;
361+
let path: ast::Path = find_node_at_range(source_file.syntax(), range)?;
374362

375363
for desc in path.syntax().descendants() {
376364
if desc.to_string() == name.syntax().to_string()
@@ -609,9 +597,8 @@ impl Module {
609597

610598
let use_ =
611599
make::use_(None, make::use_tree(make::join_paths(use_tree_str), None, None, false));
612-
if let Some(item) = ast::Item::cast(use_.syntax().clone()) {
613-
self.use_items.insert(0, item);
614-
}
600+
let item = ast::Item::from(use_);
601+
self.use_items.insert(0, item);
615602
}
616603

617604
import_path_to_be_removed
@@ -825,7 +812,6 @@ fn get_replacements_for_visibilty_change(
825812
let mut impls = Vec::new();
826813

827814
items.into_iter().for_each(|item| {
828-
let item = item;
829815
if !is_clone_for_updated {
830816
*item = item.clone_for_update();
831817
}

0 commit comments

Comments
 (0)