Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9eabc2c

Browse files
unvalleyVeykril
authored andcommitted
fix: add_format_like_completions to handle no exprs
1 parent a310fc0 commit 9eabc2c

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

crates/ide-completion/src/completions/postfix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,12 +595,12 @@ fn main() {
595595
check_edit(
596596
"format",
597597
r#"fn main() { "{some_var:?}".$0 }"#,
598-
r#"fn main() { format!("{:?}", some_var) }"#,
598+
r#"fn main() { format!("{some_var:?}") }"#,
599599
);
600600
check_edit(
601601
"panic",
602602
r#"fn main() { "Panic with {a}".$0 }"#,
603-
r#"fn main() { panic!("Panic with {}", a) }"#,
603+
r#"fn main() { panic!("Panic with {a}") }"#,
604604
);
605605
check_edit(
606606
"println",

crates/ide-completion/src/completions/postfix/format_like.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ pub(crate) fn add_format_like_completions(
5454
if let Ok((out, exprs)) = parse_format_exprs(receiver_text.text()) {
5555
let exprs = with_placeholders(exprs);
5656
for (label, macro_name) in KINDS {
57-
let snippet = format!(r#"{macro_name}({out}, {})"#, exprs.join(", "));
57+
let snippet = if exprs.is_empty() {
58+
format!(r#"{}({})"#, macro_name, out)
59+
} else {
60+
format!(r#"{}({}, {})"#, macro_name, out, exprs.join(", "))
61+
};
5862

5963
postfix_snippet(label, macro_name, &snippet).add_to(acc);
6064
}
@@ -72,10 +76,9 @@ mod tests {
7276
("eprintln!", "{}", r#"eprintln!("{}", $1)"#),
7377
(
7478
"log::info!",
75-
"{} {expr} {} {2 + 2}",
76-
r#"log::info!("{} {} {} {}", $1, expr, $2, 2 + 2)"#,
79+
"{} {ident} {} {2 + 2}",
80+
r#"log::info!("{} {ident} {} {}", $1, $2, 2 + 2)"#,
7781
),
78-
("format!", "{expr:?}", r#"format!("{:?}", expr)"#),
7982
];
8083

8184
for (kind, input, output) in test_vector {
@@ -85,4 +88,18 @@ mod tests {
8588
assert_eq!(&snippet, output);
8689
}
8790
}
91+
92+
#[test]
93+
fn test_into_suggestion_no_epxrs() {
94+
let test_vector = &[
95+
("println!", "{ident}", r#"println!("{ident}")"#),
96+
("format!", "{ident:?}", r#"format!("{ident:?}")"#),
97+
];
98+
99+
for (kind, input, output) in test_vector {
100+
let (parsed_string, _exprs) = parse_format_exprs(input).unwrap();
101+
let snippet = format!(r#"{}("{}")"#, kind, parsed_string);
102+
assert_eq!(&snippet, output);
103+
}
104+
}
88105
}

0 commit comments

Comments
 (0)