Skip to content

Commit 48cb059

Browse files
committed
Auto merge of #16781 - DropDemBits:extract-format-args-escaping, r=Veykril
fix: Don't escape `\` and `$` in "Extract format expressions" assist Fixes #16745
2 parents 7f19beb + 1f37e5a commit 48cb059

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,4 +274,22 @@ fn main() {
274274
"#,
275275
);
276276
}
277+
278+
#[test]
279+
fn escaped_literals() {
280+
check_assist(
281+
extract_expressions_from_format_string,
282+
r#"
283+
//- minicore: fmt
284+
fn main() {
285+
print!("\n$ {x + 1}$0");
286+
}
287+
"#,
288+
r#"
289+
fn main() {
290+
print!("\n$ {}"$0, x + 1);
291+
}
292+
"#,
293+
);
294+
}
277295
}

crates/ide-db/src/syntax_helpers/format_string_exprs.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,6 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
7979
state = State::MaybeIncorrect;
8080
}
8181
(State::NotArg, _) => {
82-
if matches!(chr, '\\' | '$') {
83-
output.push('\\');
84-
}
8582
output.push(chr);
8683
}
8784
(State::MaybeIncorrect, '}') => {
@@ -110,9 +107,6 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
110107
state = State::FormatOpts;
111108
}
112109
(State::MaybeArg, _) => {
113-
if matches!(chr, '\\' | '$') {
114-
current_expr.push('\\');
115-
}
116110
current_expr.push(chr);
117111

118112
// While Rust uses the unicode sets of XID_start and XID_continue for Identifiers
@@ -172,19 +166,13 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
172166
state = State::Expr;
173167
}
174168

175-
if matches!(chr, '\\' | '$') {
176-
current_expr.push('\\');
177-
}
178169
current_expr.push(chr);
179170
}
180171
(State::FormatOpts, '}') => {
181172
output.push(chr);
182173
state = State::NotArg;
183174
}
184175
(State::FormatOpts, _) => {
185-
if matches!(chr, '\\' | '$') {
186-
output.push('\\');
187-
}
188176
output.push(chr);
189177
}
190178
}
@@ -217,15 +205,15 @@ mod tests {
217205
fn format_str_parser() {
218206
let test_vector = &[
219207
("no expressions", expect![["no expressions"]]),
220-
(r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]),
208+
(r"no expressions with \$0$1", expect![r"no expressions with \$0$1"]),
221209
("{expr} is {2 + 2}", expect![["{expr} is {}; 2 + 2"]]),
222210
("{expr:?}", expect![["{expr:?}"]]),
223-
("{expr:1$}", expect![[r"{expr:1\$}"]]),
224-
("{:1$}", expect![[r"{:1\$}; $1"]]),
225-
("{:>padding$}", expect![[r"{:>padding\$}; $1"]]),
211+
("{expr:1$}", expect![[r"{expr:1$}"]]),
212+
("{:1$}", expect![[r"{:1$}; $1"]]),
213+
("{:>padding$}", expect![[r"{:>padding$}; $1"]]),
226214
("{}, {}, {0}", expect![[r"{}, {}, {0}; $1, $2"]]),
227215
("{}, {}, {0:b}", expect![[r"{}, {}, {0:b}; $1, $2"]]),
228-
("{$0}", expect![[r"{}; \$0"]]),
216+
("{$0}", expect![[r"{}; $0"]]),
229217
("{malformed", expect![["-"]]),
230218
("malformed}", expect![["-"]]),
231219
("{{correct", expect![["{{correct"]]),

0 commit comments

Comments
 (0)