Skip to content

Commit 48a9e20

Browse files
bors[bot]matklad
andauthored
Merge #4011
4011: Don\t suggest import itself as a completion for import r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 0262c9b + 028f1e2 commit 48a9e20

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

crates/ra_ide/src/completion/complete_unqualified_path.rs

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! Completion of names from the current scope, e.g. locals and imported items.
22
3+
use hir::ScopeDef;
4+
use test_utils::tested_by;
5+
36
use crate::completion::{CompletionContext, Completions};
7+
use ra_syntax::AstNode;
48

59
pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
610
if !ctx.is_trivial_path {
@@ -14,19 +18,53 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
1418
return;
1519
}
1620

17-
ctx.scope().process_all_names(&mut |name, res| acc.add_resolution(ctx, name.to_string(), &res));
21+
ctx.scope().process_all_names(&mut |name, res| {
22+
if ctx.use_item_syntax.is_some() {
23+
if let (ScopeDef::Unknown, Some(name_ref)) = (&res, &ctx.name_ref_syntax) {
24+
if name_ref.syntax().text() == name.to_string().as_str() {
25+
tested_by!(self_fulfilling_completion);
26+
return;
27+
}
28+
}
29+
}
30+
acc.add_resolution(ctx, name.to_string(), &res)
31+
});
1832
}
1933

2034
#[cfg(test)]
2135
mod tests {
2236
use insta::assert_debug_snapshot;
37+
use test_utils::covers;
2338

2439
use crate::completion::{test_utils::do_completion, CompletionItem, CompletionKind};
2540

2641
fn do_reference_completion(ra_fixture: &str) -> Vec<CompletionItem> {
2742
do_completion(ra_fixture, CompletionKind::Reference)
2843
}
2944

45+
#[test]
46+
fn self_fulfilling_completion() {
47+
covers!(self_fulfilling_completion);
48+
assert_debug_snapshot!(
49+
do_reference_completion(
50+
r#"
51+
use foo<|>
52+
use std::collections;
53+
"#,
54+
),
55+
@r###"
56+
[
57+
CompletionItem {
58+
label: "collections",
59+
source_range: [21; 24),
60+
delete: [21; 24),
61+
insert: "collections",
62+
},
63+
]
64+
"###
65+
);
66+
}
67+
3068
#[test]
3169
fn bind_pat_and_path_ignore_at() {
3270
assert_debug_snapshot!(

crates/ra_ide/src/marks.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ test_utils::marks!(
88
test_resolve_parent_module_on_module_decl
99
search_filters_by_range
1010
dont_insert_macro_call_parens_unncessary
11+
self_fulfilling_completion
1112
);

0 commit comments

Comments
 (0)