]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_assists/src/handlers/replace_qualified_name_with_use.rs
Merge #11481
[rust.git] / crates / ide_assists / src / handlers / replace_qualified_name_with_use.rs
index 4a30b83b5a3d0849c0ed6ee4744f2ea3327ff610..3121e2298178068fbf5d041f986c81cf1d237c01 100644 (file)
@@ -54,12 +54,23 @@ pub(crate) fn replace_qualified_name_with_use(
         _ => return None,
     };
 
-    let scope = ImportScope::find_insert_use_container_with_macros(path.syntax(), &ctx.sema)?;
-    let path_to_qualifier = ctx.sema.scope(path.syntax()).module()?.find_use_path_prefixed(
-        ctx.sema.db,
-        module,
-        ctx.config.insert_use.prefix_kind,
-    )?;
+    let starts_with_name_ref = !matches!(
+        path.first_segment().and_then(|it| it.kind()),
+        Some(
+            ast::PathSegmentKind::CrateKw
+                | ast::PathSegmentKind::SuperKw
+                | ast::PathSegmentKind::SelfKw
+        )
+    );
+    let path_to_qualifier = starts_with_name_ref
+        .then(|| {
+            ctx.sema.scope(path.syntax()).module().and_then(|m| {
+                m.find_use_path_prefixed(ctx.sema.db, module, ctx.config.insert_use.prefix_kind)
+            })
+        })
+        .flatten();
+
+    let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?;
     let target = path.syntax().text_range();
     acc.add(
         AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
@@ -73,12 +84,12 @@ pub(crate) fn replace_qualified_name_with_use(
                 ImportScope::Module(it) => ImportScope::Module(builder.make_mut(it)),
                 ImportScope::Block(it) => ImportScope::Block(builder.make_mut(it)),
             };
+            shorten_paths(scope.as_syntax_node(), &path.clone_for_update());
             // stick the found import in front of the to be replaced path
-            let path = match mod_path_to_ast(&path_to_qualifier).qualifier() {
+            let path = match path_to_qualifier.and_then(|it| mod_path_to_ast(&it).qualifier()) {
                 Some(qualifier) => make::path_concat(qualifier, path),
                 None => path,
             };
-            shorten_paths(scope.as_syntax_node(), &path.clone_for_update());
             insert_use(&scope, path, &ctx.config.insert_use);
         },
     )
@@ -91,10 +102,10 @@ fn shorten_paths(node: &SyntaxNode, path: &ast::Path) {
             match child {
                 // Don't modify `use` items, as this can break the `use` item when injecting a new
                 // import into the use tree.
-                ast::Use(_it) => continue,
+                ast::Use(_) => continue,
                 // Don't descend into submodules, they don't have the same `use` items in scope.
                 // FIXME: This isn't true due to `super::*` imports?
-                ast::Module(_it) => continue,
+                ast::Module(_) => continue,
                 ast::Path(p) => if maybe_replace_path(p.clone(), path.clone()).is_none() {
                     shorten_paths(p.syntax(), path);
                 },
@@ -130,10 +141,7 @@ fn path_eq_no_generics(lhs: ast::Path, rhs: ast::Path) -> bool {
                     && lhs
                         .name_ref()
                         .zip(rhs.name_ref())
-                        .map_or(false, |(lhs, rhs)| lhs.text() == rhs.text()) =>
-            {
-                ()
-            }
+                        .map_or(false, |(lhs, rhs)| lhs.text() == rhs.text()) => {}
             _ => return false,
         }
 
@@ -208,7 +216,7 @@ fn test_replace_add_use_no_anchor_middle_segment() {
     ",
         );
     }
-    #[test]
+
     #[test]
     fn dont_import_trivial_paths() {
         cov_mark::check!(dont_import_trivial_paths);
@@ -291,7 +299,7 @@ fn main() {
     ",
             r"
 mod std { pub mod fmt { pub trait Display {} } }
-use std::fmt::{self, Display};
+use std::fmt::{Display, self};
 
 fn main() {
     fmt;
@@ -323,7 +331,7 @@ fn replace_reuses_path_qualifier() {
             replace_qualified_name_with_use,
             r"
 pub mod foo {
-    struct Foo;
+    pub struct Foo;
 }
 
 mod bar {
@@ -338,7 +346,7 @@ fn main() {
 use foo::Foo;
 
 pub mod foo {
-    struct Foo;
+    pub struct Foo;
 }
 
 mod bar {
@@ -348,6 +356,39 @@ mod bar {
 fn main() {
     Foo;
 }
+",
+        );
+    }
+
+    #[test]
+    fn replace_does_not_always_try_to_replace_by_full_item_path() {
+        check_assist(
+            replace_qualified_name_with_use,
+            r"
+use std::mem;
+
+mod std {
+    pub mod mem {
+        pub fn drop<T>(_: T) {}
+    }
+}
+
+fn main() {
+    mem::drop$0(0);
+}
+",
+            r"
+use std::mem::{self, drop};
+
+mod std {
+    pub mod mem {
+        pub fn drop<T>(_: T) {}
+    }
+}
+
+fn main() {
+    drop(0);
+}
 ",
         );
     }