diff options
author | Kirill Bulatov <[email protected]> | 2020-11-10 10:00:42 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-11-16 19:19:06 +0000 |
commit | 1e458efe628215dfc07943f8dd39f66ac059d3de (patch) | |
tree | 4ad588f859be9cad26fb281dcfe3b39ad18ae099 /crates/completion/src/completions | |
parent | 6ab97244b88b180c1cafd5b47533bd4366a09177 (diff) |
Add braces to functions and macros
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r-- | crates/completion/src/completions/complete_magic.rs | 66 |
1 files changed, 59 insertions, 7 deletions
diff --git a/crates/completion/src/completions/complete_magic.rs b/crates/completion/src/completions/complete_magic.rs index 15af2190d..4cf21e19d 100644 --- a/crates/completion/src/completions/complete_magic.rs +++ b/crates/completion/src/completions/complete_magic.rs | |||
@@ -1,7 +1,8 @@ | |||
1 | //! TODO kb move this into the complete_unqualified_path when starts to work properly | 1 | //! TODO kb move this into the complete_unqualified_path when starts to work properly |
2 | 2 | ||
3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope, MergeBehaviour}; | 3 | use assists::utils::{insert_use, mod_path_to_ast, ImportScope, MergeBehaviour}; |
4 | use hir::Query; | 4 | use either::Either; |
5 | use hir::{db::HirDatabase, MacroDef, ModuleDef, Query}; | ||
5 | use itertools::Itertools; | 6 | use itertools::Itertools; |
6 | use syntax::{algo, AstNode}; | 7 | use syntax::{algo, AstNode}; |
7 | use text_edit::TextEdit; | 8 | use text_edit::TextEdit; |
@@ -28,14 +29,23 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
28 | // TODO kb use imports_locator instead? | 29 | // TODO kb use imports_locator instead? |
29 | .query_external_importables(ctx.db, Query::new(&potential_import_name).limit(40)) | 30 | .query_external_importables(ctx.db, Query::new(&potential_import_name).limit(40)) |
30 | .unique() | 31 | .unique() |
31 | .filter_map(|import_candidate| match import_candidate { | 32 | .filter_map(|import_candidate| { |
32 | either::Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), | 33 | let use_path = match import_candidate { |
33 | either::Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), | 34 | Either::Left(module_def) => current_module.find_use_path(ctx.db, module_def), |
35 | Either::Right(macro_def) => current_module.find_use_path(ctx.db, macro_def), | ||
36 | }?; | ||
37 | // TODO kb need to omit braces when there are some already. | ||
38 | // maybe remove braces completely? | ||
39 | Some((use_path, additional_completion(ctx.db, import_candidate))) | ||
34 | }) | 40 | }) |
35 | .filter_map(|mod_path| { | 41 | .filter_map(|(mod_path, additional_completion)| { |
36 | let mut builder = TextEdit::builder(); | 42 | let mut builder = TextEdit::builder(); |
37 | 43 | ||
38 | let correct_qualifier = mod_path.segments.last()?.to_string(); | 44 | let correct_qualifier = format!( |
45 | "{}{}", | ||
46 | mod_path.segments.last()?, | ||
47 | additional_completion.unwrap_or_default() | ||
48 | ); | ||
39 | builder.replace(anchor.syntax().text_range(), correct_qualifier); | 49 | builder.replace(anchor.syntax().text_range(), correct_qualifier); |
40 | 50 | ||
41 | // TODO kb: assists already have the merge behaviour setting, need to unite both | 51 | // TODO kb: assists already have the merge behaviour setting, need to unite both |
@@ -60,6 +70,21 @@ pub(crate) fn complete_magic(acc: &mut Completions, ctx: &CompletionContext) -> | |||
60 | Some(()) | 70 | Some(()) |
61 | } | 71 | } |
62 | 72 | ||
73 | fn additional_completion( | ||
74 | db: &dyn HirDatabase, | ||
75 | import_candidate: Either<ModuleDef, MacroDef>, | ||
76 | ) -> Option<String> { | ||
77 | match import_candidate { | ||
78 | Either::Left(ModuleDef::Function(_)) => Some("()".to_string()), | ||
79 | Either::Right(macro_def) => { | ||
80 | let (left_brace, right_brace) = | ||
81 | crate::render::macro_::guess_macro_braces(db, macro_def); | ||
82 | Some(format!("!{}{}", left_brace, right_brace)) | ||
83 | } | ||
84 | _ => None, | ||
85 | } | ||
86 | } | ||
87 | |||
63 | #[cfg(test)] | 88 | #[cfg(test)] |
64 | mod tests { | 89 | mod tests { |
65 | use crate::test_utils::check_edit; | 90 | use crate::test_utils::check_edit; |
@@ -83,7 +108,34 @@ fn main() { | |||
83 | use dep::io::stdin; | 108 | use dep::io::stdin; |
84 | 109 | ||
85 | fn main() { | 110 | fn main() { |
86 | stdin | 111 | stdin() |
112 | } | ||
113 | "#, | ||
114 | ); | ||
115 | } | ||
116 | |||
117 | #[test] | ||
118 | fn macro_magic_completion() { | ||
119 | check_edit( | ||
120 | "dep::macro_with_curlies", | ||
121 | r#" | ||
122 | //- /lib.rs crate:dep | ||
123 | /// Please call me as macro_with_curlies! {} | ||
124 | #[macro_export] | ||
125 | macro_rules! macro_with_curlies { | ||
126 | () => {} | ||
127 | } | ||
128 | |||
129 | //- /main.rs crate:main deps:dep | ||
130 | fn main() { | ||
131 | curli<|> | ||
132 | } | ||
133 | "#, | ||
134 | r#" | ||
135 | use dep::macro_with_curlies; | ||
136 | |||
137 | fn main() { | ||
138 | macro_with_curlies! {} | ||
87 | } | 139 | } |
88 | "#, | 140 | "#, |
89 | ); | 141 | ); |