diff options
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_path.rs')
-rw-r--r-- | crates/ra_ide_api/src/completion/complete_path.rs | 104 |
1 files changed, 103 insertions, 1 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 91ca7525e..d337fe970 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs | |||
@@ -1,6 +1,6 @@ | |||
1 | use join_to_string::join; | 1 | use join_to_string::join; |
2 | use hir::{Docs, Resolution}; | 2 | use hir::{Docs, Resolution}; |
3 | use ra_syntax::AstNode; | 3 | use ra_syntax::{AstNode, ast::NameOwner}; |
4 | use test_utils::tested_by; | 4 | use test_utils::tested_by; |
5 | 5 | ||
6 | use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; | 6 | use crate::completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext}; |
@@ -58,6 +58,51 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) { | |||
58 | } | 58 | } |
59 | }); | 59 | }); |
60 | } | 60 | } |
61 | hir::ModuleDef::Struct(s) => { | ||
62 | let ty = s.ty(ctx.db); | ||
63 | ty.iterate_impl_items(ctx.db, |item| match item { | ||
64 | hir::ImplItem::Method(func) => { | ||
65 | let sig = func.signature(ctx.db); | ||
66 | if !sig.has_self_param() { | ||
67 | CompletionItem::new( | ||
68 | CompletionKind::Reference, | ||
69 | ctx.source_range(), | ||
70 | sig.name().to_string(), | ||
71 | ) | ||
72 | .from_function(ctx, func) | ||
73 | .kind(CompletionItemKind::Method) | ||
74 | .add_to(acc); | ||
75 | } | ||
76 | None::<()> | ||
77 | } | ||
78 | hir::ImplItem::Const(ct) => { | ||
79 | let source = ct.source(ctx.db); | ||
80 | if let Some(name) = source.1.name() { | ||
81 | CompletionItem::new( | ||
82 | CompletionKind::Reference, | ||
83 | ctx.source_range(), | ||
84 | name.text().to_string(), | ||
85 | ) | ||
86 | .from_const(ctx, ct) | ||
87 | .add_to(acc); | ||
88 | } | ||
89 | None::<()> | ||
90 | } | ||
91 | hir::ImplItem::Type(ty) => { | ||
92 | let source = ty.source(ctx.db); | ||
93 | if let Some(name) = source.1.name() { | ||
94 | CompletionItem::new( | ||
95 | CompletionKind::Reference, | ||
96 | ctx.source_range(), | ||
97 | name.text().to_string(), | ||
98 | ) | ||
99 | .from_type(ctx, ty) | ||
100 | .add_to(acc); | ||
101 | } | ||
102 | None::<()> | ||
103 | } | ||
104 | }); | ||
105 | } | ||
61 | _ => return, | 106 | _ => return, |
62 | }; | 107 | }; |
63 | } | 108 | } |
@@ -198,6 +243,63 @@ mod tests { | |||
198 | } | 243 | } |
199 | 244 | ||
200 | #[test] | 245 | #[test] |
246 | fn completes_struct_associated_method() { | ||
247 | check_reference_completion( | ||
248 | "struct_associated_method", | ||
249 | " | ||
250 | //- /lib.rs | ||
251 | /// A Struct | ||
252 | struct S; | ||
253 | |||
254 | impl S { | ||
255 | /// An associated method | ||
256 | fn m() { } | ||
257 | } | ||
258 | |||
259 | fn foo() { let _ = S::<|> } | ||
260 | ", | ||
261 | ); | ||
262 | } | ||
263 | |||
264 | #[test] | ||
265 | fn completes_struct_associated_const() { | ||
266 | check_reference_completion( | ||
267 | "struct_associated_const", | ||
268 | " | ||
269 | //- /lib.rs | ||
270 | /// A Struct | ||
271 | struct S; | ||
272 | |||
273 | impl S { | ||
274 | /// An associated const | ||
275 | const C: i32 = 42; | ||
276 | } | ||
277 | |||
278 | fn foo() { let _ = S::<|> } | ||
279 | ", | ||
280 | ); | ||
281 | } | ||
282 | |||
283 | #[test] | ||
284 | fn completes_struct_associated_type() { | ||
285 | check_reference_completion( | ||
286 | "struct_associated_type", | ||
287 | " | ||
288 | //- /lib.rs | ||
289 | /// A Struct | ||
290 | struct S; | ||
291 | |||
292 | impl S { | ||
293 | /// An associated type | ||
294 | type T = i32; | ||
295 | } | ||
296 | |||
297 | fn foo() { let _ = S::<|> } | ||
298 | ", | ||
299 | ); | ||
300 | } | ||
301 | |||
302 | #[test] | ||
201 | fn completes_use_paths_across_crates() { | 303 | fn completes_use_paths_across_crates() { |
202 | check_reference_completion( | 304 | check_reference_completion( |
203 | "completes_use_paths_across_crates", | 305 | "completes_use_paths_across_crates", |