7865: preserve escape sequences when replacing string with char r=Veykril a=jDomantas

Currently it replaces escape sequence with the actual value, which is very wrong for `"\n"`.

Co-authored-by: Domantas Jadenkus <djadenkus@gmail.com>
This commit is contained in:
bors[bot] 2021-03-06 19:54:36 +00:00 committed by GitHub
commit 856c2850cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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}');
}
"##,
)
}
}