diff options
author | Aleksey Kladov <[email protected]> | 2018-12-21 20:04:56 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-12-21 20:04:56 +0000 |
commit | 2136e75c0bce090d104bb5b5006e48e42fb22a0a (patch) | |
tree | d17fa5832a8e04f925f9d3b62c3c2bf23a2de714 /crates/ra_analysis/src/completion/complete_path.rs | |
parent | cbe67339df2bbcb17e12ad74e8b8cd53baffb9f7 (diff) |
move path completion to a separate component
Diffstat (limited to 'crates/ra_analysis/src/completion/complete_path.rs')
-rw-r--r-- | crates/ra_analysis/src/completion/complete_path.rs | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/crates/ra_analysis/src/completion/complete_path.rs b/crates/ra_analysis/src/completion/complete_path.rs new file mode 100644 index 000000000..d04503e46 --- /dev/null +++ b/crates/ra_analysis/src/completion/complete_path.rs | |||
@@ -0,0 +1,95 @@ | |||
1 | use crate::{ | ||
2 | completion::{CompletionItem, Completions, CompletionKind::*, SyntaxContext}, | ||
3 | Cancelable, | ||
4 | }; | ||
5 | |||
6 | pub(super) fn complete_path(acc: &mut Completions, ctx: &SyntaxContext) -> Cancelable<()> { | ||
7 | let (path, module) = match (&ctx.path_prefix, &ctx.module) { | ||
8 | (Some(path), Some(module)) => (path.clone(), module), | ||
9 | _ => return Ok(()), | ||
10 | }; | ||
11 | let def_id = match module.resolve_path(ctx.db, path)? { | ||
12 | None => return Ok(()), | ||
13 | Some(it) => it, | ||
14 | }; | ||
15 | let target_module = match def_id.resolve(ctx.db)? { | ||
16 | hir::Def::Module(it) => it, | ||
17 | _ => return Ok(()), | ||
18 | }; | ||
19 | let module_scope = target_module.scope(ctx.db)?; | ||
20 | module_scope.entries().for_each(|(name, _res)| { | ||
21 | CompletionItem::new(name.to_string()) | ||
22 | .kind(Reference) | ||
23 | .add_to(acc) | ||
24 | }); | ||
25 | Ok(()) | ||
26 | } | ||
27 | |||
28 | #[cfg(test)] | ||
29 | mod tests { | ||
30 | use crate::completion::{CompletionKind, check_completion}; | ||
31 | |||
32 | fn check_reference_completion(code: &str, expected_completions: &str) { | ||
33 | check_completion(code, expected_completions, CompletionKind::Reference); | ||
34 | } | ||
35 | |||
36 | #[test] | ||
37 | fn completes_use_item_starting_with_self() { | ||
38 | check_reference_completion( | ||
39 | r" | ||
40 | use self::m::<|>; | ||
41 | |||
42 | mod m { | ||
43 | struct Bar; | ||
44 | } | ||
45 | ", | ||
46 | "Bar", | ||
47 | ); | ||
48 | } | ||
49 | |||
50 | #[test] | ||
51 | fn completes_use_item_starting_with_crate() { | ||
52 | check_reference_completion( | ||
53 | " | ||
54 | //- /lib.rs | ||
55 | mod foo; | ||
56 | struct Spam; | ||
57 | //- /foo.rs | ||
58 | use crate::Sp<|> | ||
59 | ", | ||
60 | "Spam;foo", | ||
61 | ); | ||
62 | } | ||
63 | |||
64 | #[test] | ||
65 | fn completes_nested_use_tree() { | ||
66 | check_reference_completion( | ||
67 | " | ||
68 | //- /lib.rs | ||
69 | mod foo; | ||
70 | struct Spam; | ||
71 | //- /foo.rs | ||
72 | use crate::{Sp<|>}; | ||
73 | ", | ||
74 | "Spam;foo", | ||
75 | ); | ||
76 | } | ||
77 | |||
78 | #[test] | ||
79 | fn completes_deeply_nested_use_tree() { | ||
80 | check_reference_completion( | ||
81 | " | ||
82 | //- /lib.rs | ||
83 | mod foo; | ||
84 | pub mod bar { | ||
85 | pub mod baz { | ||
86 | pub struct Spam; | ||
87 | } | ||
88 | } | ||
89 | //- /foo.rs | ||
90 | use crate::{bar::{baz::Sp<|>}}; | ||
91 | ", | ||
92 | "Spam", | ||
93 | ); | ||
94 | } | ||
95 | } | ||