Skip to content

Commit 6d219c9

Browse files
Properly select a target for auto importing
1 parent e3c81b6 commit 6d219c9

File tree

1 file changed

+17
-29
lines changed

1 file changed

+17
-29
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use hir::db::HirDatabase;
22
use ra_syntax::{
33
ast::{self, AstNode},
4-
SmolStr, SyntaxElement,
5-
SyntaxKind::{NAME_REF, USE_ITEM},
4+
SmolStr,
5+
SyntaxKind::USE_ITEM,
66
SyntaxNode,
77
};
88

@@ -32,25 +32,28 @@ pub(crate) fn auto_import<F: ImportsLocator>(
3232
ctx: AssistCtx<impl HirDatabase>,
3333
imports_locator: &mut F,
3434
) -> Option<Assist> {
35-
let path: ast::Path = ctx.find_node_at_offset()?;
36-
let module = path.syntax().ancestors().find_map(ast::Module::cast);
35+
let path_to_import: ast::Path = ctx.find_node_at_offset()?;
36+
let path_to_import_syntax = path_to_import.syntax();
37+
if path_to_import_syntax.ancestors().find(|ancestor| ancestor.kind() == USE_ITEM).is_some() {
38+
return None;
39+
}
40+
41+
let module = path_to_import_syntax.ancestors().find_map(ast::Module::cast);
3742
let position = match module.and_then(|it| it.item_list()) {
3843
Some(item_list) => item_list.syntax().clone(),
3944
None => {
40-
let current_file = path.syntax().ancestors().find_map(ast::SourceFile::cast)?;
45+
let current_file = path_to_import_syntax.ancestors().find_map(ast::SourceFile::cast)?;
4146
current_file.syntax().clone()
4247
}
4348
};
4449
let source_analyzer = ctx.source_analyzer(&position, None);
4550
let module_with_name_to_import = source_analyzer.module()?;
46-
let path_to_import = ctx.covering_element().ancestors().find_map(ast::Path::cast)?;
4751
if source_analyzer.resolve_path(ctx.db, &path_to_import).is_some() {
4852
return None;
4953
}
5054

51-
let name_to_import = &find_applicable_name_ref(ctx.covering_element())?.syntax().to_string();
5255
let proposed_imports = imports_locator
53-
.find_imports(&name_to_import.to_string())
56+
.find_imports(&path_to_import_syntax.to_string())
5457
.into_iter()
5558
.filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
5659
.filter(|use_path| !use_path.segments.is_empty())
@@ -64,26 +67,11 @@ pub(crate) fn auto_import<F: ImportsLocator>(
6467
ctx.add_assist_group(AssistId("auto_import"), "auto import", || {
6568
proposed_imports
6669
.into_iter()
67-
.map(|import| import_to_action(import, &position, &path_to_import.syntax()))
70+
.map(|import| import_to_action(import, &position, &path_to_import_syntax))
6871
.collect()
6972
})
7073
}
7174

72-
fn find_applicable_name_ref(element: SyntaxElement) -> Option<ast::NameRef> {
73-
if element.ancestors().find(|ancestor| ancestor.kind() == USE_ITEM).is_some() {
74-
None
75-
} else if element.kind() == NAME_REF {
76-
Some(element.as_node().cloned().and_then(ast::NameRef::cast)?)
77-
} else {
78-
let parent = element.parent()?;
79-
if parent.kind() == NAME_REF {
80-
Some(ast::NameRef::cast(parent)?)
81-
} else {
82-
None
83-
}
84-
}
85-
}
86-
8775
fn import_to_action(import: String, position: &SyntaxNode, anchor: &SyntaxNode) -> ActionBuilder {
8876
let mut action_builder = ActionBuilder::default();
8977
action_builder.label(format!("Import `{}`", &import));
@@ -110,16 +98,16 @@ mod tests {
11098
auto_import,
11199
TestImportsLocator::new,
112100
r"
113-
PubStruct<|>
101+
<|>PubStruct
114102
115103
pub mod PubMod {
116104
pub struct PubStruct;
117105
}
118106
",
119107
r"
120-
use PubMod::PubStruct;
108+
<|>use PubMod::PubStruct;
121109
122-
PubStruct<|>
110+
PubStruct
123111
124112
pub mod PubMod {
125113
pub struct PubStruct;
@@ -134,7 +122,7 @@ mod tests {
134122
auto_import,
135123
TestImportsLocator::new,
136124
r"
137-
PubStruct<|>
125+
PubSt<|>ruct
138126
139127
pub mod PubMod1 {
140128
pub struct PubStruct;
@@ -149,7 +137,7 @@ mod tests {
149137
r"
150138
use PubMod1::PubStruct;
151139
152-
PubStruct<|>
140+
PubSt<|>ruct
153141
154142
pub mod PubMod1 {
155143
pub struct PubStruct;

0 commit comments

Comments
 (0)