diff options
-rw-r--r-- | crates/ra_assists/src/assists/raw_string.rs | 56 |
1 files changed, 44 insertions, 12 deletions
diff --git a/crates/ra_assists/src/assists/raw_string.rs b/crates/ra_assists/src/assists/raw_string.rs index dd8db0b7d..b182687a4 100644 --- a/crates/ra_assists/src/assists/raw_string.rs +++ b/crates/ra_assists/src/assists/raw_string.rs | |||
@@ -28,17 +28,7 @@ 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 | let mut max_hash_streak = 0; | 31 | let max_hash_streak = count_hashes(&unescaped); |
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); | 32 | let mut hashes = String::with_capacity(max_hash_streak + 1); |
43 | for _ in 0..hashes.capacity() { | 33 | for _ in 0..hashes.capacity() { |
44 | hashes.push('#'); | 34 | hashes.push('#'); |
@@ -52,6 +42,19 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As | |||
52 | ctx.build() | 42 | ctx.build() |
53 | } | 43 | } |
54 | 44 | ||
45 | fn count_hashes(s: &str) -> usize { | ||
46 | let indexes: Vec<_> = s.match_indices("\"#").map(|(i, _)| i).collect(); | ||
47 | let mut max_hash_streak = 0usize; | ||
48 | for idx in indexes { | ||
49 | let (_, sub) = s.split_at(idx + 1); | ||
50 | let nb_hash = sub.chars().take_while(|c| *c == '#').count(); | ||
51 | if nb_hash > max_hash_streak { | ||
52 | max_hash_streak = nb_hash; | ||
53 | } | ||
54 | } | ||
55 | max_hash_streak | ||
56 | } | ||
57 | |||
55 | fn find_usual_string_range(s: &str) -> Option<TextRange> { | 58 | fn find_usual_string_range(s: &str) -> Option<TextRange> { |
56 | Some(TextRange::from_to( | 59 | Some(TextRange::from_to( |
57 | TextUnit::from(s.find('"')? as u32), | 60 | TextUnit::from(s.find('"')? as u32), |
@@ -165,12 +168,31 @@ string"#; | |||
165 | "###, | 168 | "###, |
166 | r####" | 169 | r####" |
167 | fn f() { | 170 | fn f() { |
168 | let s = <|>r###"#random## | 171 | let s = <|>r#"#random## |
172 | string"#; | ||
173 | } | ||
174 | "####, | ||
175 | ) | ||
176 | } | ||
177 | |||
178 | #[test] | ||
179 | fn make_raw_string_closing_hashes_inside_works() { | ||
180 | check_assist( | ||
181 | make_raw_string, | ||
182 | r###" | ||
183 | fn f() { | ||
184 | let s = <|>"#random\"##\nstring"; | ||
185 | } | ||
186 | "###, | ||
187 | r####" | ||
188 | fn f() { | ||
189 | let s = <|>r###"#random"## | ||
169 | string"###; | 190 | string"###; |
170 | } | 191 | } |
171 | "####, | 192 | "####, |
172 | ) | 193 | ) |
173 | } | 194 | } |
195 | |||
174 | #[test] | 196 | #[test] |
175 | fn make_raw_string_nothing_to_unescape_works() { | 197 | fn make_raw_string_nothing_to_unescape_works() { |
176 | check_assist( | 198 | check_assist( |
@@ -410,4 +432,14 @@ string"###; | |||
410 | "#, | 432 | "#, |
411 | ); | 433 | ); |
412 | } | 434 | } |
435 | |||
436 | #[test] | ||
437 | fn count_hashes_test() { | ||
438 | assert_eq!(0, count_hashes("abc")); | ||
439 | assert_eq!(0, count_hashes("###")); | ||
440 | assert_eq!(1, count_hashes("\"#abc")); | ||
441 | assert_eq!(0, count_hashes("#abc")); | ||
442 | assert_eq!(2, count_hashes("#ab\"##c")); | ||
443 | assert_eq!(4, count_hashes("#ab\"##\"####c")); | ||
444 | } | ||
413 | } | 445 | } |