Skip to content

Commit 4180565

Browse files
committed
fix: donot generate redundant use stmt for items in selection in extract_module
1 parent 9c8a57e commit 4180565

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

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

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,7 @@ impl Module {
462462
}
463463
}
464464

465-
let def_in_mod_and_out_sel =
465+
let (def_in_mod, def_out_sel) =
466466
check_def_in_mod_and_out_sel(def, ctx, curr_parent_module, selection_range, file_id);
467467

468468
// Find use stmt that use def in current file
@@ -493,9 +493,9 @@ impl Module {
493493
//If not, insert a use stmt with super and the given nameref
494494
match self.process_use_stmt_for_import_resolve(use_stmt, node_syntax) {
495495
Some((use_tree_str, _)) => use_tree_str_opt = Some(use_tree_str),
496-
None if def_in_mod_and_out_sel => {
496+
None if def_in_mod && def_out_sel => {
497497
//Considered only after use_stmt is not present
498-
//def_in_mod_and_out_sel | exists_outside_sel(exists_inside_sel =
498+
//def_in_mod && def_out_sel | exists_outside_sel(exists_inside_sel =
499499
//true for all cases)
500500
// false | false -> Do nothing
501501
// false | true -> If source is in selection -> nothing to do, If source is outside
@@ -516,7 +516,7 @@ impl Module {
516516
import_path_to_be_removed = Some(text_range);
517517
}
518518

519-
if def_in_mod_and_out_sel {
519+
if def_in_mod && def_out_sel {
520520
if let Some(first_path_in_use_tree) = use_tree_str.last() {
521521
let first_path_in_use_tree_str = first_path_in_use_tree.to_string();
522522
if !first_path_in_use_tree_str.contains("super")
@@ -529,7 +529,7 @@ impl Module {
529529
}
530530

531531
use_tree_str_opt = Some(use_tree_str);
532-
} else if def_in_mod_and_out_sel {
532+
} else if def_in_mod && def_out_sel {
533533
self.make_use_stmt_of_node_with_super(node_syntax);
534534
}
535535
}
@@ -538,7 +538,7 @@ impl Module {
538538
let mut use_tree_str = use_tree_str;
539539
use_tree_str.reverse();
540540

541-
if exists_outside_sel || !exists_inside_sel || !def_in_mod_and_out_sel {
541+
if exists_outside_sel || !exists_inside_sel || !def_in_mod || !def_out_sel {
542542
if let Some(first_path_in_use_tree) = use_tree_str.first() {
543543
if first_path_in_use_tree.to_string().contains("super") {
544544
use_tree_str.insert(0, make::ext::ident_path("super"));
@@ -549,7 +549,10 @@ impl Module {
549549
let use_ =
550550
make::use_(None, make::use_tree(make::join_paths(use_tree_str), None, None, false));
551551
let item = ast::Item::from(use_);
552-
self.use_items.insert(0, item);
552+
553+
if def_out_sel {
554+
self.use_items.insert(0, item.clone());
555+
}
553556
}
554557

555558
import_path_to_be_removed
@@ -624,7 +627,7 @@ fn check_def_in_mod_and_out_sel(
624627
curr_parent_module: &Option<ast::Module>,
625628
selection_range: TextRange,
626629
curr_file_id: FileId,
627-
) -> bool {
630+
) -> (bool, bool) {
628631
macro_rules! check_item {
629632
($x:ident) => {
630633
if let Some(source) = $x.source(ctx.db()) {
@@ -634,9 +637,8 @@ fn check_def_in_mod_and_out_sel(
634637
source.file_id.original_file(ctx.db()) == curr_file_id
635638
};
636639

637-
if have_same_parent {
638-
return !selection_range.contains_range(source.value.syntax().text_range());
639-
}
640+
let in_sel = !selection_range.contains_range(source.value.syntax().text_range());
641+
return (have_same_parent, in_sel);
640642
}
641643
};
642644
}
@@ -653,9 +655,12 @@ fn check_def_in_mod_and_out_sel(
653655

654656
if have_same_parent {
655657
if let ModuleSource::Module(module_) = source.value {
656-
return !selection_range.contains_range(module_.syntax().text_range());
658+
let in_sel = !selection_range.contains_range(module_.syntax().text_range());
659+
return (have_same_parent, in_sel);
657660
}
658661
}
662+
663+
return (have_same_parent, false);
659664
}
660665
Definition::Function(x) => check_item!(x),
661666
Definition::Adt(x) => check_item!(x),
@@ -667,7 +672,7 @@ fn check_def_in_mod_and_out_sel(
667672
_ => {}
668673
}
669674

670-
false
675+
(false, false)
671676
}
672677

673678
fn get_replacements_for_visibility_change(

0 commit comments

Comments
 (0)