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 {
return None;
}
let quote_offets = token.quote_offsets()?;
acc.add(
AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
"Replace string with char",
target,
|edit| {
let token_text = token.syntax().text();
let inner_text = &token_text[1..token_text.len() - 1];
edit.replace(token.syntax().text_range(), format!("'{}'", inner_text));
let (left, right) = quote_offets.quotes;
edit.replace(left, String::from('\''));
edit.replace(right, String::from('\''));
},
)
}
@ -49,10 +50,10 @@ fn replace_string_with_char_target() {
check_assist_target(
replace_string_with_char,
r#"
fn f() {
let s = "$0c";
}
"#,
fn f() {
let s = "$0c";
}
"#,
r#""c""#,
);
}
@ -62,15 +63,15 @@ fn replace_string_with_char_assist() {
check_assist(
replace_string_with_char,
r#"
fn f() {
let s = "$0c";
}
"#,
fn f() {
let s = "$0c";
}
"#,
r##"
fn f() {
let s = 'c';
}
"##,
fn f() {
let s = 'c';
}
"##,
)
}
@ -79,15 +80,15 @@ fn replace_string_with_char_assist_with_emoji() {
check_assist(
replace_string_with_char,
r#"
fn f() {
let s = "$0😀";
}
"#,
fn f() {
let s = "$0😀";
}
"#,
r##"
fn f() {
let s = '😀';
}
"##,
fn f() {
let s = '😀';
}
"##,
)
}
@ -96,10 +97,10 @@ fn replace_string_with_char_assist_not_applicable() {
check_assist_not_applicable(
replace_string_with_char,
r#"
fn f() {
let s = "$0test";
}
"#,
fn f() {
let s = "$0test";
}
"#,
)
}
@ -108,15 +109,15 @@ fn replace_string_with_char_works_inside_macros() {
check_assist(
replace_string_with_char,
r#"
fn f() {
format!($0"x", 92)
}
"#,
fn f() {
format!($0"x", 92)
}
"#,
r##"
fn f() {
format!('x', 92)
}
"##,
fn f() {
format!('x', 92)
}
"##,
)
}
@ -125,15 +126,15 @@ fn replace_string_with_char_works_func_args() {
check_assist(
replace_string_with_char,
r#"
fn f() {
find($0"x");
}
"#,
fn f() {
find($0"x");
}
"#,
r##"
fn f() {
find('x');
}
"##,
fn f() {
find('x');
}
"##,
)
}
@ -142,15 +143,15 @@ fn replace_string_with_char_newline() {
check_assist(
replace_string_with_char,
r#"
fn f() {
find($0"\n");
}
"#,
fn f() {
find($0"\n");
}
"#,
r##"
fn f() {
find('\n');
}
"##,
fn f() {
find('\n');
}
"##,
)
}
@ -159,15 +160,32 @@ fn replace_string_with_char_unicode_escape() {
check_assist(
replace_string_with_char,
r#"
fn f() {
find($0"\u{7FFF}");
}
"#,
fn f() {
find($0"\u{7FFF}");
}
"#,
r##"
fn f() {
find('\u{7FFF}');
}
"##,
fn f() {
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'
}
"##,
)
}
}