]> git.lizzy.rs Git - rust.git/blob - crates/ra_ide_api/src/completion/complete_path.rs
Rename Type => TypeAlias
[rust.git] / crates / ra_ide_api / src / completion / complete_path.rs
1 use hir::Resolution;
2 use ra_syntax::AstNode;
3 use test_utils::tested_by;
4
5 use crate::completion::{Completions, CompletionContext};
6
7 pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
8     let path = match &ctx.path_prefix {
9         Some(path) => path.clone(),
10         _ => return,
11     };
12     let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
13         Some(Resolution::Def(def)) => def,
14         _ => return,
15     };
16     match def {
17         hir::ModuleDef::Module(module) => {
18             let module_scope = module.scope(ctx.db);
19             for (name, res) in module_scope.entries() {
20                 if Some(module) == ctx.module {
21                     if let Some(import) = res.import {
22                         let path = module.import_source(ctx.db, import);
23                         if path.syntax().range().contains_inclusive(ctx.offset) {
24                             // for `use self::foo<|>`, don't suggest `foo` as a completion
25                             tested_by!(dont_complete_current_use);
26                             continue;
27                         }
28                     }
29                 }
30                 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
31             }
32         }
33         hir::ModuleDef::Enum(e) => {
34             for variant in e.variants(ctx.db) {
35                 acc.add_enum_variant(ctx, variant);
36             }
37         }
38         hir::ModuleDef::Struct(s) => {
39             let ty = s.ty(ctx.db);
40             ty.iterate_impl_items(ctx.db, |item| {
41                 match item {
42                     hir::ImplItem::Method(func) => {
43                         let sig = func.signature(ctx.db);
44                         if !sig.has_self_param() {
45                             acc.add_function(ctx, func);
46                         }
47                     }
48                     hir::ImplItem::Const(ct) => acc.add_const(ctx, ct),
49                     hir::ImplItem::TypeAlias(ty) => acc.add_type_alias(ctx, ty),
50                 }
51                 None::<()>
52             });
53         }
54         _ => return,
55     };
56 }
57
58 #[cfg(test)]
59 mod tests {
60     use test_utils::covers;
61
62     use crate::completion::{CompletionKind, check_completion, do_completion};
63
64     fn check_reference_completion(code: &str, expected_completions: &str) {
65         check_completion(code, expected_completions, CompletionKind::Reference);
66     }
67
68     #[test]
69     fn dont_complete_current_use() {
70         covers!(dont_complete_current_use);
71         let completions = do_completion(r"use self::foo<|>;", CompletionKind::Reference);
72         assert!(completions.is_empty());
73     }
74
75     #[test]
76     fn completes_mod_with_docs() {
77         check_reference_completion(
78             "mod_with_docs",
79             r"
80             use self::my<|>;
81
82             /// Some simple
83             /// docs describing `mod my`.
84             mod my {
85                 struct Bar;
86             }
87             ",
88         );
89     }
90
91     #[test]
92     fn completes_use_item_starting_with_self() {
93         check_reference_completion(
94             "use_item_starting_with_self",
95             r"
96             use self::m::<|>;
97
98             mod m {
99                 struct Bar;
100             }
101             ",
102         );
103     }
104
105     #[test]
106     fn completes_use_item_starting_with_crate() {
107         check_reference_completion(
108             "use_item_starting_with_crate",
109             "
110             //- /lib.rs
111             mod foo;
112             struct Spam;
113             //- /foo.rs
114             use crate::Sp<|>
115             ",
116         );
117     }
118
119     #[test]
120     fn completes_nested_use_tree() {
121         check_reference_completion(
122             "nested_use_tree",
123             "
124             //- /lib.rs
125             mod foo;
126             struct Spam;
127             //- /foo.rs
128             use crate::{Sp<|>};
129             ",
130         );
131     }
132
133     #[test]
134     fn completes_deeply_nested_use_tree() {
135         check_reference_completion(
136             "deeply_nested_use_tree",
137             "
138             //- /lib.rs
139             mod foo;
140             pub mod bar {
141                 pub mod baz {
142                     pub struct Spam;
143                 }
144             }
145             //- /foo.rs
146             use crate::{bar::{baz::Sp<|>}};
147             ",
148         );
149     }
150
151     #[test]
152     fn completes_enum_variant() {
153         check_reference_completion(
154             "enum_variant",
155             "
156             //- /lib.rs
157             /// An enum
158             enum E {
159                 /// Foo Variant
160                 Foo,
161                 /// Bar Variant with i32
162                 Bar(i32)
163             }
164             fn foo() { let _ = E::<|> }
165             ",
166         );
167     }
168
169     #[test]
170     fn completes_enum_variant_with_details() {
171         check_reference_completion(
172             "enum_variant_with_details",
173             "
174             //- /lib.rs
175             struct S { field: u32 }
176             /// An enum
177             enum E {
178                 /// Foo Variant (empty)
179                 Foo,
180                 /// Bar Variant with i32 and u32
181                 Bar(i32, u32),
182                 ///
183                 S(S),
184             }
185             fn foo() { let _ = E::<|> }
186             ",
187         );
188     }
189
190     #[test]
191     fn completes_struct_associated_method() {
192         check_reference_completion(
193             "struct_associated_method",
194             "
195             //- /lib.rs
196             /// A Struct
197             struct S;
198
199             impl S {
200                 /// An associated method
201                 fn m() { }
202             }
203
204             fn foo() { let _ = S::<|> }
205             ",
206         );
207     }
208
209     #[test]
210     fn completes_struct_associated_const() {
211         check_reference_completion(
212             "struct_associated_const",
213             "
214             //- /lib.rs
215             /// A Struct
216             struct S;
217
218             impl S {
219                 /// An associated const
220                 const C: i32 = 42;
221             }
222
223             fn foo() { let _ = S::<|> }
224             ",
225         );
226     }
227
228     #[test]
229     fn completes_struct_associated_type() {
230         check_reference_completion(
231             "struct_associated_type",
232             "
233             //- /lib.rs
234             /// A Struct
235             struct S;
236
237             impl S {
238                 /// An associated type
239                 type T = i32;
240             }
241
242             fn foo() { let _ = S::<|> }
243             ",
244         );
245     }
246
247     #[test]
248     fn completes_use_paths_across_crates() {
249         check_reference_completion(
250             "completes_use_paths_across_crates",
251             "
252             //- /main.rs
253             use foo::<|>;
254
255             //- /foo/lib.rs
256             pub mod bar {
257                 pub struct S;
258             }
259             ",
260         );
261     }
262 }