Only replace quotes in replace_string_with_char assist

This commit is contained in:
Lukas Wirth 2021-03-06 21:21:18 +01:00
parent 856c2850cd
commit 1a276f8959

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('\''));
}, },
) )
} }
@ -167,6 +168,23 @@ mod tests {
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'
}
"##, "##,
) )
} }