Skip to content

Commit 285f09c

Browse files
unvalleyVeykril
authored andcommitted
feat: extract only expressions from format string
1 parent 29f3d7d commit 285f09c

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,14 @@ pub(crate) fn extract_expressions_from_format_string(
121121
let mut placeholder_idx = 1;
122122

123123
for extracted_args in extracted_args {
124-
// remove expr from format string
125-
args.push_str(", ");
126-
127124
match extracted_args {
128-
Arg::Ident(s) | Arg::Expr(s) => {
125+
Arg::Expr(s)=> {
126+
args.push_str(", ");
129127
// insert arg
130128
args.push_str(&s);
131129
}
132130
Arg::Placeholder => {
131+
args.push_str(", ");
133132
// try matching with existing argument
134133
match existing_args.next() {
135134
Some(ea) => {
@@ -142,6 +141,7 @@ pub(crate) fn extract_expressions_from_format_string(
142141
}
143142
}
144143
}
144+
Arg::Ident(_s) => (),
145145
}
146146
}
147147

@@ -292,6 +292,29 @@ fn main() {
292292
fn main() {
293293
print!("My name is {} {}"$0, stringify!(Paperino), x + x)
294294
}
295+
"#,
296+
),
297+
);
298+
}
299+
300+
#[test]
301+
fn extract_only_expressions() {
302+
check_assist(
303+
extract_expressions_from_format_string,
304+
&add_macro_decl(
305+
r#"
306+
fn main() {
307+
let var = 1 + 1;
308+
print!("foobar {var} {var:?} {x$0 + x}")
309+
}
310+
"#,
311+
),
312+
&add_macro_decl(
313+
r#"
314+
fn main() {
315+
let var = 1 + 1;
316+
print!("foobar {var} {var:?} {}"$0, x + x)
317+
}
295318
"#,
296319
),
297320
);

0 commit comments

Comments
 (0)