Skip to content

Commit c44575b

Browse files
bors[bot]Veykril
andauthored
Merge #7896
7896: Only replace quotes in replace_string_with_char assist r=Veykril a=Veykril bors r+ Co-authored-by: Lukas Wirth <[email protected]>
2 parents 5480bed + 1a276f8 commit c44575b

File tree

1 file changed

+77
-59
lines changed

1 file changed

+77
-59
lines changed

crates/ide_assists/src/handlers/replace_string_with_char.rs

Lines changed: 77 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
2525
if value.chars().take(2).count() != 1 {
2626
return None;
2727
}
28+
let quote_offets = token.quote_offsets()?;
2829

2930
acc.add(
3031
AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
3132
"Replace string with char",
3233
target,
3334
|edit| {
34-
let token_text = token.syntax().text();
35-
let inner_text = &token_text[1..token_text.len() - 1];
36-
edit.replace(token.syntax().text_range(), format!("'{}'", inner_text));
35+
let (left, right) = quote_offets.quotes;
36+
edit.replace(left, String::from('\''));
37+
edit.replace(right, String::from('\''));
3738
},
3839
)
3940
}
@@ -49,10 +50,10 @@ mod tests {
4950
check_assist_target(
5051
replace_string_with_char,
5152
r#"
52-
fn f() {
53-
let s = "$0c";
54-
}
55-
"#,
53+
fn f() {
54+
let s = "$0c";
55+
}
56+
"#,
5657
r#""c""#,
5758
);
5859
}
@@ -62,15 +63,15 @@ mod tests {
6263
check_assist(
6364
replace_string_with_char,
6465
r#"
65-
fn f() {
66-
let s = "$0c";
67-
}
68-
"#,
66+
fn f() {
67+
let s = "$0c";
68+
}
69+
"#,
6970
r##"
70-
fn f() {
71-
let s = 'c';
72-
}
73-
"##,
71+
fn f() {
72+
let s = 'c';
73+
}
74+
"##,
7475
)
7576
}
7677

@@ -79,15 +80,15 @@ mod tests {
7980
check_assist(
8081
replace_string_with_char,
8182
r#"
82-
fn f() {
83-
let s = "$0😀";
84-
}
85-
"#,
83+
fn f() {
84+
let s = "$0😀";
85+
}
86+
"#,
8687
r##"
87-
fn f() {
88-
let s = '😀';
89-
}
90-
"##,
88+
fn f() {
89+
let s = '😀';
90+
}
91+
"##,
9192
)
9293
}
9394

@@ -96,10 +97,10 @@ mod tests {
9697
check_assist_not_applicable(
9798
replace_string_with_char,
9899
r#"
99-
fn f() {
100-
let s = "$0test";
101-
}
102-
"#,
100+
fn f() {
101+
let s = "$0test";
102+
}
103+
"#,
103104
)
104105
}
105106

@@ -108,15 +109,15 @@ mod tests {
108109
check_assist(
109110
replace_string_with_char,
110111
r#"
111-
fn f() {
112-
format!($0"x", 92)
113-
}
114-
"#,
112+
fn f() {
113+
format!($0"x", 92)
114+
}
115+
"#,
115116
r##"
116-
fn f() {
117-
format!('x', 92)
118-
}
119-
"##,
117+
fn f() {
118+
format!('x', 92)
119+
}
120+
"##,
120121
)
121122
}
122123

@@ -125,15 +126,15 @@ mod tests {
125126
check_assist(
126127
replace_string_with_char,
127128
r#"
128-
fn f() {
129-
find($0"x");
130-
}
131-
"#,
129+
fn f() {
130+
find($0"x");
131+
}
132+
"#,
132133
r##"
133-
fn f() {
134-
find('x');
135-
}
136-
"##,
134+
fn f() {
135+
find('x');
136+
}
137+
"##,
137138
)
138139
}
139140

@@ -142,15 +143,15 @@ mod tests {
142143
check_assist(
143144
replace_string_with_char,
144145
r#"
145-
fn f() {
146-
find($0"\n");
147-
}
148-
"#,
146+
fn f() {
147+
find($0"\n");
148+
}
149+
"#,
149150
r##"
150-
fn f() {
151-
find('\n');
152-
}
153-
"##,
151+
fn f() {
152+
find('\n');
153+
}
154+
"##,
154155
)
155156
}
156157

@@ -159,15 +160,32 @@ mod tests {
159160
check_assist(
160161
replace_string_with_char,
161162
r#"
162-
fn f() {
163-
find($0"\u{7FFF}");
164-
}
165-
"#,
163+
fn f() {
164+
find($0"\u{7FFF}");
165+
}
166+
"#,
167+
r##"
168+
fn f() {
169+
find('\u{7FFF}');
170+
}
171+
"##,
172+
)
173+
}
174+
175+
#[test]
176+
fn replace_raw_string_with_char() {
177+
check_assist(
178+
replace_string_with_char,
166179
r##"
167-
fn f() {
168-
find('\u{7FFF}');
169-
}
170-
"##,
180+
fn f() {
181+
$0r#"X"#
182+
}
183+
"##,
184+
r##"
185+
fn f() {
186+
'X'
187+
}
188+
"##,
171189
)
172190
}
173191
}

0 commit comments

Comments
 (0)