Skip to content

Commit 76d0546

Browse files
committed
Use lookup table instead of enum for postfix completion kinds
1 parent b7ac540 commit 76d0546

File tree

1 file changed

+20
-62
lines changed

1 file changed

+20
-62
lines changed

crates/ide/src/completion/complete_postfix/format_like.rs

Lines changed: 20 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,18 @@ use crate::completion::{
2020
};
2121
use syntax::ast::{self, AstToken};
2222

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+
2335
pub(super) fn add_format_like_completions(
2436
acc: &mut Completions,
2537
ctx: &CompletionContext,
@@ -36,11 +48,10 @@ pub(super) fn add_format_like_completions(
3648
let mut parser = FormatStrParser::new(input);
3749

3850
if parser.parse().is_ok() {
39-
for kind in PostfixKind::all_suggestions() {
40-
let snippet = parser.into_suggestion(*kind);
41-
let (label, detail) = kind.into_description();
51+
for (label, macro_name) in KINDS {
52+
let snippet = parser.into_suggestion(macro_name);
4253

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);
4455
}
4556
}
4657
}
@@ -66,59 +77,6 @@ pub struct FormatStrParser {
6677
parsed: bool,
6778
}
6879

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-
12280
#[derive(Debug, Clone, Copy, PartialEq)]
12381
enum State {
12482
NotExpr,
@@ -235,11 +193,11 @@ impl FormatStrParser {
235193
Ok(())
236194
}
237195

238-
pub fn into_suggestion(&self, kind: PostfixKind) -> String {
196+
pub fn into_suggestion(&self, macro_name: &str) -> String {
239197
assert!(self.parsed, "Attempt to get a suggestion from not parsed expression");
240198

241199
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)
243201
}
244202
}
245203

@@ -300,13 +258,13 @@ mod tests {
300258
#[test]
301259
fn test_into_suggestion() {
302260
let test_vector = &[
303-
(PostfixKind::Println, "{}", r#"println!("{}", $1)"#),
261+
("println!", "{}", r#"println!("{}", $1)"#),
304262
(
305-
PostfixKind::LogInfo,
263+
"log::info!",
306264
"{} {expr} {} {2 + 2}",
307265
r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#,
308266
),
309-
(PostfixKind::Format, "{expr:?}", r#"format!("{:?}", expr)"#),
267+
("format!", "{expr:?}", r#"format!("{:?}", expr)"#),
310268
];
311269

312270
for (kind, input, output) in test_vector {

0 commit comments

Comments
 (0)