aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/handlers/replace_string_with_char.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ide_assists/src/handlers/replace_string_with_char.rs')
-rw-r--r--crates/ide_assists/src/handlers/replace_string_with_char.rs136
1 files changed, 95 insertions, 41 deletions
diff --git a/crates/ide_assists/src/handlers/replace_string_with_char.rs b/crates/ide_assists/src/handlers/replace_string_with_char.rs
index 317318c24..634b9c0b7 100644
--- a/crates/ide_assists/src/handlers/replace_string_with_char.rs
+++ b/crates/ide_assists/src/handlers/replace_string_with_char.rs
@@ -25,13 +25,16 @@ pub(crate) fn replace_string_with_char(acc: &mut Assists, ctx: &AssistContext) -
25 if value.chars().take(2).count() != 1 { 25 if value.chars().take(2).count() != 1 {
26 return None; 26 return None;
27 } 27 }
28 let quote_offets = token.quote_offsets()?;
28 29
29 acc.add( 30 acc.add(
30 AssistId("replace_string_with_char", AssistKind::RefactorRewrite), 31 AssistId("replace_string_with_char", AssistKind::RefactorRewrite),
31 "Replace string with char", 32 "Replace string with char",
32 target, 33 target,
33 |edit| { 34 |edit| {
34 edit.replace(token.syntax().text_range(), format!("'{}'", value)); 35 let (left, right) = quote_offets.quotes;
36 edit.replace(left, String::from('\''));
37 edit.replace(right, String::from('\''));
35 }, 38 },
36 ) 39 )
37} 40}
@@ -47,10 +50,10 @@ mod tests {
47 check_assist_target( 50 check_assist_target(
48 replace_string_with_char, 51 replace_string_with_char,
49 r#" 52 r#"
50 fn f() { 53fn f() {
51 let s = "$0c"; 54 let s = "$0c";
52 } 55}
53 "#, 56"#,
54 r#""c""#, 57 r#""c""#,
55 ); 58 );
56 } 59 }
@@ -60,15 +63,15 @@ mod tests {
60 check_assist( 63 check_assist(
61 replace_string_with_char, 64 replace_string_with_char,
62 r#" 65 r#"
63 fn f() { 66fn f() {
64 let s = "$0c"; 67 let s = "$0c";
65 } 68}
66 "#, 69"#,
67 r##" 70 r##"
68 fn f() { 71fn f() {
69 let s = 'c'; 72 let s = 'c';
70 } 73}
71 "##, 74"##,
72 ) 75 )
73 } 76 }
74 77
@@ -77,15 +80,15 @@ mod tests {
77 check_assist( 80 check_assist(
78 replace_string_with_char, 81 replace_string_with_char,
79 r#" 82 r#"
80 fn f() { 83fn f() {
81 let s = "$0😀"; 84 let s = "$0😀";
82 } 85}
83 "#, 86"#,
84 r##" 87 r##"
85 fn f() { 88fn f() {
86 let s = '😀'; 89 let s = '😀';
87 } 90}
88 "##, 91"##,
89 ) 92 )
90 } 93 }
91 94
@@ -94,10 +97,10 @@ mod tests {
94 check_assist_not_applicable( 97 check_assist_not_applicable(
95 replace_string_with_char, 98 replace_string_with_char,
96 r#" 99 r#"
97 fn f() { 100fn f() {
98 let s = "$0test"; 101 let s = "$0test";
99 } 102}
100 "#, 103"#,
101 ) 104 )
102 } 105 }
103 106
@@ -106,15 +109,15 @@ mod tests {
106 check_assist( 109 check_assist(
107 replace_string_with_char, 110 replace_string_with_char,
108 r#" 111 r#"
109 fn f() { 112fn f() {
110 format!($0"x", 92) 113 format!($0"x", 92)
111 } 114}
112 "#, 115"#,
113 r##" 116 r##"
114 fn f() { 117fn f() {
115 format!('x', 92) 118 format!('x', 92)
116 } 119}
117 "##, 120"##,
118 ) 121 )
119 } 122 }
120 123
@@ -123,15 +126,66 @@ mod tests {
123 check_assist( 126 check_assist(
124 replace_string_with_char, 127 replace_string_with_char,
125 r#" 128 r#"
126 fn f() { 129fn f() {
127 find($0"x"); 130 find($0"x");
128 } 131}
129 "#, 132"#,
130 r##" 133 r##"
131 fn f() { 134fn f() {
132 find('x'); 135 find('x');
133 } 136}
134 "##, 137"##,
138 )
139 }
140
141 #[test]
142 fn replace_string_with_char_newline() {
143 check_assist(
144 replace_string_with_char,
145 r#"
146fn f() {
147 find($0"\n");
148}
149"#,
150 r##"
151fn f() {
152 find('\n');
153}
154"##,
155 )
156 }
157
158 #[test]
159 fn replace_string_with_char_unicode_escape() {
160 check_assist(
161 replace_string_with_char,
162 r#"
163fn f() {
164 find($0"\u{7FFF}");
165}
166"#,
167 r##"
168fn 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,
179 r##"
180fn f() {
181 $0r#"X"#
182}
183"##,
184 r##"
185fn f() {
186 'X'
187}
188"##,
135 ) 189 )
136 } 190 }
137} 191}