aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-06 19:54:36 +0000
committerGitHub <[email protected]>2021-03-06 19:54:36 +0000
commit856c2850cd4ab90b79ef6f9232e2bcfc04d8fde2 (patch)
treebb8e089adda7b848ba75b48b9afd3e6e8a45a429
parent71b8fb7c572eb658ee1136f086d6348aafba1e1d (diff)
parent5bb4aec05f547cce8d9752a0e856b9b91e911643 (diff)
Merge #7865
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 <[email protected]>
-rw-r--r--crates/ide_assists/src/handlers/replace_string_with_char.rs38
1 files changed, 37 insertions, 1 deletions
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) -
31 "Replace string with char", 31 "Replace string with char",
32 target, 32 target,
33 |edit| { 33 |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));
35 }, 37 },
36 ) 38 )
37} 39}
@@ -134,4 +136,38 @@ mod tests {
134 "##, 136 "##,
135 ) 137 )
136 } 138 }
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 }
137} 173}