aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_analysis/src/completion/complete_use_tree.rs
blob: 5f2f6e4496eff08aef5560495511c355c208ec8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use crate::completion::{CompletionContext, CompletionItem, Completions, CompletionKind, CompletionItemKind};

pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
    // complete keyword "crate" in use stmt
    match (ctx.use_item_syntax.as_ref(), ctx.path_prefix.as_ref()) {
        (Some(_), None) => {
            CompletionItem::new(CompletionKind::Keyword, "crate")
                .kind(CompletionItemKind::Keyword)
                .lookup_by("crate")
                .snippet("crate::")
                .add_to(acc);
            CompletionItem::new(CompletionKind::Keyword, "self")
                .kind(CompletionItemKind::Keyword)
                .lookup_by("self")
                .add_to(acc);
            CompletionItem::new(CompletionKind::Keyword, "super")
                .kind(CompletionItemKind::Keyword)
                .lookup_by("super")
                .add_to(acc);
        }
        (Some(_), Some(_)) => {
            CompletionItem::new(CompletionKind::Keyword, "self")
                .kind(CompletionItemKind::Keyword)
                .lookup_by("self")
                .add_to(acc);
            CompletionItem::new(CompletionKind::Keyword, "super")
                .kind(CompletionItemKind::Keyword)
                .lookup_by("super")
                .add_to(acc);
        }
        _ => {}
    }
}

#[cfg(test)]
mod tests {
    use crate::completion::{CompletionKind, check_completion};
    fn check_keyword_completion(code: &str, expected_completions: &str) {
        check_completion(code, expected_completions, CompletionKind::Keyword);
    }

    #[test]
    fn completes_keywords_in_use_stmt() {
        check_keyword_completion(
            r"
            use <|>
            ",
            r#"
            crate "crate" "crate::"
            self "self"
            super "super"
            "#,
        );

        check_keyword_completion(
            r"
            use a::<|>
            ",
            r#"
            self "self"
            super "super"
            "#,
        );

        check_keyword_completion(
            r"
            use a::{b, <|>}
            ",
            r#"
            self "self"
            super "super"
            "#,
        );
    }
}