diff options
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r-- | crates/completion/src/completions/magic.rs | 82 |
1 files changed, 34 insertions, 48 deletions
diff --git a/crates/completion/src/completions/magic.rs b/crates/completion/src/completions/magic.rs index 272c9a354..ef0fc27ba 100644 --- a/crates/completion/src/completions/magic.rs +++ b/crates/completion/src/completions/magic.rs | |||
@@ -2,12 +2,14 @@ | |||
2 | 2 | ||
3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; | 3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope}; |
4 | use either::Either; | 4 | use either::Either; |
5 | use hir::{db::HirDatabase, MacroDef, ModuleDef}; | 5 | use hir::ScopeDef; |
6 | use ide_db::imports_locator; | 6 | use ide_db::imports_locator; |
7 | use syntax::{algo, AstNode}; | 7 | use syntax::{algo, AstNode}; |
8 | use text_edit::TextEdit; | ||
9 | 8 | ||
10 | use crate::{context::CompletionContext, item::CompletionKind, CompletionItem, CompletionItemKind}; | 9 | use crate::{ |
10 | context::CompletionContext, | ||
11 | render::{render_resolution, RenderContext}, | ||
12 | }; | ||
11 | 13 | ||
12 | use super::Completions; | 14 | use super::Completions; |
13 | 15 | ||
@@ -25,57 +27,41 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
25 | let possible_imports = | 27 | let possible_imports = |
26 | imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name) | 28 | imports_locator::find_similar_imports(&ctx.sema, ctx.krate?, &potential_import_name) |
27 | .filter_map(|import_candidate| { | 29 | .filter_map(|import_candidate| { |
28 | let use_path = match import_candidate { | 30 | Some(match import_candidate { |
29 | Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), | 31 | Either::Left(module_def) => ( |
30 | Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), | 32 | current_module.find_use_path(ctx.db, module_def)?, |
31 | }?; | 33 | ScopeDef::ModuleDef(module_def), |
32 | Some((use_path, additional_completion(ctx.db, import_candidate))) | 34 | ), |
35 | Either::Right(macro_def) => ( | ||
36 | current_module.find_use_path(ctx.db, macro_def)?, | ||
37 | ScopeDef::MacroDef(macro_def), | ||
38 | ), | ||
39 | }) | ||
33 | }) | 40 | }) |
34 | .filter_map(|(mod_path, additional_completion)| { | 41 | .filter_map(|(mod_path, definition)| { |
35 | let mut builder = TextEdit::builder(); | 42 | let mut resolution_with_missing_import = render_resolution( |
43 | RenderContext::new(ctx), | ||
44 | mod_path.segments.last()?.to_string(), | ||
45 | &definition, | ||
46 | )?; | ||
36 | 47 | ||
37 | let correct_qualifier = format!( | 48 | let mut text_edits = |
38 | "{}{}", | 49 | resolution_with_missing_import.text_edit().to_owned().into_builder(); |
39 | mod_path.segments.last()?, | ||
40 | additional_completion.unwrap_or_default() | ||
41 | ); | ||
42 | builder.replace(anchor.syntax().text_range(), correct_qualifier); | ||
43 | 50 | ||
44 | let rewriter = | 51 | let rewriter = |
45 | insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); | 52 | insert_use(&import_scope, mod_path_to_ast(&mod_path), ctx.config.merge); |
46 | let old_ast = rewriter.rewrite_root()?; | 53 | let old_ast = rewriter.rewrite_root()?; |
47 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut builder); | 54 | algo::diff(&old_ast, &rewriter.rewrite(&old_ast)).into_text_edit(&mut text_edits); |
48 | 55 | ||
49 | let completion_item: CompletionItem = CompletionItem::new( | 56 | resolution_with_missing_import.update_text_edit(text_edits.finish()); |
50 | CompletionKind::Magic, | 57 | |
51 | ctx.source_range(), | 58 | Some(resolution_with_missing_import) |
52 | mod_path.to_string(), | ||
53 | ) | ||
54 | .kind(CompletionItemKind::Struct) | ||
55 | .text_edit(builder.finish()) | ||
56 | .into(); | ||
57 | Some(completion_item) | ||
58 | }); | 59 | }); |
59 | acc.add_all(possible_imports); | ||
60 | 60 | ||
61 | acc.add_all(possible_imports); | ||
61 | Some(()) | 62 | Some(()) |
62 | } | 63 | } |
63 | 64 | ||
64 | fn additional_completion( | ||
65 | db: &dyn HirDatabase, | ||
66 | import_candidate: Either<ModuleDef, MacroDef>, | ||
67 | ) -> Option<String> { | ||
68 | match import_candidate { | ||
69 | Either::Left(ModuleDef::Function(_)) => Some("()".to_string()), | ||
70 | Either::Right(macro_def) => { | ||
71 | let (left_brace, right_brace) = | ||
72 | crate::render::macro_::guess_macro_braces(db, macro_def); | ||
73 | Some(format!("!{}{}", left_brace, right_brace)) | ||
74 | } | ||
75 | _ => None, | ||
76 | } | ||
77 | } | ||
78 | |||
79 | #[cfg(test)] | 65 | #[cfg(test)] |
80 | mod tests { | 66 | mod tests { |
81 | use crate::test_utils::check_edit; | 67 | use crate::test_utils::check_edit; |
@@ -83,7 +69,7 @@ mod tests { | |||
83 | #[test] | 69 | #[test] |
84 | fn function_magic_completion() { | 70 | fn function_magic_completion() { |
85 | check_edit( | 71 | check_edit( |
86 | "dep::io::stdin", | 72 | "stdin", |
87 | r#" | 73 | r#" |
88 | //- /lib.rs crate:dep | 74 | //- /lib.rs crate:dep |
89 | pub mod io { | 75 | pub mod io { |
@@ -99,7 +85,7 @@ fn main() { | |||
99 | use dep::io::stdin; | 85 | use dep::io::stdin; |
100 | 86 | ||
101 | fn main() { | 87 | fn main() { |
102 | stdin() | 88 | stdin()$0 |
103 | } | 89 | } |
104 | "#, | 90 | "#, |
105 | ); | 91 | ); |
@@ -108,7 +94,7 @@ fn main() { | |||
108 | #[test] | 94 | #[test] |
109 | fn macro_magic_completion() { | 95 | fn macro_magic_completion() { |
110 | check_edit( | 96 | check_edit( |
111 | "dep::macro_with_curlies", | 97 | "macro_with_curlies!", |
112 | r#" | 98 | r#" |
113 | //- /lib.rs crate:dep | 99 | //- /lib.rs crate:dep |
114 | /// Please call me as macro_with_curlies! {} | 100 | /// Please call me as macro_with_curlies! {} |
@@ -126,7 +112,7 @@ fn main() { | |||
126 | use dep::macro_with_curlies; | 112 | use dep::macro_with_curlies; |
127 | 113 | ||
128 | fn main() { | 114 | fn main() { |
129 | macro_with_curlies! {} | 115 | macro_with_curlies! {$0} |
130 | } | 116 | } |
131 | "#, | 117 | "#, |
132 | ); | 118 | ); |
@@ -135,7 +121,7 @@ fn main() { | |||
135 | #[test] | 121 | #[test] |
136 | fn case_insensitive_magic_completion_works() { | 122 | fn case_insensitive_magic_completion_works() { |
137 | check_edit( | 123 | check_edit( |
138 | "dep::some_module::ThirdStruct", | 124 | "ThirdStruct", |
139 | r#" | 125 | r#" |
140 | //- /lib.rs crate:dep | 126 | //- /lib.rs crate:dep |
141 | pub struct FirstStruct; | 127 | pub struct FirstStruct; |