From 01d59f4a322c019cb9cbc2935fe8e7ba432bcd3d Mon Sep 17 00:00:00 2001 From: Kirill Bulatov Date: Sun, 2 Feb 2020 23:24:43 +0200 Subject: Use name only when searching for an import candidate --- crates/ra_assists/src/assists/auto_import.rs | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'crates/ra_assists') diff --git a/crates/ra_assists/src/assists/auto_import.rs b/crates/ra_assists/src/assists/auto_import.rs index 2629f00e6..7fd1f0e7d 100644 --- a/crates/ra_assists/src/assists/auto_import.rs +++ b/crates/ra_assists/src/assists/auto_import.rs @@ -1,7 +1,7 @@ use hir::{db::HirDatabase, ModPath}; use ra_syntax::{ ast::{self, AstNode}, - SyntaxKind::USE_ITEM, + SyntaxKind::{NAME_REF, USE_ITEM}, SyntaxNode, }; @@ -36,6 +36,8 @@ pub(crate) fn auto_import( if path_to_import_syntax.ancestors().find(|ancestor| ancestor.kind() == USE_ITEM).is_some() { return None; } + let name_to_import = + path_to_import_syntax.descendants().find(|child| child.kind() == NAME_REF)?; let module = path_to_import_syntax.ancestors().find_map(ast::Module::cast); let position = match module.and_then(|it| it.item_list()) { @@ -52,7 +54,7 @@ pub(crate) fn auto_import( } let proposed_imports = imports_locator - .find_imports(&path_to_import_syntax.to_string()) + .find_imports(&name_to_import.to_string()) .into_iter() .filter_map(|module_def| module_with_name_to_import.find_use_path(ctx.db, module_def)) .filter(|use_path| !use_path.segments.is_empty()) @@ -121,21 +123,29 @@ mod tests { r" use PubMod::PubStruct1; - PubStruct2<|> + struct Test { + test: Pub<|>Struct2, + } pub mod PubMod { pub struct PubStruct1; - pub struct PubStruct2; + pub struct PubStruct2 { + _t: T, + } } ", r" use PubMod::{PubStruct2, PubStruct1}; - PubStruct2<|> + struct Test { + test: Pub<|>Struct2, + } pub mod PubMod { pub struct PubStruct1; - pub struct PubStruct2; + pub struct PubStruct2 { + _t: T, + } } ", ); -- cgit v1.2.3