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+
36use crate :: completion:: { CompletionContext , Completions } ;
7+ use ra_syntax:: AstNode ;
48
59pub ( 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) ]
2135mod 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 ! (
0 commit comments