Unnamed repository; edit this file 'description' to name the repository.
fix: add_format_like_completions to handle no exprs
unvalley 2023-01-09
parent a310fc0 · commit 9eabc2c
-rw-r--r--crates/ide-completion/src/completions/postfix.rs4
-rw-r--r--crates/ide-completion/src/completions/postfix/format_like.rs25
2 files changed, 23 insertions, 6 deletions
diff --git a/crates/ide-completion/src/completions/postfix.rs b/crates/ide-completion/src/completions/postfix.rs
index 3db400604b..f4f37d77d8 100644
--- a/crates/ide-completion/src/completions/postfix.rs
+++ b/crates/ide-completion/src/completions/postfix.rs
@@ -595,12 +595,12 @@ fn main() {
check_edit(
"format",
r#"fn main() { "{some_var:?}".$0 }"#,
- r#"fn main() { format!("{:?}", some_var) }"#,
+ r#"fn main() { format!("{some_var:?}") }"#,
);
check_edit(
"panic",
r#"fn main() { "Panic with {a}".$0 }"#,
- r#"fn main() { panic!("Panic with {}", a) }"#,
+ r#"fn main() { panic!("Panic with {a}") }"#,
);
check_edit(
"println",
diff --git a/crates/ide-completion/src/completions/postfix/format_like.rs b/crates/ide-completion/src/completions/postfix/format_like.rs
index d64d6379a9..dfcc78e923 100644
--- a/crates/ide-completion/src/completions/postfix/format_like.rs
+++ b/crates/ide-completion/src/completions/postfix/format_like.rs
@@ -54,7 +54,11 @@ pub(crate) fn add_format_like_completions(
if let Ok((out, exprs)) = parse_format_exprs(receiver_text.text()) {
let exprs = with_placeholders(exprs);
for (label, macro_name) in KINDS {
- let snippet = format!(r#"{macro_name}({out}, {})"#, exprs.join(", "));
+ let snippet = if exprs.is_empty() {
+ format!(r#"{}({})"#, macro_name, out)
+ } else {
+ format!(r#"{}({}, {})"#, macro_name, out, exprs.join(", "))
+ };
postfix_snippet(label, macro_name, &snippet).add_to(acc);
}
@@ -72,10 +76,9 @@ mod tests {
("eprintln!", "{}", r#"eprintln!("{}", $1)"#),
(
"log::info!",
- "{} {expr} {} {2 + 2}",
- r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#,
+ "{} {ident} {} {2 + 2}",
+ r#"log::info!("{} {ident} {} {}", $1, $2, 2 + 2)"#,
),
- ("format!", "{expr:?}", r#"format!("{:?}", expr)"#),
];
for (kind, input, output) in test_vector {
@@ -85,4 +88,18 @@ mod tests {
assert_eq!(&snippet, output);
}
}
+
+ #[test]
+ fn test_into_suggestion_no_epxrs() {
+ let test_vector = &[
+ ("println!", "{ident}", r#"println!("{ident}")"#),
+ ("format!", "{ident:?}", r#"format!("{ident:?}")"#),
+ ];
+
+ for (kind, input, output) in test_vector {
+ let (parsed_string, _exprs) = parse_format_exprs(input).unwrap();
+ let snippet = format!(r#"{}("{}")"#, kind, parsed_string);
+ assert_eq!(&snippet, output);
+ }
+ }
}