Skip to content

Commit 5bb4aec

Browse files
committed
preserve escape sequences when replacing string with char
1 parent d0fa7ab commit 5bb4aec

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

crates/ide_assists/src/handlers/replace_string_with_char.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
3131
"Replace string with char",
3232
target,
3333
|edit| {
34-
edit.replace(token.syntax().text_range(), format!("'{}'", value));
34+
let token_text = token.syntax().text();
35+
let inner_text = &token_text[1..token_text.len() - 1];
36+
edit.replace(token.syntax().text_range(), format!("'{}'", inner_text));
3537
},
3638
)
3739
}
@@ -134,4 +136,38 @@ mod tests {
134136
"##,
135137
)
136138
}
139+
140+
#[test]
141+
fn replace_string_with_char_newline() {
142+
check_assist(
143+
replace_string_with_char,
144+
r#"
145+
fn f() {
146+
find($0"\n");
147+
}
148+
"#,
149+
r##"
150+
fn f() {
151+
find('\n');
152+
}
153+
"##,
154+
)
155+
}
156+
157+
#[test]
158+
fn replace_string_with_char_unicode_escape() {
159+
check_assist(
160+
replace_string_with_char,
161+
r#"
162+
fn f() {
163+
find($0"\u{7FFF}");
164+
}
165+
"#,
166+
r##"
167+
fn f() {
168+
find('\u{7FFF}');
169+
}
170+
"##,
171+
)
172+
}
137173
}

0 commit comments

Comments
 (0)