From 5bb4aec05f547cce8d9752a0e856b9b91e911643 Mon Sep 17 00:00:00 2001 From: Domantas Jadenkus Date: Wed, 3 Mar 2021 23:20:18 +0200 Subject: preserve escape sequences when replacing string with char --- .../src/handlers/replace_string_with_char.rs | 38 +++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/crates/ide_assists/src/handlers/replace_string_with_char.rs b/crates/ide_assists/src/handlers/replace_string_with_char.rs index 317318c24..303c0dcbe 100644 --- a/crates/ide_assists/src/handlers/replace_string_with_char.rs +++ b/crates/ide_assists/src/handlers/replace_string_with_char.rs @@ -31,7 +31,9 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) - "Replace string with char", target, |edit| { - edit.replace(token.syntax().text_range(), format!("'{}'", value)); + 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)); }, ) } @@ -134,4 +136,38 @@ mod tests { "##, ) } + + #[test] + fn replace_string_with_char_newline() { + check_assist( + replace_string_with_char, + r#" + fn f() { + find($0"\n"); + } + "#, + r##" + fn f() { + find('\n'); + } + "##, + ) + } + + #[test] + fn replace_string_with_char_unicode_escape() { + check_assist( + replace_string_with_char, + r#" + fn f() { + find($0"\u{7FFF}"); + } + "#, + r##" + fn f() { + find('\u{7FFF}'); + } + "##, + ) + } } -- cgit v1.2.3