about summary refs log tree commit diff
diff options
context:
space:
mode:
authorDropDemBits <r3usrlnd@gmail.com>2024-03-04 20:42:59 -0500
committerDropDemBits <r3usrlnd@gmail.com>2024-03-07 14:20:23 -0500
commit1f37e5ac9aab5e2a6673f5c52ffaec5260355d09 (patch)
treeab6699dbb68497f8ddb2782dada1ad56d4e27c73
parent9f14343f9ee24f53f17492c5f9b653427e2ad15e (diff)
downloadrust-1f37e5ac9aab5e2a6673f5c52ffaec5260355d09.tar.gz
rust-1f37e5ac9aab5e2a6673f5c52ffaec5260355d09.zip
fix: Don't escape `\` and `$` in "Extract format expressions" assist
-rw-r--r--crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs18
-rw-r--r--crates/ide-db/src/syntax_helpers/format_string_exprs.rs22
2 files changed, 23 insertions, 17 deletions
diff --git a/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs b/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs
index 9d72d3af096..2725a97de8e 100644
--- a/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs
+++ b/crates/ide-assists/src/handlers/extract_expressions_from_format_string.rs
@@ -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);
+}
+            "#,
+        );
+    }
 }
diff --git a/crates/ide-db/src/syntax_helpers/format_string_exprs.rs b/crates/ide-db/src/syntax_helpers/format_string_exprs.rs
index 49594aee9f3..93750a93b32 100644
--- a/crates/ide-db/src/syntax_helpers/format_string_exprs.rs
+++ b/crates/ide-db/src/syntax_helpers/format_string_exprs.rs
@@ -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, '}') => {
@@ -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
@@ -172,9 +166,6 @@ 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, '}') => {
@@ -182,9 +173,6 @@ pub fn parse_format_exprs(input: &str) -> Result<(String, Vec<Arg>), ()> {
                 state = State::NotArg;
             }
             (State::FormatOpts, _) => {
-                if matches!(chr, '\\' | '$') {
-                    output.push('\\');
-                }
                 output.push(chr);
             }
         }
@@ -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"]]),