diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide/src/completion/complete_postfix/format_like.rs | 82 |
1 files changed, 20 insertions, 62 deletions
diff --git a/crates/ide/src/completion/complete_postfix/format_like.rs b/crates/ide/src/completion/complete_postfix/format_like.rs index 53cb3af39..b663ba171 100644 --- a/crates/ide/src/completion/complete_postfix/format_like.rs +++ b/crates/ide/src/completion/complete_postfix/format_like.rs | |||
@@ -20,6 +20,18 @@ use crate::completion::{ | |||
20 | }; | 20 | }; |
21 | use syntax::ast::{self, AstToken}; | 21 | use syntax::ast::{self, AstToken}; |
22 | 22 | ||
23 | /// Mapping ("postfix completion item" => "macro to use") | ||
24 | static KINDS: &[(&str, &str)] = &[ | ||
25 | ("fmt", "format!"), | ||
26 | ("panic", "panic!"), | ||
27 | ("println", "println!"), | ||
28 | ("logd", "log::debug!"), | ||
29 | ("logt", "log::trace!"), | ||
30 | ("logi", "log::info!"), | ||
31 | ("logw", "log::warn!"), | ||
32 | ("loge", "log::error!"), | ||
33 | ]; | ||
34 | |||
23 | pub(super) fn add_format_like_completions( | 35 | pub(super) fn add_format_like_completions( |
24 | acc: &mut Completions, | 36 | acc: &mut Completions, |
25 | ctx: &CompletionContext, | 37 | ctx: &CompletionContext, |
@@ -36,11 +48,10 @@ pub(super) fn add_format_like_completions( | |||
36 | let mut parser = FormatStrParser::new(input); | 48 | let mut parser = FormatStrParser::new(input); |
37 | 49 | ||
38 | if parser.parse().is_ok() { | 50 | if parser.parse().is_ok() { |
39 | for kind in PostfixKind::all_suggestions() { | 51 | for (label, macro_name) in KINDS { |
40 | let snippet = parser.into_suggestion(*kind); | 52 | let snippet = parser.into_suggestion(macro_name); |
41 | let (label, detail) = kind.into_description(); | ||
42 | 53 | ||
43 | postfix_snippet(ctx, cap, &dot_receiver, label, detail, &snippet).add_to(acc); | 54 | postfix_snippet(ctx, cap, &dot_receiver, label, macro_name, &snippet).add_to(acc); |
44 | } | 55 | } |
45 | } | 56 | } |
46 | } | 57 | } |
@@ -66,59 +77,6 @@ pub struct FormatStrParser { | |||
66 | parsed: bool, | 77 | parsed: bool, |
67 | } | 78 | } |
68 | 79 | ||
69 | #[derive(Debug, Clone, Copy)] | ||
70 | pub enum PostfixKind { | ||
71 | Format, | ||
72 | Panic, | ||
73 | Println, | ||
74 | LogDebug, | ||
75 | LogTrace, | ||
76 | LogInfo, | ||
77 | LogWarn, | ||
78 | LogError, | ||
79 | } | ||
80 | |||
81 | impl PostfixKind { | ||
82 | pub fn all_suggestions() -> &'static [PostfixKind] { | ||
83 | &[ | ||
84 | Self::Format, | ||
85 | Self::Panic, | ||
86 | Self::Println, | ||
87 | Self::LogDebug, | ||
88 | Self::LogTrace, | ||
89 | Self::LogInfo, | ||
90 | Self::LogWarn, | ||
91 | Self::LogError, | ||
92 | ] | ||
93 | } | ||
94 | |||
95 | pub fn into_description(self) -> (&'static str, &'static str) { | ||
96 | match self { | ||
97 | Self::Format => ("fmt", "format!"), | ||
98 | Self::Panic => ("panic", "panic!"), | ||
99 | Self::Println => ("println", "println!"), | ||
100 | Self::LogDebug => ("logd", "log::debug!"), | ||
101 | Self::LogTrace => ("logt", "log::trace!"), | ||
102 | Self::LogInfo => ("logi", "log::info!"), | ||
103 | Self::LogWarn => ("logw", "log::warn!"), | ||
104 | Self::LogError => ("loge", "log::error!"), | ||
105 | } | ||
106 | } | ||
107 | |||
108 | pub fn into_macro_name(self) -> &'static str { | ||
109 | match self { | ||
110 | Self::Format => "format!", | ||
111 | Self::Panic => "panic!", | ||
112 | Self::Println => "println!", | ||
113 | Self::LogDebug => "log::debug!", | ||
114 | Self::LogTrace => "log::trace!", | ||
115 | Self::LogInfo => "log::info!", | ||
116 | Self::LogWarn => "log::warn!", | ||
117 | Self::LogError => "log::error!", | ||
118 | } | ||
119 | } | ||
120 | } | ||
121 | |||
122 | #[derive(Debug, Clone, Copy, PartialEq)] | 80 | #[derive(Debug, Clone, Copy, PartialEq)] |
123 | enum State { | 81 | enum State { |
124 | NotExpr, | 82 | NotExpr, |
@@ -235,11 +193,11 @@ impl FormatStrParser { | |||
235 | Ok(()) | 193 | Ok(()) |
236 | } | 194 | } |
237 | 195 | ||
238 | pub fn into_suggestion(&self, kind: PostfixKind) -> String { | 196 | pub fn into_suggestion(&self, macro_name: &str) -> String { |
239 | assert!(self.parsed, "Attempt to get a suggestion from not parsed expression"); | 197 | assert!(self.parsed, "Attempt to get a suggestion from not parsed expression"); |
240 | 198 | ||
241 | let expressions_as_string = self.extracted_expressions.join(", "); | 199 | let expressions_as_string = self.extracted_expressions.join(", "); |
242 | format!(r#"{}("{}", {})"#, kind.into_macro_name(), self.output, expressions_as_string) | 200 | format!(r#"{}("{}", {})"#, macro_name, self.output, expressions_as_string) |
243 | } | 201 | } |
244 | } | 202 | } |
245 | 203 | ||
@@ -300,13 +258,13 @@ mod tests { | |||
300 | #[test] | 258 | #[test] |
301 | fn test_into_suggestion() { | 259 | fn test_into_suggestion() { |
302 | let test_vector = &[ | 260 | let test_vector = &[ |
303 | (PostfixKind::Println, "{}", r#"println!("{}", $1)"#), | 261 | ("println!", "{}", r#"println!("{}", $1)"#), |
304 | ( | 262 | ( |
305 | PostfixKind::LogInfo, | 263 | "log::info!", |
306 | "{} {expr} {} {2 + 2}", | 264 | "{} {expr} {} {2 + 2}", |
307 | r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#, | 265 | r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#, |
308 | ), | 266 | ), |
309 | (PostfixKind::Format, "{expr:?}", r#"format!("{:?}", expr)"#), | 267 | ("format!", "{expr:?}", r#"format!("{:?}", expr)"#), |
310 | ]; | 268 | ]; |
311 | 269 | ||
312 | for (kind, input, output) in test_vector { | 270 | for (kind, input, output) in test_vector { |