Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs')
-rw-r--r--crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs38
1 files changed, 22 insertions, 16 deletions
diff --git a/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs b/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
index d0aa835e79..65330b34c4 100644
--- a/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
+++ b/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs
@@ -5,7 +5,7 @@ use ide_db::{
};
use syntax::{
ast::{self, make, HasGenericArgs},
- match_ast, ted, AstNode, SyntaxNode,
+ match_ast, ted, AstNode, Edition, SyntaxNode,
};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@@ -29,32 +29,32 @@ pub(crate) fn replace_qualified_name_with_use(
acc: &mut Assists,
ctx: &AssistContext<'_>,
) -> Option<()> {
- let path: ast::Path = ctx.find_node_at_offset()?;
+ let original_path: ast::Path = ctx.find_node_at_offset()?;
// We don't want to mess with use statements
- if path.syntax().ancestors().find_map(ast::UseTree::cast).is_some() {
+ if original_path.syntax().ancestors().find_map(ast::UseTree::cast).is_some() {
cov_mark::hit!(not_applicable_in_use);
return None;
}
- if path.qualifier().is_none() {
+ if original_path.qualifier().is_none() {
cov_mark::hit!(dont_import_trivial_paths);
return None;
}
// only offer replacement for non assoc items
- match ctx.sema.resolve_path(&path)? {
+ match ctx.sema.resolve_path(&original_path)? {
hir::PathResolution::Def(def) if def.as_assoc_item(ctx.sema.db).is_none() => (),
_ => return None,
}
// then search for an import for the first path segment of what we want to replace
// that way it is less likely that we import the item from a different location due re-exports
- let module = match ctx.sema.resolve_path(&path.first_qualifier_or_self())? {
+ let module = match ctx.sema.resolve_path(&original_path.first_qualifier_or_self())? {
hir::PathResolution::Def(module @ hir::ModuleDef::Module(_)) => module,
_ => return None,
};
let starts_with_name_ref = !matches!(
- path.first_segment().and_then(|it| it.kind()),
+ original_path.first_segment().and_then(|it| it.kind()),
Some(
ast::PathSegmentKind::CrateKw
| ast::PathSegmentKind::SuperKw
@@ -63,7 +63,7 @@ pub(crate) fn replace_qualified_name_with_use(
);
let path_to_qualifier = starts_with_name_ref
.then(|| {
- ctx.sema.scope(path.syntax())?.module().find_use_path(
+ ctx.sema.scope(original_path.syntax())?.module().find_use_path(
ctx.sema.db,
module,
ctx.config.insert_use.prefix_kind,
@@ -72,8 +72,8 @@ pub(crate) fn replace_qualified_name_with_use(
})
.flatten();
- let scope = ImportScope::find_insert_use_container(path.syntax(), &ctx.sema)?;
- let target = path.syntax().text_range();
+ let scope = ImportScope::find_insert_use_container(original_path.syntax(), &ctx.sema)?;
+ let target = original_path.syntax().text_range();
acc.add(
AssistId("replace_qualified_name_with_use", AssistKind::RefactorRewrite),
"Replace qualified path with use",
@@ -86,13 +86,19 @@ 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);
- let path = drop_generic_args(&path);
+ shorten_paths(scope.as_syntax_node(), &original_path);
+ let path = drop_generic_args(&original_path);
+ let edition = ctx
+ .sema
+ .scope(original_path.syntax())
+ .map(|semantics_scope| semantics_scope.krate().edition(ctx.db()))
+ .unwrap_or(Edition::CURRENT);
// stick the found import in front of the to be replaced path
- let path = match path_to_qualifier.and_then(|it| mod_path_to_ast(&it).qualifier()) {
- Some(qualifier) => make::path_concat(qualifier, path),
- None => path,
- };
+ let path =
+ match path_to_qualifier.and_then(|it| mod_path_to_ast(&it, edition).qualifier()) {
+ Some(qualifier) => make::path_concat(qualifier, path),
+ None => path,
+ };
insert_use(&scope, path, &ctx.config.insert_use);
},
)