diff options
Diffstat (limited to 'crates/completion')
-rw-r--r-- | crates/completion/src/completions/complete_magic.rs | 64 |
1 files changed, 15 insertions, 49 deletions
diff --git a/crates/completion/src/completions/complete_magic.rs b/crates/completion/src/completions/complete_magic.rs index 9242b860c..15af2190d 100644 --- a/crates/completion/src/completions/complete_magic.rs +++ b/crates/completion/src/completions/complete_magic.rs | |||
@@ -10,6 +10,7 @@ use crate::{context::CompletionContext, item::CompletionKind, CompletionItem, Co | |||
10 | 10 | ||
11 | use super::Completions; | 11 | use super::Completions; |
12 | 12 | ||
13 | // TODO kb when typing, completes partial results, need to rerun manually to see the proper ones | ||
13 | pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | 14 | pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { |
14 | if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { | 15 | if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { |
15 | return None; | 16 | return None; |
@@ -19,7 +20,7 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
19 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; | 20 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; |
20 | 21 | ||
21 | // TODO kb consider heuristics, such as "don't show `hash_map` import if `HashMap` is the import for completion" | 22 | // TODO kb consider heuristics, such as "don't show `hash_map` import if `HashMap` is the import for completion" |
22 | // TODO kb module functions are not completed, consider `std::io::stdin` one | 23 | // also apply completion ordering |
23 | let potential_import_name = ctx.token.to_string(); | 24 | let potential_import_name = ctx.token.to_string(); |
24 | 25 | ||
25 | let possible_imports = ctx | 26 | let possible_imports = ctx |
@@ -38,6 +39,7 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
38 | builder.replace(anchor.syntax().text_range(), correct_qualifier); | 39 | builder.replace(anchor.syntax().text_range(), correct_qualifier); |
39 | 40 | ||
40 | // TODO kb: assists already have the merge behaviour setting, need to unite both | 41 | // TODO kb: assists already have the merge behaviour setting, need to unite both |
42 | // also consider a settings toggle for this particular feature? | ||
41 | let rewriter = | 43 | let rewriter = |
42 | insert_use(&import_scope, mod_path_to_ast(&mod_path), Some(MergeBehaviour::Full)); | 44 | insert_use(&import_scope, mod_path_to_ast(&mod_path), Some(MergeBehaviour::Full)); |
43 | let old_ast = rewriter.rewrite_root()?; | 45 | let old_ast = rewriter.rewrite_root()?; |
@@ -60,37 +62,10 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
60 | 62 | ||
61 | #[cfg(test)] | 63 | #[cfg(test)] |
62 | mod tests { | 64 | mod tests { |
63 | use expect_test::{expect, Expect}; | 65 | use crate::test_utils::check_edit; |
64 | |||
65 | use crate::{ | ||
66 | item::CompletionKind, | ||
67 | test_utils::{check_edit, completion_list}, | ||
68 | }; | ||
69 | |||
70 | fn check(ra_fixture: &str, expect: Expect) { | ||
71 | let actual = completion_list(ra_fixture, CompletionKind::Magic); | ||
72 | expect.assert_eq(&actual) | ||
73 | } | ||
74 | 66 | ||
75 | #[test] | 67 | #[test] |
76 | fn function_magic_completion() { | 68 | fn function_magic_completion() { |
77 | check( | ||
78 | r#" | ||
79 | //- /lib.rs crate:dep | ||
80 | pub mod io { | ||
81 | pub fn stdin() {} | ||
82 | }; | ||
83 | |||
84 | //- /main.rs crate:main deps:dep | ||
85 | fn main() { | ||
86 | stdi<|> | ||
87 | } | ||
88 | "#, | ||
89 | expect![[r#" | ||
90 | st dep::io::stdin | ||
91 | "#]], | ||
92 | ); | ||
93 | |||
94 | check_edit( | 69 | check_edit( |
95 | "dep::io::stdin", | 70 | "dep::io::stdin", |
96 | r#" | 71 | r#" |
@@ -116,37 +91,28 @@ fn main() { | |||
116 | 91 | ||
117 | #[test] | 92 | #[test] |
118 | fn case_insensitive_magic_completion_works() { | 93 | fn case_insensitive_magic_completion_works() { |
119 | check( | ||
120 | r#" | ||
121 | //- /lib.rs crate:dep | ||
122 | pub struct TestStruct; | ||
123 | |||
124 | //- /main.rs crate:main deps:dep | ||
125 | fn main() { | ||
126 | teru<|> | ||
127 | } | ||
128 | "#, | ||
129 | expect![[r#" | ||
130 | st dep::TestStruct | ||
131 | "#]], | ||
132 | ); | ||
133 | |||
134 | check_edit( | 94 | check_edit( |
135 | "dep::TestStruct", | 95 | "dep::some_module::ThirdStruct", |
136 | r#" | 96 | r#" |
137 | //- /lib.rs crate:dep | 97 | //- /lib.rs crate:dep |
138 | pub struct TestStruct; | 98 | pub struct FirstStruct; |
99 | pub mod some_module { | ||
100 | pub struct SecondStruct; | ||
101 | pub struct ThirdStruct; | ||
102 | } | ||
139 | 103 | ||
140 | //- /main.rs crate:main deps:dep | 104 | //- /main.rs crate:main deps:dep |
105 | use dep::{FirstStruct, some_module::SecondStruct}; | ||
106 | |||
141 | fn main() { | 107 | fn main() { |
142 | teru<|> | 108 | this<|> |
143 | } | 109 | } |
144 | "#, | 110 | "#, |
145 | r#" | 111 | r#" |
146 | use dep::TestStruct; | 112 | use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}}; |
147 | 113 | ||
148 | fn main() { | 114 | fn main() { |
149 | TestStruct | 115 | ThirdStruct |
150 | } | 116 | } |
151 | "#, | 117 | "#, |
152 | ); | 118 | ); |