aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_path.rs
blob: 0b9948d4b423818c2baa6ee8223e539893aaf3e0 (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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
use join_to_string::join;

use hir::{Docs, Resolution};

use crate::{
    completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
};

pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
    let path = match &ctx.path_prefix {
        Some(path) => path.clone(),
        _ => return,
    };
    let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
        Some(Resolution::Def(def)) => def,
        _ => return,
    };
    match def {
        hir::ModuleDef::Module(module) => {
            let module_scope = module.scope(ctx.db);
            for (name, res) in module_scope.entries() {
                CompletionItem::new(
                    CompletionKind::Reference,
                    ctx.source_range(),
                    name.to_string(),
                )
                .from_resolution(ctx, &res.def.map(hir::Resolution::Def))
                .add_to(acc);
            }
        }
        hir::ModuleDef::Enum(e) => {
            e.variants(ctx.db).into_iter().for_each(|variant| {
                if let Some(name) = variant.name(ctx.db) {
                    let detail_types = variant
                        .fields(ctx.db)
                        .into_iter()
                        .map(|field| field.ty(ctx.db));
                    let detail = join(detail_types)
                        .separator(", ")
                        .surround_with("(", ")")
                        .to_string();

                    CompletionItem::new(
                        CompletionKind::Reference,
                        ctx.source_range(),
                        name.to_string(),
                    )
                    .kind(CompletionItemKind::EnumVariant)
                    .set_documentation(variant.docs(ctx.db))
                    .set_detail(Some(detail))
                    .add_to(acc)
                }
            });
        }
        _ => return,
    };
}

#[cfg(test)]
mod tests {
    use crate::completion::CompletionKind;
    use crate::completion::completion_item::check_completion;

    fn check_reference_completion(code: &str, expected_completions: &str) {
        check_completion(code, expected_completions, CompletionKind::Reference);
    }

    #[test]
    #[ignore] // should not complete foo, which currently doesn't work
    fn dont_complete_current_use() {
        check_reference_completion(
            "dont_complete_current_use",
            r"
            use self::foo<|>;
            ",
        );
    }

    #[test]
    fn completes_mod_with_docs() {
        check_reference_completion(
            "mod_with_docs",
            r"
            use self::my<|>;

            /// Some simple
            /// docs describing `mod my`.
            mod my {
                struct Bar;
            }
            ",
        );
    }

    #[test]
    fn completes_use_item_starting_with_self() {
        check_reference_completion(
            "use_item_starting_with_self",
            r"
            use self::m::<|>;

            mod m {
                struct Bar;
            }
            ",
        );
    }

    #[test]
    fn completes_use_item_starting_with_crate() {
        check_reference_completion(
            "use_item_starting_with_crate",
            "
            //- /lib.rs
            mod foo;
            struct Spam;
            //- /foo.rs
            use crate::Sp<|>
            ",
        );
    }

    #[test]
    fn completes_nested_use_tree() {
        check_reference_completion(
            "nested_use_tree",
            "
            //- /lib.rs
            mod foo;
            struct Spam;
            //- /foo.rs
            use crate::{Sp<|>};
            ",
        );
    }

    #[test]
    fn completes_deeply_nested_use_tree() {
        check_reference_completion(
            "deeply_nested_use_tree",
            "
            //- /lib.rs
            mod foo;
            pub mod bar {
                pub mod baz {
                    pub struct Spam;
                }
            }
            //- /foo.rs
            use crate::{bar::{baz::Sp<|>}};
            ",
        );
    }

    #[test]
    fn completes_enum_variant() {
        check_reference_completion(
            "enum_variant",
            "
            //- /lib.rs
            /// An enum
            enum E {
                /// Foo Variant
                Foo,
                /// Bar Variant with i32
                Bar(i32)
            }
            fn foo() { let _ = E::<|> }
            ",
        );
    }

    #[test]
    fn completes_enum_variant_with_details() {
        check_reference_completion(
            "enum_variant_with_details",
            "
            //- /lib.rs
            struct S { field: u32 }
            /// An enum
            enum E {
                /// Foo Variant (empty)
                Foo,
                /// Bar Variant with i32 and u32
                Bar(i32, u32),
                ///
                S(S),
            }
            fn foo() { let _ = E::<|> }
            ",
        );
    }
}