@@ -5,7 +5,7 @@ use syntax::{
5
5
ast:: IsString ,
6
6
} ;
7
7
8
- use crate :: { AssistContext , AssistId , Assists } ;
8
+ use crate :: { AssistContext , AssistId , Assists , utils :: string_suffix } ;
9
9
10
10
// Assist: replace_string_with_char
11
11
//
@@ -38,9 +38,11 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext<'_
38
38
target,
39
39
|edit| {
40
40
let ( left, right) = quote_offsets. quotes ;
41
+ let suffix = TextSize :: of ( string_suffix ( token. text ( ) ) . unwrap_or_default ( ) ) ;
42
+ let right = TextRange :: new ( right. start ( ) , right. end ( ) - suffix) ;
41
43
edit. replace ( left, '\'' ) ;
42
44
edit. replace ( right, '\'' ) ;
43
- if value == "'" {
45
+ if token . text_without_quotes ( ) == "'" {
44
46
edit. insert ( left. end ( ) , '\\' ) ;
45
47
}
46
48
} ,
@@ -71,12 +73,14 @@ pub(crate) fn replace_char_with_string(acc: &mut Assists, ctx: &AssistContext<'_
71
73
"Replace char with string" ,
72
74
target,
73
75
|edit| {
74
- if token. text ( ) == "'\" '" {
75
- edit. replace ( token. text_range ( ) , r#""\"""# ) ;
76
+ let suffix = string_suffix ( token. text ( ) ) . unwrap_or_default ( ) ;
77
+ if token. text ( ) . starts_with ( "'\" '" ) {
78
+ edit. replace ( token. text_range ( ) , format ! ( r#""\""{suffix}"# ) ) ;
76
79
} else {
77
80
let len = TextSize :: of ( '\'' ) ;
81
+ let suffix = TextSize :: of ( suffix) ;
78
82
edit. replace ( TextRange :: at ( target. start ( ) , len) , '"' ) ;
79
- edit. replace ( TextRange :: at ( target. end ( ) - len, len) , '"' ) ;
83
+ edit. replace ( TextRange :: at ( target. end ( ) - suffix - len, len) , '"' ) ;
80
84
}
81
85
} ,
82
86
)
@@ -105,6 +109,23 @@ fn f() {
105
109
)
106
110
}
107
111
112
+ #[ test]
113
+ fn replace_string_with_char_has_suffix ( ) {
114
+ check_assist (
115
+ replace_string_with_char,
116
+ r#"
117
+ fn f() {
118
+ let s = "$0c"i32;
119
+ }
120
+ "# ,
121
+ r##"
122
+ fn f() {
123
+ let s = 'c'i32;
124
+ }
125
+ "## ,
126
+ )
127
+ }
128
+
108
129
#[ test]
109
130
fn replace_string_with_char_assist_with_multi_byte_char ( ) {
110
131
check_assist (
@@ -287,6 +308,40 @@ fn f() {
287
308
)
288
309
}
289
310
311
+ #[ test]
312
+ fn replace_char_with_string_quote_has_suffix ( ) {
313
+ check_assist (
314
+ replace_char_with_string,
315
+ r#"
316
+ fn f() {
317
+ find($0'"'i32);
318
+ }
319
+ "# ,
320
+ r#"
321
+ fn f() {
322
+ find("\""i32);
323
+ }
324
+ "# ,
325
+ )
326
+ }
327
+
328
+ #[ test]
329
+ fn replace_char_with_string_escaped_quote_has_suffix ( ) {
330
+ check_assist (
331
+ replace_char_with_string,
332
+ r#"
333
+ fn f() {
334
+ find($0'\"'i32);
335
+ }
336
+ "# ,
337
+ r#"
338
+ fn f() {
339
+ find("\""i32);
340
+ }
341
+ "# ,
342
+ )
343
+ }
344
+
290
345
#[ test]
291
346
fn replace_string_with_char_quote ( ) {
292
347
check_assist (
@@ -300,6 +355,91 @@ fn f() {
300
355
fn f() {
301
356
find('\'');
302
357
}
358
+ "# ,
359
+ )
360
+ }
361
+
362
+ #[ test]
363
+ fn replace_string_with_escaped_char_quote ( ) {
364
+ check_assist (
365
+ replace_string_with_char,
366
+ r#"
367
+ fn f() {
368
+ find($0"\'");
369
+ }
370
+ "# ,
371
+ r#"
372
+ fn f() {
373
+ find('\'');
374
+ }
375
+ "# ,
376
+ )
377
+ }
378
+
379
+ #[ test]
380
+ fn replace_string_with_char_quote_has_suffix ( ) {
381
+ check_assist (
382
+ replace_string_with_char,
383
+ r#"
384
+ fn f() {
385
+ find($0"'"i32);
386
+ }
387
+ "# ,
388
+ r#"
389
+ fn f() {
390
+ find('\''i32);
391
+ }
392
+ "# ,
393
+ )
394
+ }
395
+
396
+ #[ test]
397
+ fn replace_string_with_escaped_char_quote_has_suffix ( ) {
398
+ check_assist (
399
+ replace_string_with_char,
400
+ r#"
401
+ fn f() {
402
+ find($0"\'"i32);
403
+ }
404
+ "# ,
405
+ r#"
406
+ fn f() {
407
+ find('\''i32);
408
+ }
409
+ "# ,
410
+ )
411
+ }
412
+
413
+ #[ test]
414
+ fn replace_raw_string_with_char_quote ( ) {
415
+ check_assist (
416
+ replace_string_with_char,
417
+ r#"
418
+ fn f() {
419
+ find($0r"'");
420
+ }
421
+ "# ,
422
+ r#"
423
+ fn f() {
424
+ find('\'');
425
+ }
426
+ "# ,
427
+ )
428
+ }
429
+
430
+ #[ test]
431
+ fn replace_string_with_code_escaped_char_quote ( ) {
432
+ check_assist (
433
+ replace_string_with_char,
434
+ r#"
435
+ fn f() {
436
+ find($0"\x27");
437
+ }
438
+ "# ,
439
+ r#"
440
+ fn f() {
441
+ find('\x27');
442
+ }
303
443
"# ,
304
444
)
305
445
}
0 commit comments