@@ -25,15 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
25
25
if value. chars ( ) . take ( 2 ) . count ( ) != 1 {
26
26
return None ;
27
27
}
28
+ let quote_offets = token. quote_offsets ( ) ?;
28
29
29
30
acc. add (
30
31
AssistId ( "replace_string_with_char" , AssistKind :: RefactorRewrite ) ,
31
32
"Replace string with char" ,
32
33
target,
33
34
|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 ( '\'' ) ) ;
37
38
} ,
38
39
)
39
40
}
@@ -49,10 +50,10 @@ mod tests {
49
50
check_assist_target (
50
51
replace_string_with_char,
51
52
r#"
52
- fn f() {
53
- let s = "$0c";
54
- }
55
- "#,
53
+ fn f() {
54
+ let s = "$0c";
55
+ }
56
+ "# ,
56
57
r#""c""# ,
57
58
) ;
58
59
}
@@ -62,15 +63,15 @@ mod tests {
62
63
check_assist (
63
64
replace_string_with_char,
64
65
r#"
65
- fn f() {
66
- let s = "$0c";
67
- }
68
- "#,
66
+ fn f() {
67
+ let s = "$0c";
68
+ }
69
+ "# ,
69
70
r##"
70
- fn f() {
71
- let s = 'c';
72
- }
73
- "##,
71
+ fn f() {
72
+ let s = 'c';
73
+ }
74
+ "## ,
74
75
)
75
76
}
76
77
@@ -79,15 +80,15 @@ mod tests {
79
80
check_assist (
80
81
replace_string_with_char,
81
82
r#"
82
- fn f() {
83
- let s = "$0😀";
84
- }
85
- "#,
83
+ fn f() {
84
+ let s = "$0😀";
85
+ }
86
+ "# ,
86
87
r##"
87
- fn f() {
88
- let s = '😀';
89
- }
90
- "##,
88
+ fn f() {
89
+ let s = '😀';
90
+ }
91
+ "## ,
91
92
)
92
93
}
93
94
@@ -96,10 +97,10 @@ mod tests {
96
97
check_assist_not_applicable (
97
98
replace_string_with_char,
98
99
r#"
99
- fn f() {
100
- let s = "$0test";
101
- }
102
- "#,
100
+ fn f() {
101
+ let s = "$0test";
102
+ }
103
+ "# ,
103
104
)
104
105
}
105
106
@@ -108,15 +109,15 @@ mod tests {
108
109
check_assist (
109
110
replace_string_with_char,
110
111
r#"
111
- fn f() {
112
- format!($0"x", 92)
113
- }
114
- "#,
112
+ fn f() {
113
+ format!($0"x", 92)
114
+ }
115
+ "# ,
115
116
r##"
116
- fn f() {
117
- format!('x', 92)
118
- }
119
- "##,
117
+ fn f() {
118
+ format!('x', 92)
119
+ }
120
+ "## ,
120
121
)
121
122
}
122
123
@@ -125,15 +126,15 @@ mod tests {
125
126
check_assist (
126
127
replace_string_with_char,
127
128
r#"
128
- fn f() {
129
- find($0"x");
130
- }
131
- "#,
129
+ fn f() {
130
+ find($0"x");
131
+ }
132
+ "# ,
132
133
r##"
133
- fn f() {
134
- find('x');
135
- }
136
- "##,
134
+ fn f() {
135
+ find('x');
136
+ }
137
+ "## ,
137
138
)
138
139
}
139
140
@@ -142,15 +143,15 @@ mod tests {
142
143
check_assist (
143
144
replace_string_with_char,
144
145
r#"
145
- fn f() {
146
- find($0"\n");
147
- }
148
- "#,
146
+ fn f() {
147
+ find($0"\n");
148
+ }
149
+ "# ,
149
150
r##"
150
- fn f() {
151
- find('\n');
152
- }
153
- "##,
151
+ fn f() {
152
+ find('\n');
153
+ }
154
+ "## ,
154
155
)
155
156
}
156
157
@@ -159,15 +160,32 @@ mod tests {
159
160
check_assist (
160
161
replace_string_with_char,
161
162
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,
166
179
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
+ "## ,
171
189
)
172
190
}
173
191
}
0 commit comments