4011: Don\t suggest import itself as a completion for import r=matklad a=matklad



bors r+
🤖

Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-04-17 12:28:57 +00:00 committed by GitHub
commit 48a9e20618
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View file

@ -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!(

View file

@ -8,4 +8,5 @@ test_utils::marks!(
test_resolve_parent_module_on_module_decl
search_filters_by_range
dont_insert_macro_call_parens_unncessary
self_fulfilling_completion
);