Skip to content

Commit 5b1b2ca

Browse files
Merge #2990
2990: Use name only when searching for an import candidate r=me a=SomeoneToIgnore Co-authored-by: Kirill Bulatov <[email protected]>
2 parents 52456c4 + 01d59f4 commit 5b1b2ca

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

crates/ra_assists/src/assists/auto_import.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use hir::{db::HirDatabase, ModPath};
22
use ra_syntax::{
33
ast::{self, AstNode},
4-
SyntaxKind::USE_ITEM,
4+
SyntaxKind::{NAME_REF, USE_ITEM},
55
SyntaxNode,
66
};
77

@@ -36,6 +36,8 @@ pub(crate) fn auto_import<F: ImportsLocator>(
3636
if path_to_import_syntax.ancestors().find(|ancestor| ancestor.kind() == USE_ITEM).is_some() {
3737
return None;
3838
}
39+
let name_to_import =
40+
path_to_import_syntax.descendants().find(|child| child.kind() == NAME_REF)?;
3941

4042
let module = path_to_import_syntax.ancestors().find_map(ast::Module::cast);
4143
let position = match module.and_then(|it| it.item_list()) {
@@ -52,7 +54,7 @@ pub(crate) fn auto_import<F: ImportsLocator>(
5254
}
5355

5456
let proposed_imports = imports_locator
55-
.find_imports(&path_to_import_syntax.to_string())
57+
.find_imports(&name_to_import.to_string())
5658
.into_iter()
5759
.filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def))
5860
.filter(|use_path| !use_path.segments.is_empty())
@@ -121,21 +123,29 @@ mod tests {
121123
r"
122124
use PubMod::PubStruct1;
123125
124-
PubStruct2<|>
126+
struct Test {
127+
test: Pub<|>Struct2<u8>,
128+
}
125129
126130
pub mod PubMod {
127131
pub struct PubStruct1;
128-
pub struct PubStruct2;
132+
pub struct PubStruct2<T> {
133+
_t: T,
134+
}
129135
}
130136
",
131137
r"
132138
use PubMod::{PubStruct2, PubStruct1};
133139
134-
PubStruct2<|>
140+
struct Test {
141+
test: Pub<|>Struct2<u8>,
142+
}
135143
136144
pub mod PubMod {
137145
pub struct PubStruct1;
138-
pub struct PubStruct2;
146+
pub struct PubStruct2<T> {
147+
_t: T,
148+
}
139149
}
140150
",
141151
);

0 commit comments

Comments
 (0)