7896: Only replace quotes in replace_string_with_char assist r=Veykril a=Veykril

bors r+

Co-authored-by: Lukas Wirth <lukastw97@gmail.com>
This commit is contained in:
bors[bot] 2021-03-06 20:22:36 +00:00 committed by GitHub
commit c44575b485
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -25,15 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
if value.chars().take(2).count() != 1 { if value.chars().take(2).count() != 1 {
return None; return None;
} }
let quote_offets = token.quote_offsets()?;
acc.add( acc.add(
AssistId("replace_string_with_char", AssistKind::RefactorRewrite), AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
"Replace string with char", "Replace string with char",
target, target,
|edit| { |edit| {
let token_text = token.syntax().text(); let (left, right) = quote_offets.quotes;
let inner_text = &token_text[1..token_text.len() - 1]; edit.replace(left, String::from('\''));
edit.replace(token.syntax().text_range(), format!("'{}'", inner_text)); edit.replace(right, String::from('\''));
}, },
) )
} }
@ -49,10 +50,10 @@ mod tests {
check_assist_target( check_assist_target(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
let s = "$0c"; let s = "$0c";
} }
"#, "#,
r#""c""#, r#""c""#,
); );
} }
@ -62,15 +63,15 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
let s = "$0c"; let s = "$0c";
} }
"#, "#,
r##" r##"
fn f() { fn f() {
let s = 'c'; let s = 'c';
} }
"##, "##,
) )
} }
@ -79,15 +80,15 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
let s = "$0😀"; let s = "$0😀";
} }
"#, "#,
r##" r##"
fn f() { fn f() {
let s = '😀'; let s = '😀';
} }
"##, "##,
) )
} }
@ -96,10 +97,10 @@ mod tests {
check_assist_not_applicable( check_assist_not_applicable(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
let s = "$0test"; let s = "$0test";
} }
"#, "#,
) )
} }
@ -108,15 +109,15 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
format!($0"x", 92) format!($0"x", 92)
} }
"#, "#,
r##" r##"
fn f() { fn f() {
format!('x', 92) format!('x', 92)
} }
"##, "##,
) )
} }
@ -125,15 +126,15 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
find($0"x"); find($0"x");
} }
"#, "#,
r##" r##"
fn f() { fn f() {
find('x'); find('x');
} }
"##, "##,
) )
} }
@ -142,15 +143,15 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
find($0"\n"); find($0"\n");
} }
"#, "#,
r##" r##"
fn f() { fn f() {
find('\n'); find('\n');
} }
"##, "##,
) )
} }
@ -159,15 +160,32 @@ mod tests {
check_assist( check_assist(
replace_string_with_char, replace_string_with_char,
r#" r#"
fn f() { fn f() {
find($0"\u{7FFF}"); find($0"\u{7FFF}");
} }
"#, "#,
r##" r##"
fn f() { fn f() {
find('\u{7FFF}'); find('\u{7FFF}');
} }
"##, "##,
)
}
#[test]
fn replace_raw_string_with_char() {
check_assist(
replace_string_with_char,
r##"
fn f() {
$0r#"X"#
}
"##,
r##"
fn f() {
'X'
}
"##,
) )
} }
} }