aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorDomantas Jadenkus <[email protected]>2021-03-03 21:20:18 +0000
committerDomantas Jadenkus <[email protected]>2021-03-03 21:20:18 +0000
commit5bb4aec05f547cce8d9752a0e856b9b91e911643 (patch)
treee32289c9870fe39e12552aefd7cd033ade3d89fe /crates
parentd0fa7abc5044471b951149aa35244620db847ff9 (diff)
preserve escape sequences when replacing string with char
Diffstat (limited to 'crates')
-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}