Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Don't escape \ and $ in "Extract format expressions" assist #16781

Merged
merged 1 commit into from
Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -274,4 +274,22 @@ fn main() {
"#,
);
}

#[test]
fn escaped_literals() {
check_assist(
extract_expressions_from_format_string,
r#"
//- minicore: fmt
fn main() {
print!("\n$ {x + 1}$0");
}
"#,
r#"
fn main() {
print!("\n$ {}"$0, x + 1);
}
"#,
);
}
}
22 changes: 5 additions & 17 deletions crates/ide-db/src/syntax_helpers/format_string_exprs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,6 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
state = State::MaybeIncorrect;
}
(State::NotArg, _) => {
if matches!(chr, '\\' | '$') {
output.push('\\');
}
output.push(chr);
}
(State::MaybeIncorrect, '}') => {
Expand Down Expand Up @@ -110,9 +107,6 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
state = State::FormatOpts;
}
(State::MaybeArg, _) => {
if matches!(chr, '\\' | '$') {
current_expr.push('\\');
}
current_expr.push(chr);

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

if matches!(chr, '\\' | '$') {
current_expr.push('\\');
}
current_expr.push(chr);
}
(State::FormatOpts, '}') => {
output.push(chr);
state = State::NotArg;
}
(State::FormatOpts, _) => {
if matches!(chr, '\\' | '$') {
output.push('\\');
}
output.push(chr);
}
}
Expand Down Expand Up @@ -217,15 +205,15 @@ mod tests {
fn format_str_parser() {
let test_vector = &[
("no expressions", expect![["no expressions"]]),
(r"no expressions with \$0$1", expect![r"no expressions with \\\$0\$1"]),
(r"no expressions with \$0$1", expect![r"no expressions with \$0$1"]),
("{expr} is {2 + 2}", expect![["{expr} is {}; 2 + 2"]]),
("{expr:?}", expect![["{expr:?}"]]),
("{expr:1$}", expect![[r"{expr:1\$}"]]),
("{:1$}", expect![[r"{:1\$}; $1"]]),
("{:>padding$}", expect![[r"{:>padding\$}; $1"]]),
("{expr:1$}", expect![[r"{expr:1$}"]]),
("{:1$}", expect![[r"{:1$}; $1"]]),
("{:>padding$}", expect![[r"{:>padding$}; $1"]]),
("{}, {}, {0}", expect![[r"{}, {}, {0}; $1, $2"]]),
("{}, {}, {0:b}", expect![[r"{}, {}, {0:b}; $1, $2"]]),
("{$0}", expect![[r"{}; \$0"]]),
("{$0}", expect![[r"{}; $0"]]),
("{malformed", expect![["-"]]),
("malformed}", expect![["-"]]),
("{{correct", expect![["{{correct"]]),
Expand Down
Loading