fix: add_format_like_completions to handle no exprs

This commit is contained in:
unvalley 2022-11-29 01:39:27 +09:00 committed by Lukas Wirth
parent a310fc0cd5
commit 9eabc2cde8
2 changed files with 23 additions and 6 deletions

View file

@ -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",

View file

@ -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);
}
}
}