diff options
-rw-r--r-- | crates/ra_assists/src/assists/raw_string.rs | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index e57f24e7c..dd8db0b7d 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -28,7 +28,25 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
28 | if error.is_err() { | 28 | if error.is_err() { |
29 | eprintln!("Error unescaping string"); | 29 | eprintln!("Error unescaping string"); |
30 | } else { | 30 | } else { |
31 | edit.replace(literal.syntax().text_range(), format!("r#\"{}\"#", unescaped)); | 31 | let mut max_hash_streak = 0; |
32 | unescaped.chars().fold(0, |acc, c| { | ||
33 | if c == '#' { | ||
34 | acc + 1 | ||
35 | } else { | ||
36 | if acc > max_hash_streak { | ||
37 | max_hash_streak = acc; | ||
38 | } | ||
39 | 0 | ||
40 | } | ||
41 | }); | ||
42 | let mut hashes = String::with_capacity(max_hash_streak + 1); | ||
43 | for _ in 0..hashes.capacity() { | ||
44 | hashes.push('#'); | ||
45 | } | ||
46 | edit.replace( | ||
47 | literal.syntax().text_range(), | ||
48 | format!("r{}\"{}\"{}", hashes, unescaped, hashes), | ||
49 | ); | ||
32 | } | 50 | } |
33 | }); | 51 | }); |
34 | ctx.build() | 52 | ctx.build() |
@@ -137,6 +155,23 @@ string"#; | |||
137 | } | 155 | } |
138 | 156 | ||
139 | #[test] | 157 | #[test] |
158 | fn make_raw_string_hashes_inside_works() { | ||
159 | check_assist( | ||
160 | make_raw_string, | ||
161 | r###" | ||
162 | fn f() { | ||
163 | let s = <|>"#random##\nstring"; | ||
164 | } | ||
165 | "###, | ||
166 | r####" | ||
167 | fn f() { | ||
168 | let s = <|>r###"#random## | ||
169 | string"###; | ||
170 | } | ||
171 | "####, | ||
172 | ) | ||
173 | } | ||
174 | #[test] | ||
140 | fn make_raw_string_nothing_to_unescape_works() { | 175 | fn make_raw_string_nothing_to_unescape_works() { |
141 | check_assist( | 176 | check_assist( |
142 | make_raw_string, | 177 | make_raw_string, |