From 028f1e2e3add764956911a0f2663107cb945c0ec Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 17 Apr 2020 14:28:20 +0200 Subject: Don\t suggest import itself as a completion for import --- .../src/completion/complete_unqualified_path.rs | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'crates/ra_ide/src/completion') diff --git a/crates/ra_ide/src/completion/complete_unqualified_path.rs b/crates/ra_ide/src/completion/complete_unqualified_path.rs index 2d8e0776c..ad5fdcc4e 100644 --- a/crates/ra_ide/src/completion/complete_unqualified_path.rs +++ b/crates/ra_ide/src/completion/complete_unqualified_path.rs @@ -1,6 +1,10 @@ //! Completion of names from the current scope, e.g. locals and imported items. +use hir::ScopeDef; +use test_utils::tested_by; + use crate::completion::{CompletionContext, Completions}; +use ra_syntax::AstNode; pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) { if !ctx.is_trivial_path { @@ -14,12 +18,23 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC return; } - ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res)); + ctx.scope().process_all_names(&mut |name, res| { + if ctx.use_item_syntax.is_some() { + if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) { + if name_ref.syntax().text() == name.to_string().as_str() { + tested_by!(self_fulfilling_completion); + return; + } + } + } + acc.add_resolution(ctx, name.to_string(), &res) + }); } #[cfg(test)] mod tests { use insta::assert_debug_snapshot; + use test_utils::covers; use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind}; @@ -27,6 +42,29 @@ mod tests { do_completion(ra_fixture, CompletionKind::Reference) } + #[test] + fn self_fulfilling_completion() { + covers!(self_fulfilling_completion); + assert_debug_snapshot!( + do_reference_completion( + r#" + use foo<|> + use std::collections; + "#, + ), + @r###" + [ + CompletionItem { + label: "collections", + source_range: [21; 24), + delete: [21; 24), + insert: "collections", + }, + ] + "### + ); + } + #[test] fn bind_pat_and_path_ignore_at() { assert_debug_snapshot!( -- cgit v1.2.3