diff options
author | Lukas Wirth <[email protected]> | 2021-03-06 20:21:18 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-03-06 20:21:18 +0000 |
commit | 1a276f8959b063324efc05d8972d4ffe7bab3fd8 (patch) | |
tree | 6dd2dcaa436675aad5ddbc3fd7cbead018e16246 /crates/ide_assists/src/handlers | |
parent | 856c2850cd4ab90b79ef6f9232e2bcfc04d8fde2 (diff) |
Only replace quotes in replace_string_with_char assist
Diffstat (limited to 'crates/ide_assists/src/handlers')
-rw-r--r-- | crates/ide_assists/src/handlers/replace_string_with_char.rs | 136 |
1 files changed, 77 insertions, 59 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 303c0dcbe..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,15 +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 | let token_text = token.syntax().text(); | 35 | let (left, right) = quote_offets.quotes; |
35 | let inner_text = &token_text[1..token_text.len() - 1]; | 36 | edit.replace(left, String::from('\'')); |
36 | edit.replace(token.syntax().text_range(), format!("'{}'", inner_text)); | 37 | edit.replace(right, String::from('\'')); |
37 | }, | 38 | }, |
38 | ) | 39 | ) |
39 | } | 40 | } |
@@ -49,10 +50,10 @@ mod tests { | |||
49 | check_assist_target( | 50 | check_assist_target( |
50 | replace_string_with_char, | 51 | replace_string_with_char, |
51 | r#" | 52 | r#" |
52 | fn f() { | 53 | fn f() { |
53 | let s = "$0c"; | 54 | let s = "$0c"; |
54 | } | 55 | } |
55 | "#, | 56 | "#, |
56 | r#""c""#, | 57 | r#""c""#, |
57 | ); | 58 | ); |
58 | } | 59 | } |
@@ -62,15 +63,15 @@ mod tests { | |||
62 | check_assist( | 63 | check_assist( |
63 | replace_string_with_char, | 64 | replace_string_with_char, |
64 | r#" | 65 | r#" |
65 | fn f() { | 66 | fn f() { |
66 | let s = "$0c"; | 67 | let s = "$0c"; |
67 | } | 68 | } |
68 | "#, | 69 | "#, |
69 | r##" | 70 | r##" |
70 | fn f() { | 71 | fn f() { |
71 | let s = 'c'; | 72 | let s = 'c'; |
72 | } | 73 | } |
73 | "##, | 74 | "##, |
74 | ) | 75 | ) |
75 | } | 76 | } |
76 | 77 | ||
@@ -79,15 +80,15 @@ mod tests { | |||
79 | check_assist( | 80 | check_assist( |
80 | replace_string_with_char, | 81 | replace_string_with_char, |
81 | r#" | 82 | r#" |
82 | fn f() { | 83 | fn f() { |
83 | let s = "$0😀"; | 84 | let s = "$0😀"; |
84 | } | 85 | } |
85 | "#, | 86 | "#, |
86 | r##" | 87 | r##" |
87 | fn f() { | 88 | fn f() { |
88 | let s = '😀'; | 89 | let s = '😀'; |
89 | } | 90 | } |
90 | "##, | 91 | "##, |
91 | ) | 92 | ) |
92 | } | 93 | } |
93 | 94 | ||
@@ -96,10 +97,10 @@ mod tests { | |||
96 | check_assist_not_applicable( | 97 | check_assist_not_applicable( |
97 | replace_string_with_char, | 98 | replace_string_with_char, |
98 | r#" | 99 | r#" |
99 | fn f() { | 100 | fn f() { |
100 | let s = "$0test"; | 101 | let s = "$0test"; |
101 | } | 102 | } |
102 | "#, | 103 | "#, |
103 | ) | 104 | ) |
104 | } | 105 | } |
105 | 106 | ||
@@ -108,15 +109,15 @@ mod tests { | |||
108 | check_assist( | 109 | check_assist( |
109 | replace_string_with_char, | 110 | replace_string_with_char, |
110 | r#" | 111 | r#" |
111 | fn f() { | 112 | fn f() { |
112 | format!($0"x", 92) | 113 | format!($0"x", 92) |
113 | } | 114 | } |
114 | "#, | 115 | "#, |
115 | r##" | 116 | r##" |
116 | fn f() { | 117 | fn f() { |
117 | format!('x', 92) | 118 | format!('x', 92) |
118 | } | 119 | } |
119 | "##, | 120 | "##, |
120 | ) | 121 | ) |
121 | } | 122 | } |
122 | 123 | ||
@@ -125,15 +126,15 @@ mod tests { | |||
125 | check_assist( | 126 | check_assist( |
126 | replace_string_with_char, | 127 | replace_string_with_char, |
127 | r#" | 128 | r#" |
128 | fn f() { | 129 | fn f() { |
129 | find($0"x"); | 130 | find($0"x"); |
130 | } | 131 | } |
131 | "#, | 132 | "#, |
132 | r##" | 133 | r##" |
133 | fn f() { | 134 | fn f() { |
134 | find('x'); | 135 | find('x'); |
135 | } | 136 | } |
136 | "##, | 137 | "##, |
137 | ) | 138 | ) |
138 | } | 139 | } |
139 | 140 | ||
@@ -142,15 +143,15 @@ mod tests { | |||
142 | check_assist( | 143 | check_assist( |
143 | replace_string_with_char, | 144 | replace_string_with_char, |
144 | r#" | 145 | r#" |
145 | fn f() { | 146 | fn f() { |
146 | find($0"\n"); | 147 | find($0"\n"); |
147 | } | 148 | } |
148 | "#, | 149 | "#, |
149 | r##" | 150 | r##" |
150 | fn f() { | 151 | fn f() { |
151 | find('\n'); | 152 | find('\n'); |
152 | } | 153 | } |
153 | "##, | 154 | "##, |
154 | ) | 155 | ) |
155 | } | 156 | } |
156 | 157 | ||
@@ -159,15 +160,32 @@ mod tests { | |||
159 | check_assist( | 160 | check_assist( |
160 | replace_string_with_char, | 161 | replace_string_with_char, |
161 | r#" | 162 | r#" |
162 | fn f() { | 163 | fn f() { |
163 | find($0"\u{7FFF}"); | 164 | find($0"\u{7FFF}"); |
164 | } | 165 | } |
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 | r##" | 179 | r##" |
167 | fn f() { | 180 | fn f() { |
168 | find('\u{7FFF}'); | 181 | $0r#"X"# |
169 | } | 182 | } |
170 | "##, | 183 | "##, |
184 | r##" | ||
185 | fn f() { | ||
186 | 'X' | ||
187 | } | ||
188 | "##, | ||
171 | ) | 189 | ) |
172 | } | 190 | } |
173 | } | 191 | } |