mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
feat(assists): Be smart about hashes
Add max_hashes_streak + 1 hashes to the raw string
This commit is contained in:
parent
e293c34e85
commit
b06c5fac14
1 changed files with 36 additions and 1 deletions
|
@ -28,7 +28,25 @@ pub(crate) fn make_raw_string(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
|
||||||
if error.is_err() {
|
if error.is_err() {
|
||||||
eprintln!("Error unescaping string");
|
eprintln!("Error unescaping string");
|
||||||
} else {
|
} else {
|
||||||
edit.replace(literal.syntax().text_range(), format!("r#\"{}\"#", unescaped));
|
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 mut hashes = String::with_capacity(max_hash_streak + 1);
|
||||||
|
for _ in 0..hashes.capacity() {
|
||||||
|
hashes.push('#');
|
||||||
|
}
|
||||||
|
edit.replace(
|
||||||
|
literal.syntax().text_range(),
|
||||||
|
format!("r{}\"{}\"{}", hashes, unescaped, hashes),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
ctx.build()
|
ctx.build()
|
||||||
|
@ -136,6 +154,23 @@ string"#;
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn make_raw_string_hashes_inside_works() {
|
||||||
|
check_assist(
|
||||||
|
make_raw_string,
|
||||||
|
r###"
|
||||||
|
fn f() {
|
||||||
|
let s = <|>"#random##\nstring";
|
||||||
|
}
|
||||||
|
"###,
|
||||||
|
r####"
|
||||||
|
fn f() {
|
||||||
|
let s = <|>r###"#random##
|
||||||
|
string"###;
|
||||||
|
}
|
||||||
|
"####,
|
||||||
|
)
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn make_raw_string_nothing_to_unescape_works() {
|
fn make_raw_string_nothing_to_unescape_works() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue