]> git.lizzy.rs Git - rust.git/blobdiff - crates/ide_assists/src/handlers/replace_string_with_char.rs
Add macro test
[rust.git] / crates / ide_assists / src / handlers / replace_string_with_char.rs
index 303c0dcbebfcba22bb8377f2a078c3226739c42d..634b9c0b7915753700bee65f6ceabd7eb79428f7 100644 (file)
@@ -25,15 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
     if value.chars().take(2).count() != 1 {
         return None;
     }
+    let quote_offets = token.quote_offsets()?;
 
     acc.add(
         AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
         "Replace string with char",
         target,
         |edit| {
-            let token_text = token.syntax().text();
-            let inner_text = &token_text[1..token_text.len() - 1];
-            edit.replace(token.syntax().text_range(), format!("'{}'", inner_text));
+            let (left, right) = quote_offets.quotes;
+            edit.replace(left, String::from('\''));
+            edit.replace(right, String::from('\''));
         },
     )
 }
@@ -49,10 +50,10 @@ fn replace_string_with_char_target() {
         check_assist_target(
             replace_string_with_char,
             r#"
-            fn f() {
-                let s = "$0c";
-            }
-            "#,
+fn f() {
+    let s = "$0c";
+}
+"#,
             r#""c""#,
         );
     }
@@ -62,15 +63,15 @@ fn replace_string_with_char_assist() {
         check_assist(
             replace_string_with_char,
             r#"
-    fn f() {
-        let s = "$0c";
-    }
-    "#,
+fn f() {
+    let s = "$0c";
+}
+"#,
             r##"
-    fn f() {
-        let s = 'c';
-    }
-    "##,
+fn f() {
+    let s = 'c';
+}
+"##,
         )
     }
 
@@ -79,15 +80,15 @@ fn replace_string_with_char_assist_with_emoji() {
         check_assist(
             replace_string_with_char,
             r#"
-    fn f() {
-        let s = "$0😀";
-    }
-    "#,
+fn f() {
+    let s = "$0😀";
+}
+"#,
             r##"
-    fn f() {
-        let s = '😀';
-    }
-    "##,
+fn f() {
+    let s = '😀';
+}
+"##,
         )
     }
 
@@ -96,10 +97,10 @@ fn replace_string_with_char_assist_not_applicable() {
         check_assist_not_applicable(
             replace_string_with_char,
             r#"
-    fn f() {
-        let s = "$0test";
-    }
-    "#,
+fn f() {
+    let s = "$0test";
+}
+"#,
         )
     }
 
@@ -108,15 +109,15 @@ fn replace_string_with_char_works_inside_macros() {
         check_assist(
             replace_string_with_char,
             r#"
-                fn f() {
-                    format!($0"x", 92)
-                }
-                "#,
+fn f() {
+    format!($0"x", 92)
+}
+"#,
             r##"
-                fn f() {
-                    format!('x', 92)
-                }
-                "##,
+fn f() {
+    format!('x', 92)
+}
+"##,
         )
     }
 
@@ -125,15 +126,15 @@ fn replace_string_with_char_works_func_args() {
         check_assist(
             replace_string_with_char,
             r#"
-                fn f() {
-                    find($0"x");
-                }
-                "#,
+fn f() {
+    find($0"x");
+}
+"#,
             r##"
-                fn f() {
-                    find('x');
-                }
-                "##,
+fn f() {
+    find('x');
+}
+"##,
         )
     }
 
@@ -142,15 +143,15 @@ fn replace_string_with_char_newline() {
         check_assist(
             replace_string_with_char,
             r#"
-                fn f() {
-                    find($0"\n");
-                }
-                "#,
+fn f() {
+    find($0"\n");
+}
+"#,
             r##"
-                fn f() {
-                    find('\n');
-                }
-                "##,
+fn f() {
+    find('\n');
+}
+"##,
         )
     }
 
@@ -159,15 +160,32 @@ fn replace_string_with_char_unicode_escape() {
         check_assist(
             replace_string_with_char,
             r#"
-                fn f() {
-                    find($0"\u{7FFF}");
-                }
-                "#,
+fn f() {
+    find($0"\u{7FFF}");
+}
+"#,
+            r##"
+fn f() {
+    find('\u{7FFF}');
+}
+"##,
+        )
+    }
+
+    #[test]
+    fn replace_raw_string_with_char() {
+        check_assist(
+            replace_string_with_char,
             r##"
-                fn f() {
-                    find('\u{7FFF}');
-                }
-                "##,
+fn f() {
+    $0r#"X"#
+}
+"##,
+            r##"
+fn f() {
+    'X'
+}
+"##,
         )
     }
 }