5 SyntaxKind
::{CHAR, STRING}
,
9 use crate::{AssistContext, AssistId, AssistKind, Assists}
;
11 // Assist: replace_string_with_char
13 // Replace string literal with char literal.
26 pub(crate) fn replace_string_with_char(acc
: &mut Assists
, ctx
: &AssistContext
<'_
>) -> Option
<()> {
27 let token
= ctx
.find_token_syntax_at_offset(STRING
).and_then(ast
::String
::cast
)?
;
28 let value
= token
.value()?
;
29 let target
= token
.syntax().text_range();
31 if value
.chars().take(2).count() != 1 {
34 let quote_offets
= token
.quote_offsets()?
;
37 AssistId("replace_string_with_char", AssistKind
::RefactorRewrite
),
38 "Replace string with char",
41 let (left
, right
) = quote_offets
.quotes
;
42 edit
.replace(left
, '
\''
);
43 edit
.replace(right
, '
\''
);
45 edit
.insert(left
.end(), '
\\'
);
51 // Assist: replace_char_with_string
53 // Replace a char literal with a string literal.
66 pub(crate) fn replace_char_with_string(acc
: &mut Assists
, ctx
: &AssistContext
<'_
>) -> Option
<()> {
67 let token
= ctx
.find_token_syntax_at_offset(CHAR
)?
;
68 let target
= token
.text_range();
71 AssistId("replace_char_with_string", AssistKind
::RefactorRewrite
),
72 "Replace char with string",
75 if token
.text() == "'\"'" {
76 edit
.replace(token
.text_range(), r
#""\"""#);
78 let len
= TextSize
::of('
\''
);
79 edit
.replace(TextRange
::at(target
.start(), len
), '
"');
80 edit.replace(TextRange::at(target.end() - len, len), '"'
);
88 use crate::tests
::{check_assist, check_assist_not_applicable}
;
93 fn replace_string_with_char_assist() {
95 replace_string_with_char
,
110 fn replace_string_with_char_assist_with_multi_byte_char() {
112 replace_string_with_char
,
127 fn replace_string_with_char_multiple_chars() {
128 check_assist_not_applicable(
129 replace_string_with_char
,
139 fn replace_string_with_char_works_inside_macros() {
141 replace_string_with_char
,
156 fn replace_string_with_char_newline() {
158 replace_string_with_char
,
173 fn replace_string_with_char_unicode_escape() {
175 replace_string_with_char
,
190 fn replace_raw_string_with_char() {
192 replace_string_with_char
,
207 fn replace_char_with_string_assist() {
209 replace_char_with_string
,
224 fn replace_char_with_string_assist_with_multi_byte_char() {
226 replace_char_with_string
,
241 fn replace_char_with_string_newline() {
243 replace_char_with_string
,
258 fn replace_char_with_string_unicode_escape() {
260 replace_char_with_string
,
275 fn replace_char_with_string_quote() {
277 replace_char_with_string
,
292 fn replace_string_with_char_quote() {
294 replace_string_with_char,