aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/completion/complete_path.rs
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:48:48 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 19:48:48 +0000
commit46f74e33ca53a7897e9020d3de75cc76a6b89d79 (patch)
tree2bc001c8ecf58b49ac9a0da1f20d5644ce29fb3a /crates/ra_ide_api/src/completion/complete_path.rs
parent4f4f7933b1b7ff34f8633b1686b18b2d1b994c47 (diff)
parent0c62b1bb7a49bf527780ce1f8cade5eb4fbfdb2d (diff)
Merge #471
471: rename crates to match reality r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api/src/completion/complete_path.rs')
-rw-r--r--crates/ra_ide_api/src/completion/complete_path.rs128
1 files changed, 128 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs
new file mode 100644
index 000000000..4723a65a6
--- /dev/null
+++ b/crates/ra_ide_api/src/completion/complete_path.rs
@@ -0,0 +1,128 @@
1use crate::{
2 Cancelable,
3 completion::{CompletionItem, CompletionItemKind, Completions, CompletionKind, CompletionContext},
4};
5
6pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) -> 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)?.take_types() {
12 Some(it) => it,
13 None => return Ok(()),
14 };
15 match def_id.resolve(ctx.db)? {
16 hir::Def::Module(module) => {
17 let module_scope = module.scope(ctx.db)?;
18 module_scope.entries().for_each(|(name, res)| {
19 CompletionItem::new(CompletionKind::Reference, name.to_string())
20 .from_resolution(ctx, res)
21 .add_to(acc)
22 });
23 }
24 hir::Def::Enum(e) => e
25 .variants(ctx.db)?
26 .into_iter()
27 .for_each(|(name, _variant)| {
28 CompletionItem::new(CompletionKind::Reference, name.to_string())
29 .kind(CompletionItemKind::EnumVariant)
30 .add_to(acc)
31 }),
32 _ => return Ok(()),
33 };
34 Ok(())
35}
36
37#[cfg(test)]
38mod tests {
39 use crate::completion::{CompletionKind, check_completion};
40
41 fn check_reference_completion(code: &str, expected_completions: &str) {
42 check_completion(code, expected_completions, CompletionKind::Reference);
43 }
44
45 #[test]
46 fn completes_use_item_starting_with_self() {
47 check_reference_completion(
48 r"
49 use self::m::<|>;
50
51 mod m {
52 struct Bar;
53 }
54 ",
55 "Bar",
56 );
57 }
58
59 #[test]
60 fn completes_use_item_starting_with_crate() {
61 check_reference_completion(
62 "
63 //- /lib.rs
64 mod foo;
65 struct Spam;
66 //- /foo.rs
67 use crate::Sp<|>
68 ",
69 "Spam;foo",
70 );
71 }
72
73 #[test]
74 fn completes_nested_use_tree() {
75 check_reference_completion(
76 "
77 //- /lib.rs
78 mod foo;
79 struct Spam;
80 //- /foo.rs
81 use crate::{Sp<|>};
82 ",
83 "Spam;foo",
84 );
85 }
86
87 #[test]
88 fn completes_deeply_nested_use_tree() {
89 check_reference_completion(
90 "
91 //- /lib.rs
92 mod foo;
93 pub mod bar {
94 pub mod baz {
95 pub struct Spam;
96 }
97 }
98 //- /foo.rs
99 use crate::{bar::{baz::Sp<|>}};
100 ",
101 "Spam",
102 );
103 }
104
105 #[test]
106 fn completes_enum_variant() {
107 check_reference_completion(
108 "
109 //- /lib.rs
110 enum E { Foo, Bar(i32) }
111 fn foo() { let _ = E::<|> }
112 ",
113 "Foo;Bar",
114 );
115 }
116
117 #[test]
118 fn dont_render_function_parens_in_use_item() {
119 check_reference_completion(
120 "
121 //- /lib.rs
122 mod m { pub fn foo() {} }
123 use crate::m::f<|>;
124 ",
125 "foo",
126 )
127 }
128}