diff options
Diffstat (limited to 'crates/completion/src/completions/magic.rs')
-rw-r--r-- | crates/completion/src/completions/magic.rs | 151 |
1 files changed, 0 insertions, 151 deletions
diff --git a/crates/completion/src/completions/magic.rs b/crates/completion/src/completions/magic.rs deleted file mode 100644 index 0c4db0199..000000000 --- a/crates/completion/src/completions/magic.rs +++ /dev/null | |||
@@ -1,151 +0,0 @@ | |||
1 | //! TODO kb move this into the complete_unqualified_path when starts to work properly | ||
2 | |||
3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; | ||
4 | use either::Either; | ||
5 | use hir::{ModuleDef, ScopeDef}; | ||
6 | use ide_db::imports_locator; | ||
7 | use syntax::{algo, AstNode}; | ||
8 | |||
9 | use crate::{ | ||
10 | context::CompletionContext, | ||
11 | render::{render_resolution, RenderContext}, | ||
12 | }; | ||
13 | |||
14 | use super::Completions; | ||
15 | |||
16 | // TODO kb add a setting toggle for this feature? | ||
17 | pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | ||
18 | if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) { | ||
19 | return None; | ||
20 | } | ||
21 | let _p = profile::span("complete_magic"); | ||
22 | let current_module = ctx.scope.module()?; | ||
23 | let anchor = ctx.name_ref_syntax.as_ref()?; | ||
24 | let import_scope = ImportScope::find_insert_use_container(anchor.syntax(), &ctx.sema)?; | ||
25 | |||
26 | let potential_import_name = ctx.token.to_string(); | ||
27 | |||
28 | let possible_imports = | ||
29 | imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name, 400) | ||
30 | .filter_map(|import_candidate| match import_candidate { | ||
31 | // when completing outside the use declaration, modules are pretty useless | ||
32 | // and tend to bloat the completion suggestions a lot | ||
33 | Either::Left(ModuleDef::Module(_)) => None, | ||
34 | Either::Left(module_def) => Some(( | ||
35 | current_module.find_use_path(ctx.db, module_def)?, | ||
36 | ScopeDef::ModuleDef(module_def), | ||
37 | )), | ||
38 | Either::Right(macro_def) => Some(( | ||
39 | current_module.find_use_path(ctx.db, macro_def)?, | ||
40 | ScopeDef::MacroDef(macro_def), | ||
41 | )), | ||
42 | }) | ||
43 | .filter_map(|(mod_path, definition)| { | ||
44 | let mut resolution_with_missing_import = render_resolution( | ||
45 | RenderContext::new(ctx), | ||
46 | mod_path.segments.last()?.to_string(), | ||
47 | &definition, | ||
48 | )?; | ||
49 | |||
50 | let mut text_edits = | ||
51 | resolution_with_missing_import.text_edit().to_owned().into_builder(); | ||
52 | |||
53 | let rewriter = | ||
54 | insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); | ||
55 | let old_ast = rewriter.rewrite_root()?; | ||
56 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits); | ||
57 | |||
58 | resolution_with_missing_import.update_text_edit(text_edits.finish()); | ||
59 | |||
60 | Some(resolution_with_missing_import) | ||
61 | }); | ||
62 | |||
63 | acc.add_all(possible_imports); | ||
64 | Some(()) | ||
65 | } | ||
66 | |||
67 | #[cfg(test)] | ||
68 | mod tests { | ||
69 | use crate::test_utils::check_edit; | ||
70 | |||
71 | #[test] | ||
72 | fn function_magic_completion() { | ||
73 | check_edit( | ||
74 | "stdin", | ||
75 | r#" | ||
76 | //- /lib.rs crate:dep | ||
77 | pub mod io { | ||
78 | pub fn stdin() {} | ||
79 | }; | ||
80 | |||
81 | //- /main.rs crate:main deps:dep | ||
82 | fn main() { | ||
83 | stdi<|> | ||
84 | } | ||
85 | "#, | ||
86 | r#" | ||
87 | use dep::io::stdin; | ||
88 | |||
89 | fn main() { | ||
90 | stdin()$0 | ||
91 | } | ||
92 | "#, | ||
93 | ); | ||
94 | } | ||
95 | |||
96 | #[test] | ||
97 | fn macro_magic_completion() { | ||
98 | check_edit( | ||
99 | "macro_with_curlies!", | ||
100 | r#" | ||
101 | //- /lib.rs crate:dep | ||
102 | /// Please call me as macro_with_curlies! {} | ||
103 | #[macro_export] | ||
104 | macro_rules! macro_with_curlies { | ||
105 | () => {} | ||
106 | } | ||
107 | |||
108 | //- /main.rs crate:main deps:dep | ||
109 | fn main() { | ||
110 | curli<|> | ||
111 | } | ||
112 | "#, | ||
113 | r#" | ||
114 | use dep::macro_with_curlies; | ||
115 | |||
116 | fn main() { | ||
117 | macro_with_curlies! {$0} | ||
118 | } | ||
119 | "#, | ||
120 | ); | ||
121 | } | ||
122 | |||
123 | #[test] | ||
124 | fn case_insensitive_magic_completion_works() { | ||
125 | check_edit( | ||
126 | "ThirdStruct", | ||
127 | r#" | ||
128 | //- /lib.rs crate:dep | ||
129 | pub struct FirstStruct; | ||
130 | pub mod some_module { | ||
131 | pub struct SecondStruct; | ||
132 | pub struct ThirdStruct; | ||
133 | } | ||
134 | |||
135 | //- /main.rs crate:main deps:dep | ||
136 | use dep::{FirstStruct, some_module::SecondStruct}; | ||
137 | |||
138 | fn main() { | ||
139 | this<|> | ||
140 | } | ||
141 | "#, | ||
142 | r#" | ||
143 | use dep::{FirstStruct, some_module::{SecondStruct, ThirdStruct}}; | ||
144 | |||
145 | fn main() { | ||
146 | ThirdStruct | ||
147 | } | ||
148 | "#, | ||
149 | ); | ||
150 | } | ||
151 | } | ||