1 use syntax::{ast, ast::IsString, AstToken, SyntaxKind::STRING};
3 use crate::{AssistContext, AssistId, AssistKind, Assists};
5 // Assist: replace_string_with_char
7 // Replace string with char.
20 pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
21 let token = ctx.find_token_syntax_at_offset(STRING).and_then(ast::String::cast)?;
22 let value = token.value()?;
23 let target = token.syntax().text_range();
25 if value.chars().take(2).count() != 1 {
28 let quote_offets = token.quote_offsets()?;
31 AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
32 "Replace string with char",
35 let (left, right) = quote_offets.quotes;
36 edit.replace(left, String::from('\''));
37 edit.replace(right, String::from('\''));
44 use crate::tests::{check_assist, check_assist_not_applicable, check_assist_target};
49 fn replace_string_with_char_target() {
51 replace_string_with_char,
62 fn replace_string_with_char_assist() {
64 replace_string_with_char,
79 fn replace_string_with_char_assist_with_emoji() {
81 replace_string_with_char,
96 fn replace_string_with_char_assist_not_applicable() {
97 check_assist_not_applicable(
98 replace_string_with_char,
108 fn replace_string_with_char_works_inside_macros() {
110 replace_string_with_char,
125 fn replace_string_with_char_works_func_args() {
127 replace_string_with_char,
142 fn replace_string_with_char_newline() {
144 replace_string_with_char,
159 fn replace_string_with_char_unicode_escape() {
161 replace_string_with_char,
176 fn replace_raw_string_with_char() {
178 replace_string_with_char,