1
1
//! Completion of names from the current scope, e.g. locals and imported items.
2
2
3
+ use hir:: ScopeDef ;
4
+ use test_utils:: tested_by;
5
+
3
6
use crate :: completion:: { CompletionContext , Completions } ;
7
+ use ra_syntax:: AstNode ;
4
8
5
9
pub ( super ) fn complete_unqualified_path ( acc : & mut Completions , ctx : & CompletionContext ) {
6
10
if !ctx. is_trivial_path {
@@ -14,19 +18,53 @@ pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
14
18
return ;
15
19
}
16
20
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
+ } ) ;
18
32
}
19
33
20
34
#[ cfg( test) ]
21
35
mod tests {
22
36
use insta:: assert_debug_snapshot;
37
+ use test_utils:: covers;
23
38
24
39
use crate :: completion:: { test_utils:: do_completion, CompletionItem , CompletionKind } ;
25
40
26
41
fn do_reference_completion ( ra_fixture : & str ) -> Vec < CompletionItem > {
27
42
do_completion ( ra_fixture, CompletionKind :: Reference )
28
43
}
29
44
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
+
30
68
#[ test]
31
69
fn bind_pat_and_path_ignore_at ( ) {
32
70
assert_debug_snapshot ! (
0 commit comments