feat(assists): Even smarter with hashes
Count `"#*` streak only, extract the counting in a function, unit test this function
This commit is contained in:
parent
b06c5fac14
commit
6195096fb4
@ -28,17 +28,7 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
||||
if error.is_err() {
|
||||
eprintln!("Error unescaping string");
|
||||
} else {
|
||||
let mut max_hash_streak = 0;
|
||||
unescaped.chars().fold(0, |acc, c| {
|
||||
if c == '#' {
|
||||
acc + 1
|
||||
} else {
|
||||
if acc > max_hash_streak {
|
||||
max_hash_streak = acc;
|
||||
}
|
||||
0
|
||||
}
|
||||
});
|
||||
let max_hash_streak = count_hashes(&unescaped);
|
||||
let mut hashes = String::with_capacity(max_hash_streak + 1);
|
||||
for _ in 0..hashes.capacity() {
|
||||
hashes.push('#');
|
||||
@ -52,6 +42,19 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
||||
ctx.build()
|
||||
}
|
||||
|
||||
fn count_hashes(s: &str) -> usize {
|
||||
let indexes: Vec<_> = s.match_indices("\"#").map(|(i, _)| i).collect();
|
||||
let mut max_hash_streak = 0usize;
|
||||
for idx in indexes {
|
||||
let (_, sub) = s.split_at(idx + 1);
|
||||
let nb_hash = sub.chars().take_while(|c| *c == '#').count();
|
||||
if nb_hash > max_hash_streak {
|
||||
max_hash_streak = nb_hash;
|
||||
}
|
||||
}
|
||||
max_hash_streak
|
||||
}
|
||||
|
||||
fn find_usual_string_range(s: &str) -> Option<TextRange> {
|
||||
Some(TextRange::from_to(
|
||||
TextUnit::from(s.find('"')? as u32),
|
||||
@ -165,12 +168,31 @@ fn f() {
|
||||
"###,
|
||||
r####"
|
||||
fn f() {
|
||||
let s = <|>r###"#random##
|
||||
let s = <|>r#"#random##
|
||||
string"#;
|
||||
}
|
||||
"####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_raw_string_closing_hashes_inside_works() {
|
||||
check_assist(
|
||||
make_raw_string,
|
||||
r###"
|
||||
fn f() {
|
||||
let s = <|>"#random\"##\nstring";
|
||||
}
|
||||
"###,
|
||||
r####"
|
||||
fn f() {
|
||||
let s = <|>r###"#random"##
|
||||
string"###;
|
||||
}
|
||||
"####,
|
||||
)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn make_raw_string_nothing_to_unescape_works() {
|
||||
check_assist(
|
||||
@ -410,4 +432,14 @@ fn f() {
|
||||
"#,
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn count_hashes_test() {
|
||||
assert_eq!(0, count_hashes("abc"));
|
||||
assert_eq!(0, count_hashes("###"));
|
||||
assert_eq!(1, count_hashes("\"#abc"));
|
||||
assert_eq!(0, count_hashes("#abc"));
|
||||
assert_eq!(2, count_hashes("#ab\"##c"));
|
||||
assert_eq!(4, count_hashes("#ab\"##\"####c"));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user