diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index a2523c5efe..2473e58b40 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -1,6 +1,6 @@ use rustc_hash::FxHashMap; use ra_text_edit::TextEditBuilder; -use ra_syntax::SmolStr; +use ra_syntax::{SmolStr, ast, AstNode}; use ra_assists::auto_import; use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext}; @@ -9,41 +9,43 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) { if ctx.is_trivial_path { let names = ctx.analyzer.all_names(ctx.db); names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res)); - } - if let Some(name) = ctx.path_ident.as_ref() { - let import_resolver = ImportResolver::new(); - let import_names = import_resolver.all_names(&name.to_string()); - import_names.into_iter().for_each(|(name, path)| { - let edit = { - let mut builder = TextEditBuilder::default(); - builder.replace(ctx.source_range(), name.to_string()); - auto_import::auto_import_text_edit( - ctx.token.parent(), - ctx.token.parent(), - &path, - &mut builder, - ); - builder.finish() - }; + // auto-import + // We fetch ident from the original file, because we need to pre-filter auto-imports + if ast::NameRef::cast(ctx.token.parent()).is_some() { + let import_resolver = ImportResolver::new(); + let import_names = import_resolver.all_names(ctx.token.text()); + import_names.into_iter().for_each(|(name, path)| { + let edit = { + let mut builder = TextEditBuilder::default(); + builder.replace(ctx.source_range(), name.to_string()); + auto_import::auto_import_text_edit( + ctx.token.parent(), + ctx.token.parent(), + &path, + &mut builder, + ); + builder.finish() + }; - // Hack: copied this check form conv.rs beacause auto import can produce edits - // that invalidate assert in conv_with. - if edit - .as_atoms() - .iter() - .filter(|atom| !ctx.source_range().is_subrange(&atom.delete)) - .all(|atom| ctx.source_range().intersection(&atom.delete).is_none()) - { - CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - build_import_label(&name, &path), - ) - .text_edit(edit) - .add_to(acc); - } - }); + // Hack: copied this check form conv.rs beacause auto import can produce edits + // that invalidate assert in conv_with. + if edit + .as_atoms() + .iter() + .filter(|atom| !ctx.source_range().is_subrange(&atom.delete)) + .all(|atom| ctx.source_range().intersection(&atom.delete).is_none()) + { + CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + build_import_label(&name, &path), + ) + .text_edit(edit) + .add_to(acc); + } + }); + } } } diff --git a/crates/ra_ide_api/src/completion/completion_context.rs b/crates/ra_ide_api/src/completion/completion_context.rs index 0d630fdf6c..a8c8cc7b04 100644 --- a/crates/ra_ide_api/src/completion/completion_context.rs +++ b/crates/ra_ide_api/src/completion/completion_context.rs @@ -5,10 +5,10 @@ use ra_syntax::{ algo::{find_token_at_offset, find_covering_element, find_node_at_offset}, SyntaxKind::*, }; - -use hir::{ source_binder, Name }; +use hir::source_binder; use crate::{db, FilePosition}; + /// `CompletionContext` is created early during completion to figure out, where /// exactly is the cursor, syntax-wise. #[derive(Debug)] @@ -29,8 +29,6 @@ pub(crate) struct CompletionContext<'a> { pub(super) is_trivial_path: bool, /// If not a trivial path, the prefix (qualifier). pub(super) path_prefix: Option, - /// If a trivial path, the ident. - pub(super) path_ident: Option, pub(super) after_if: bool, /// `true` if we are a statement or a last expr in the block. pub(super) can_be_stmt: bool, @@ -65,7 +63,6 @@ impl<'a> CompletionContext<'a> { is_pat_binding: false, is_trivial_path: false, path_prefix: None, - path_ident: None, after_if: false, can_be_stmt: false, is_new_item: false,