about summary refs log tree commit diff
diff options
context:
space:
mode:
authorroife <roifewu@gmail.com>2024-03-14 15:18:31 +0800
committerroife <roifewu@gmail.com>2024-03-14 15:18:31 +0800
commit418056597b6a5c55fa8652b26728e7cb9589ebc8 (patch)
treed8eb6a06a8d0e0e1748c90c3f457b772049526a3
parent9c8a57ed08a4341f881302c81633bf33ac99a6ed (diff)
downloadrust-418056597b6a5c55fa8652b26728e7cb9589ebc8.tar.gz
rust-418056597b6a5c55fa8652b26728e7cb9589ebc8.zip
fix: donot generate redundant use stmt for items in selection in extract_module
-rw-r--r--crates/ide-assists/src/handlers/extract_module.rs31
1 files changed, 18 insertions, 13 deletions
diff --git a/crates/ide-assists/src/handlers/extract_module.rs b/crates/ide-assists/src/handlers/extract_module.rs
index 95bb4e174be..7c486ae6f75 100644
--- a/crates/ide-assists/src/handlers/extract_module.rs
+++ b/crates/ide-assists/src/handlers/extract_module.rs
@@ -462,7 +462,7 @@ impl Module {
             }
         }
 
-        let def_in_mod_and_out_sel =
+        let (def_in_mod, def_out_sel) =
             check_def_in_mod_and_out_sel(def, ctx, curr_parent_module, selection_range, file_id);
 
         // Find use stmt that use def in current file
@@ -493,9 +493,9 @@ impl Module {
             //If not, insert a use stmt with super and the given nameref
             match self.process_use_stmt_for_import_resolve(use_stmt, node_syntax) {
                 Some((use_tree_str, _)) => use_tree_str_opt = Some(use_tree_str),
-                None if def_in_mod_and_out_sel => {
+                None if def_in_mod && def_out_sel => {
                     //Considered only after use_stmt is not present
-                    //def_in_mod_and_out_sel | exists_outside_sel(exists_inside_sel =
+                    //def_in_mod && def_out_sel | exists_outside_sel(exists_inside_sel =
                     //true for all cases)
                     // false | false -> Do nothing
                     // false | true -> If source is in selection -> nothing to do, If source is outside
@@ -516,7 +516,7 @@ impl Module {
                     import_path_to_be_removed = Some(text_range);
                 }
 
-                if def_in_mod_and_out_sel {
+                if def_in_mod && def_out_sel {
                     if let Some(first_path_in_use_tree) = use_tree_str.last() {
                         let first_path_in_use_tree_str = first_path_in_use_tree.to_string();
                         if !first_path_in_use_tree_str.contains("super")
@@ -529,7 +529,7 @@ impl Module {
                 }
 
                 use_tree_str_opt = Some(use_tree_str);
-            } else if def_in_mod_and_out_sel {
+            } else if def_in_mod && def_out_sel {
                 self.make_use_stmt_of_node_with_super(node_syntax);
             }
         }
@@ -538,7 +538,7 @@ impl Module {
             let mut use_tree_str = use_tree_str;
             use_tree_str.reverse();
 
-            if exists_outside_sel || !exists_inside_sel || !def_in_mod_and_out_sel {
+            if exists_outside_sel || !exists_inside_sel || !def_in_mod || !def_out_sel {
                 if let Some(first_path_in_use_tree) = use_tree_str.first() {
                     if first_path_in_use_tree.to_string().contains("super") {
                         use_tree_str.insert(0, make::ext::ident_path("super"));
@@ -549,7 +549,10 @@ impl Module {
             let use_ =
                 make::use_(None, make::use_tree(make::join_paths(use_tree_str), None, None, false));
             let item = ast::Item::from(use_);
-            self.use_items.insert(0, item);
+
+            if def_out_sel {
+                self.use_items.insert(0, item.clone());
+            }
         }
 
         import_path_to_be_removed
@@ -624,7 +627,7 @@ fn check_def_in_mod_and_out_sel(
     curr_parent_module: &Option<ast::Module>,
     selection_range: TextRange,
     curr_file_id: FileId,
-) -> bool {
+) -> (bool, bool) {
     macro_rules! check_item {
         ($x:ident) => {
             if let Some(source) = $x.source(ctx.db()) {
@@ -634,9 +637,8 @@ fn check_def_in_mod_and_out_sel(
                     source.file_id.original_file(ctx.db()) == curr_file_id
                 };
 
-                if have_same_parent {
-                    return !selection_range.contains_range(source.value.syntax().text_range());
-                }
+                let in_sel = !selection_range.contains_range(source.value.syntax().text_range());
+                return (have_same_parent, in_sel);
             }
         };
     }
@@ -653,9 +655,12 @@ fn check_def_in_mod_and_out_sel(
 
             if have_same_parent {
                 if let ModuleSource::Module(module_) = source.value {
-                    return !selection_range.contains_range(module_.syntax().text_range());
+                    let in_sel = !selection_range.contains_range(module_.syntax().text_range());
+                    return (have_same_parent, in_sel);
                 }
             }
+
+            return (have_same_parent, false);
         }
         Definition::Function(x) => check_item!(x),
         Definition::Adt(x) => check_item!(x),
@@ -667,7 +672,7 @@ fn check_def_in_mod_and_out_sel(
         _ => {}
     }
 
-    false
+    (false, false)
 }
 
 fn get_replacements_for_visibility_change(