diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-10-05 23:00:40 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-10-05 23:00:40 +0100 |
commit | dd6a352e64a2d52efd43feca4b2e80d124210a60 (patch) | |
tree | 9d8ac48868ba23669f48e665b7c4f8168d7ae9a7 /crates/assists | |
parent | 3fc7338dcb8af4bc8771ae319ae0dd1612fa9013 (diff) | |
parent | b2bfadb52c51b0f73f0b425fb6a486bb644e8ebb (diff) |
Merge #6111
6111: Add assist for converting the base of integer literals. r=SomeoneToIgnore a=vlakreeh
This PR adds an assist similar to Intellij's [convert number to](https://i.imgur.com/JH6wstP.png). It also does a small refactor to [assists/src/tests.rs](https://github.com/rust-analyzer/rust-analyzer/blob/fc34403018079ea053f26d0a31b7517053c7dd8c/crates/assists/src/tests.rs) to add the ability to specify the resolved assist for a specific action within an assist group.
## Demo
![Demo of the assist in action](https://i.imgur.com/MBhdPFH.gif)
Co-authored-by: vlakreeh <[email protected]>
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/handlers/convert_integer_literal.rs | 701 | ||||
-rw-r--r-- | crates/assists/src/lib.rs | 2 | ||||
-rw-r--r-- | crates/assists/src/tests.rs | 27 | ||||
-rw-r--r-- | crates/assists/src/tests/generated.rs | 13 |
4 files changed, 738 insertions, 5 deletions
diff --git a/crates/assists/src/handlers/convert_integer_literal.rs b/crates/assists/src/handlers/convert_integer_literal.rs new file mode 100644 index 000000000..ea35e833a --- /dev/null +++ b/crates/assists/src/handlers/convert_integer_literal.rs | |||
@@ -0,0 +1,701 @@ | |||
1 | use syntax::{ast, AstNode, SmolStr}; | ||
2 | |||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | ||
4 | |||
5 | // Assist: convert_integer_literal | ||
6 | // | ||
7 | // Converts the base of integer literals to other bases. | ||
8 | // | ||
9 | // ``` | ||
10 | // const _: i32 = 10<|>; | ||
11 | // ``` | ||
12 | // -> | ||
13 | // ``` | ||
14 | // const _: i32 = 0b1010; | ||
15 | // ``` | ||
16 | pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | ||
17 | let literal = ctx.find_node_at_offset::<ast::Literal>()?; | ||
18 | let range = literal.syntax().text_range(); | ||
19 | let group_id = GroupLabel("Convert integer base".into()); | ||
20 | |||
21 | let suffix = match literal.kind() { | ||
22 | ast::LiteralKind::IntNumber { suffix } => suffix, | ||
23 | _ => return None, | ||
24 | }; | ||
25 | let suffix_len = suffix.as_ref().map(|s| s.len()).unwrap_or(0); | ||
26 | let raw_literal_text = literal.syntax().to_string(); | ||
27 | |||
28 | // Gets the literal's text without the type suffix and without underscores. | ||
29 | let literal_text = raw_literal_text | ||
30 | .chars() | ||
31 | .take(raw_literal_text.len() - suffix_len) | ||
32 | .filter(|c| *c != '_') | ||
33 | .collect::<SmolStr>(); | ||
34 | let literal_base = IntegerLiteralBase::identify(&literal_text)?; | ||
35 | |||
36 | for base in IntegerLiteralBase::bases() { | ||
37 | if *base == literal_base { | ||
38 | continue; | ||
39 | } | ||
40 | |||
41 | let mut converted = literal_base.convert(&literal_text, base); | ||
42 | |||
43 | let label = if let Some(suffix) = &suffix { | ||
44 | format!("Convert {} ({}) to {}", &literal_text, suffix, &converted) | ||
45 | } else { | ||
46 | format!("Convert {} to {}", &literal_text, &converted) | ||
47 | }; | ||
48 | |||
49 | // Appends the type suffix back into the new literal if it exists. | ||
50 | if let Some(suffix) = &suffix { | ||
51 | converted.push_str(&suffix); | ||
52 | } | ||
53 | |||
54 | acc.add_group( | ||
55 | &group_id, | ||
56 | AssistId("convert_integer_literal", AssistKind::RefactorInline), | ||
57 | label, | ||
58 | range, | ||
59 | |builder| builder.replace(range, converted), | ||
60 | ); | ||
61 | } | ||
62 | |||
63 | Some(()) | ||
64 | } | ||
65 | |||
66 | #[derive(Debug, PartialEq, Eq)] | ||
67 | enum IntegerLiteralBase { | ||
68 | Binary, | ||
69 | Octal, | ||
70 | Decimal, | ||
71 | Hexadecimal, | ||
72 | } | ||
73 | |||
74 | impl IntegerLiteralBase { | ||
75 | fn identify(literal_text: &str) -> Option<Self> { | ||
76 | // We cannot express a literal in anything other than decimal in under 3 characters, so we return here if possible. | ||
77 | if literal_text.len() < 3 && literal_text.chars().all(|c| c.is_digit(10)) { | ||
78 | return Some(Self::Decimal); | ||
79 | } | ||
80 | |||
81 | let base = match &literal_text[..2] { | ||
82 | "0b" => Self::Binary, | ||
83 | "0o" => Self::Octal, | ||
84 | "0x" => Self::Hexadecimal, | ||
85 | _ => Self::Decimal, | ||
86 | }; | ||
87 | |||
88 | // Checks that all characters after the base prefix are all valid digits for that base. | ||
89 | if literal_text[base.prefix_len()..].chars().all(|c| c.is_digit(base.base())) { | ||
90 | Some(base) | ||
91 | } else { | ||
92 | None | ||
93 | } | ||
94 | } | ||
95 | |||
96 | fn convert(&self, literal_text: &str, to: &IntegerLiteralBase) -> String { | ||
97 | let digits = &literal_text[self.prefix_len()..]; | ||
98 | let value = u128::from_str_radix(digits, self.base()).unwrap(); | ||
99 | |||
100 | match to { | ||
101 | Self::Binary => format!("0b{:b}", value), | ||
102 | Self::Octal => format!("0o{:o}", value), | ||
103 | Self::Decimal => value.to_string(), | ||
104 | Self::Hexadecimal => format!("0x{:X}", value), | ||
105 | } | ||
106 | } | ||
107 | |||
108 | const fn base(&self) -> u32 { | ||
109 | match self { | ||
110 | Self::Binary => 2, | ||
111 | Self::Octal => 8, | ||
112 | Self::Decimal => 10, | ||
113 | Self::Hexadecimal => 16, | ||
114 | } | ||
115 | } | ||
116 | |||
117 | const fn prefix_len(&self) -> usize { | ||
118 | match self { | ||
119 | Self::Decimal => 0, | ||
120 | _ => 2, | ||
121 | } | ||
122 | } | ||
123 | |||
124 | const fn bases() -> &'static [IntegerLiteralBase] { | ||
125 | &[ | ||
126 | IntegerLiteralBase::Binary, | ||
127 | IntegerLiteralBase::Octal, | ||
128 | IntegerLiteralBase::Decimal, | ||
129 | IntegerLiteralBase::Hexadecimal, | ||
130 | ] | ||
131 | } | ||
132 | } | ||
133 | |||
134 | #[cfg(test)] | ||
135 | mod tests { | ||
136 | |||
137 | use super::*; | ||
138 | use crate::tests::{check_assist_by_label, check_assist_target}; | ||
139 | |||
140 | #[test] | ||
141 | fn binary_target() { | ||
142 | check_assist_target(convert_integer_literal, "const _: i32 = 0b1010<|>;", "0b1010"); | ||
143 | } | ||
144 | |||
145 | #[test] | ||
146 | fn octal_target() { | ||
147 | check_assist_target(convert_integer_literal, "const _: i32 = 0o12<|>;", "0o12"); | ||
148 | } | ||
149 | |||
150 | #[test] | ||
151 | fn decimal_target() { | ||
152 | check_assist_target(convert_integer_literal, "const _: i32 = 10<|>;", "10"); | ||
153 | } | ||
154 | |||
155 | #[test] | ||
156 | fn hexadecimal_target() { | ||
157 | check_assist_target(convert_integer_literal, "const _: i32 = 0xA<|>;", "0xA"); | ||
158 | } | ||
159 | |||
160 | #[test] | ||
161 | fn binary_target_with_underscores() { | ||
162 | check_assist_target(convert_integer_literal, "const _: i32 = 0b10_10<|>;", "0b10_10"); | ||
163 | } | ||
164 | |||
165 | #[test] | ||
166 | fn octal_target_with_underscores() { | ||
167 | check_assist_target(convert_integer_literal, "const _: i32 = 0o1_2<|>;", "0o1_2"); | ||
168 | } | ||
169 | |||
170 | #[test] | ||
171 | fn decimal_target_with_underscores() { | ||
172 | check_assist_target(convert_integer_literal, "const _: i32 = 1_0<|>;", "1_0"); | ||
173 | } | ||
174 | |||
175 | #[test] | ||
176 | fn hexadecimal_target_with_underscores() { | ||
177 | check_assist_target(convert_integer_literal, "const _: i32 = 0x_A<|>;", "0x_A"); | ||
178 | } | ||
179 | |||
180 | #[test] | ||
181 | fn convert_decimal_integer() { | ||
182 | let before = "const _: i32 = 1000<|>;"; | ||
183 | |||
184 | check_assist_by_label( | ||
185 | convert_integer_literal, | ||
186 | before, | ||
187 | "const _: i32 = 0b1111101000;", | ||
188 | "Convert 1000 to 0b1111101000", | ||
189 | ); | ||
190 | |||
191 | check_assist_by_label( | ||
192 | convert_integer_literal, | ||
193 | before, | ||
194 | "const _: i32 = 0o1750;", | ||
195 | "Convert 1000 to 0o1750", | ||
196 | ); | ||
197 | |||
198 | check_assist_by_label( | ||
199 | convert_integer_literal, | ||
200 | before, | ||
201 | "const _: i32 = 0x3E8;", | ||
202 | "Convert 1000 to 0x3E8", | ||
203 | ); | ||
204 | } | ||
205 | |||
206 | // Decimal numbers under 3 digits have a special case where they return early because we can't fit a | ||
207 | // other base's prefix, so we have a separate test for that. | ||
208 | #[test] | ||
209 | fn convert_small_decimal_integer() { | ||
210 | let before = "const _: i32 = 10<|>;"; | ||
211 | |||
212 | check_assist_by_label( | ||
213 | convert_integer_literal, | ||
214 | before, | ||
215 | "const _: i32 = 0b1010;", | ||
216 | "Convert 10 to 0b1010", | ||
217 | ); | ||
218 | |||
219 | check_assist_by_label( | ||
220 | convert_integer_literal, | ||
221 | before, | ||
222 | "const _: i32 = 0o12;", | ||
223 | "Convert 10 to 0o12", | ||
224 | ); | ||
225 | |||
226 | check_assist_by_label( | ||
227 | convert_integer_literal, | ||
228 | before, | ||
229 | "const _: i32 = 0xA;", | ||
230 | "Convert 10 to 0xA", | ||
231 | ); | ||
232 | } | ||
233 | |||
234 | #[test] | ||
235 | fn convert_hexadecimal_integer() { | ||
236 | let before = "const _: i32 = 0xFF<|>;"; | ||
237 | |||
238 | check_assist_by_label( | ||
239 | convert_integer_literal, | ||
240 | before, | ||
241 | "const _: i32 = 0b11111111;", | ||
242 | "Convert 0xFF to 0b11111111", | ||
243 | ); | ||
244 | |||
245 | check_assist_by_label( | ||
246 | convert_integer_literal, | ||
247 | before, | ||
248 | "const _: i32 = 0o377;", | ||
249 | "Convert 0xFF to 0o377", | ||
250 | ); | ||
251 | |||
252 | check_assist_by_label( | ||
253 | convert_integer_literal, | ||
254 | before, | ||
255 | "const _: i32 = 255;", | ||
256 | "Convert 0xFF to 255", | ||
257 | ); | ||
258 | } | ||
259 | |||
260 | #[test] | ||
261 | fn convert_binary_integer() { | ||
262 | let before = "const _: i32 = 0b11111111<|>;"; | ||
263 | |||
264 | check_assist_by_label( | ||
265 | convert_integer_literal, | ||
266 | before, | ||
267 | "const _: i32 = 0o377;", | ||
268 | "Convert 0b11111111 to 0o377", | ||
269 | ); | ||
270 | |||
271 | check_assist_by_label( | ||
272 | convert_integer_literal, | ||
273 | before, | ||
274 | "const _: i32 = 255;", | ||
275 | "Convert 0b11111111 to 255", | ||
276 | ); | ||
277 | |||
278 | check_assist_by_label( | ||
279 | convert_integer_literal, | ||
280 | before, | ||
281 | "const _: i32 = 0xFF;", | ||
282 | "Convert 0b11111111 to 0xFF", | ||
283 | ); | ||
284 | } | ||
285 | |||
286 | #[test] | ||
287 | fn convert_octal_integer() { | ||
288 | let before = "const _: i32 = 0o377<|>;"; | ||
289 | |||
290 | check_assist_by_label( | ||
291 | convert_integer_literal, | ||
292 | before, | ||
293 | "const _: i32 = 0b11111111;", | ||
294 | "Convert 0o377 to 0b11111111", | ||
295 | ); | ||
296 | |||
297 | check_assist_by_label( | ||
298 | convert_integer_literal, | ||
299 | before, | ||
300 | "const _: i32 = 255;", | ||
301 | "Convert 0o377 to 255", | ||
302 | ); | ||
303 | |||
304 | check_assist_by_label( | ||
305 | convert_integer_literal, | ||
306 | before, | ||
307 | "const _: i32 = 0xFF;", | ||
308 | "Convert 0o377 to 0xFF", | ||
309 | ); | ||
310 | } | ||
311 | |||
312 | #[test] | ||
313 | fn convert_decimal_integer_with_underscores() { | ||
314 | let before = "const _: i32 = 1_00_0<|>;"; | ||
315 | |||
316 | check_assist_by_label( | ||
317 | convert_integer_literal, | ||
318 | before, | ||
319 | "const _: i32 = 0b1111101000;", | ||
320 | "Convert 1000 to 0b1111101000", | ||
321 | ); | ||
322 | |||
323 | check_assist_by_label( | ||
324 | convert_integer_literal, | ||
325 | before, | ||
326 | "const _: i32 = 0o1750;", | ||
327 | "Convert 1000 to 0o1750", | ||
328 | ); | ||
329 | |||
330 | check_assist_by_label( | ||
331 | convert_integer_literal, | ||
332 | before, | ||
333 | "const _: i32 = 0x3E8;", | ||
334 | "Convert 1000 to 0x3E8", | ||
335 | ); | ||
336 | } | ||
337 | |||
338 | #[test] | ||
339 | fn convert_small_decimal_integer_with_underscores() { | ||
340 | let before = "const _: i32 = 1_0<|>;"; | ||
341 | |||
342 | check_assist_by_label( | ||
343 | convert_integer_literal, | ||
344 | before, | ||
345 | "const _: i32 = 0b1010;", | ||
346 | "Convert 10 to 0b1010", | ||
347 | ); | ||
348 | |||
349 | check_assist_by_label( | ||
350 | convert_integer_literal, | ||
351 | before, | ||
352 | "const _: i32 = 0o12;", | ||
353 | "Convert 10 to 0o12", | ||
354 | ); | ||
355 | |||
356 | check_assist_by_label( | ||
357 | convert_integer_literal, | ||
358 | before, | ||
359 | "const _: i32 = 0xA;", | ||
360 | "Convert 10 to 0xA", | ||
361 | ); | ||
362 | } | ||
363 | |||
364 | #[test] | ||
365 | fn convert_hexadecimal_integer_with_underscores() { | ||
366 | let before = "const _: i32 = 0x_F_F<|>;"; | ||
367 | |||
368 | check_assist_by_label( | ||
369 | convert_integer_literal, | ||
370 | before, | ||
371 | "const _: i32 = 0b11111111;", | ||
372 | "Convert 0xFF to 0b11111111", | ||
373 | ); | ||
374 | |||
375 | check_assist_by_label( | ||
376 | convert_integer_literal, | ||
377 | before, | ||
378 | "const _: i32 = 0o377;", | ||
379 | "Convert 0xFF to 0o377", | ||
380 | ); | ||
381 | |||
382 | check_assist_by_label( | ||
383 | convert_integer_literal, | ||
384 | before, | ||
385 | "const _: i32 = 255;", | ||
386 | "Convert 0xFF to 255", | ||
387 | ); | ||
388 | } | ||
389 | |||
390 | #[test] | ||
391 | fn convert_binary_integer_with_underscores() { | ||
392 | let before = "const _: i32 = 0b1111_1111<|>;"; | ||
393 | |||
394 | check_assist_by_label( | ||
395 | convert_integer_literal, | ||
396 | before, | ||
397 | "const _: i32 = 0o377;", | ||
398 | "Convert 0b11111111 to 0o377", | ||
399 | ); | ||
400 | |||
401 | check_assist_by_label( | ||
402 | convert_integer_literal, | ||
403 | before, | ||
404 | "const _: i32 = 255;", | ||
405 | "Convert 0b11111111 to 255", | ||
406 | ); | ||
407 | |||
408 | check_assist_by_label( | ||
409 | convert_integer_literal, | ||
410 | before, | ||
411 | "const _: i32 = 0xFF;", | ||
412 | "Convert 0b11111111 to 0xFF", | ||
413 | ); | ||
414 | } | ||
415 | |||
416 | #[test] | ||
417 | fn convert_octal_integer_with_underscores() { | ||
418 | let before = "const _: i32 = 0o3_77<|>;"; | ||
419 | |||
420 | check_assist_by_label( | ||
421 | convert_integer_literal, | ||
422 | before, | ||
423 | "const _: i32 = 0b11111111;", | ||
424 | "Convert 0o377 to 0b11111111", | ||
425 | ); | ||
426 | |||
427 | check_assist_by_label( | ||
428 | convert_integer_literal, | ||
429 | before, | ||
430 | "const _: i32 = 255;", | ||
431 | "Convert 0o377 to 255", | ||
432 | ); | ||
433 | |||
434 | check_assist_by_label( | ||
435 | convert_integer_literal, | ||
436 | before, | ||
437 | "const _: i32 = 0xFF;", | ||
438 | "Convert 0o377 to 0xFF", | ||
439 | ); | ||
440 | } | ||
441 | |||
442 | #[test] | ||
443 | fn convert_decimal_integer_with_suffix() { | ||
444 | let before = "const _: i32 = 1000i32<|>;"; | ||
445 | |||
446 | check_assist_by_label( | ||
447 | convert_integer_literal, | ||
448 | before, | ||
449 | "const _: i32 = 0b1111101000i32;", | ||
450 | "Convert 1000 (i32) to 0b1111101000", | ||
451 | ); | ||
452 | |||
453 | check_assist_by_label( | ||
454 | convert_integer_literal, | ||
455 | before, | ||
456 | "const _: i32 = 0o1750i32;", | ||
457 | "Convert 1000 (i32) to 0o1750", | ||
458 | ); | ||
459 | |||
460 | check_assist_by_label( | ||
461 | convert_integer_literal, | ||
462 | before, | ||
463 | "const _: i32 = 0x3E8i32;", | ||
464 | "Convert 1000 (i32) to 0x3E8", | ||
465 | ); | ||
466 | } | ||
467 | |||
468 | #[test] | ||
469 | fn convert_small_decimal_integer_with_suffix() { | ||
470 | let before = "const _: i32 = 10i32<|>;"; | ||
471 | |||
472 | check_assist_by_label( | ||
473 | convert_integer_literal, | ||
474 | before, | ||
475 | "const _: i32 = 0b1010i32;", | ||
476 | "Convert 10 (i32) to 0b1010", | ||
477 | ); | ||
478 | |||
479 | check_assist_by_label( | ||
480 | convert_integer_literal, | ||
481 | before, | ||
482 | "const _: i32 = 0o12i32;", | ||
483 | "Convert 10 (i32) to 0o12", | ||
484 | ); | ||
485 | |||
486 | check_assist_by_label( | ||
487 | convert_integer_literal, | ||
488 | before, | ||
489 | "const _: i32 = 0xAi32;", | ||
490 | "Convert 10 (i32) to 0xA", | ||
491 | ); | ||
492 | } | ||
493 | |||
494 | #[test] | ||
495 | fn convert_hexadecimal_integer_with_suffix() { | ||
496 | let before = "const _: i32 = 0xFFi32<|>;"; | ||
497 | |||
498 | check_assist_by_label( | ||
499 | convert_integer_literal, | ||
500 | before, | ||
501 | "const _: i32 = 0b11111111i32;", | ||
502 | "Convert 0xFF (i32) to 0b11111111", | ||
503 | ); | ||
504 | |||
505 | check_assist_by_label( | ||
506 | convert_integer_literal, | ||
507 | before, | ||
508 | "const _: i32 = 0o377i32;", | ||
509 | "Convert 0xFF (i32) to 0o377", | ||
510 | ); | ||
511 | |||
512 | check_assist_by_label( | ||
513 | convert_integer_literal, | ||
514 | before, | ||
515 | "const _: i32 = 255i32;", | ||
516 | "Convert 0xFF (i32) to 255", | ||
517 | ); | ||
518 | } | ||
519 | |||
520 | #[test] | ||
521 | fn convert_binary_integer_with_suffix() { | ||
522 | let before = "const _: i32 = 0b11111111i32<|>;"; | ||
523 | |||
524 | check_assist_by_label( | ||
525 | convert_integer_literal, | ||
526 | before, | ||
527 | "const _: i32 = 0o377i32;", | ||
528 | "Convert 0b11111111 (i32) to 0o377", | ||
529 | ); | ||
530 | |||
531 | check_assist_by_label( | ||
532 | convert_integer_literal, | ||
533 | before, | ||
534 | "const _: i32 = 255i32;", | ||
535 | "Convert 0b11111111 (i32) to 255", | ||
536 | ); | ||
537 | |||
538 | check_assist_by_label( | ||
539 | convert_integer_literal, | ||
540 | before, | ||
541 | "const _: i32 = 0xFFi32;", | ||
542 | "Convert 0b11111111 (i32) to 0xFF", | ||
543 | ); | ||
544 | } | ||
545 | |||
546 | #[test] | ||
547 | fn convert_octal_integer_with_suffix() { | ||
548 | let before = "const _: i32 = 0o377i32<|>;"; | ||
549 | |||
550 | check_assist_by_label( | ||
551 | convert_integer_literal, | ||
552 | before, | ||
553 | "const _: i32 = 0b11111111i32;", | ||
554 | "Convert 0o377 (i32) to 0b11111111", | ||
555 | ); | ||
556 | |||
557 | check_assist_by_label( | ||
558 | convert_integer_literal, | ||
559 | before, | ||
560 | "const _: i32 = 255i32;", | ||
561 | "Convert 0o377 (i32) to 255", | ||
562 | ); | ||
563 | |||
564 | check_assist_by_label( | ||
565 | convert_integer_literal, | ||
566 | before, | ||
567 | "const _: i32 = 0xFFi32;", | ||
568 | "Convert 0o377 (i32) to 0xFF", | ||
569 | ); | ||
570 | } | ||
571 | |||
572 | #[test] | ||
573 | fn convert_decimal_integer_with_underscores_and_suffix() { | ||
574 | let before = "const _: i32 = 1_00_0i32<|>;"; | ||
575 | |||
576 | check_assist_by_label( | ||
577 | convert_integer_literal, | ||
578 | before, | ||
579 | "const _: i32 = 0b1111101000i32;", | ||
580 | "Convert 1000 (i32) to 0b1111101000", | ||
581 | ); | ||
582 | |||
583 | check_assist_by_label( | ||
584 | convert_integer_literal, | ||
585 | before, | ||
586 | "const _: i32 = 0o1750i32;", | ||
587 | "Convert 1000 (i32) to 0o1750", | ||
588 | ); | ||
589 | |||
590 | check_assist_by_label( | ||
591 | convert_integer_literal, | ||
592 | before, | ||
593 | "const _: i32 = 0x3E8i32;", | ||
594 | "Convert 1000 (i32) to 0x3E8", | ||
595 | ); | ||
596 | } | ||
597 | |||
598 | #[test] | ||
599 | fn convert_small_decimal_integer_with_underscores_and_suffix() { | ||
600 | let before = "const _: i32 = 1_0i32<|>;"; | ||
601 | |||
602 | check_assist_by_label( | ||
603 | convert_integer_literal, | ||
604 | before, | ||
605 | "const _: i32 = 0b1010i32;", | ||
606 | "Convert 10 (i32) to 0b1010", | ||
607 | ); | ||
608 | |||
609 | check_assist_by_label( | ||
610 | convert_integer_literal, | ||
611 | before, | ||
612 | "const _: i32 = 0o12i32;", | ||
613 | "Convert 10 (i32) to 0o12", | ||
614 | ); | ||
615 | |||
616 | check_assist_by_label( | ||
617 | convert_integer_literal, | ||
618 | before, | ||
619 | "const _: i32 = 0xAi32;", | ||
620 | "Convert 10 (i32) to 0xA", | ||
621 | ); | ||
622 | } | ||
623 | |||
624 | #[test] | ||
625 | fn convert_hexadecimal_integer_with_underscores_and_suffix() { | ||
626 | let before = "const _: i32 = 0x_F_Fi32<|>;"; | ||
627 | |||
628 | check_assist_by_label( | ||
629 | convert_integer_literal, | ||
630 | before, | ||
631 | "const _: i32 = 0b11111111i32;", | ||
632 | "Convert 0xFF (i32) to 0b11111111", | ||
633 | ); | ||
634 | |||
635 | check_assist_by_label( | ||
636 | convert_integer_literal, | ||
637 | before, | ||
638 | "const _: i32 = 0o377i32;", | ||
639 | "Convert 0xFF (i32) to 0o377", | ||
640 | ); | ||
641 | |||
642 | check_assist_by_label( | ||
643 | convert_integer_literal, | ||
644 | before, | ||
645 | "const _: i32 = 255i32;", | ||
646 | "Convert 0xFF (i32) to 255", | ||
647 | ); | ||
648 | } | ||
649 | |||
650 | #[test] | ||
651 | fn convert_binary_integer_with_underscores_and_suffix() { | ||
652 | let before = "const _: i32 = 0b1111_1111i32<|>;"; | ||
653 | |||
654 | check_assist_by_label( | ||
655 | convert_integer_literal, | ||
656 | before, | ||
657 | "const _: i32 = 0o377i32;", | ||
658 | "Convert 0b11111111 (i32) to 0o377", | ||
659 | ); | ||
660 | |||
661 | check_assist_by_label( | ||
662 | convert_integer_literal, | ||
663 | before, | ||
664 | "const _: i32 = 255i32;", | ||
665 | "Convert 0b11111111 (i32) to 255", | ||
666 | ); | ||
667 | |||
668 | check_assist_by_label( | ||
669 | convert_integer_literal, | ||
670 | before, | ||
671 | "const _: i32 = 0xFFi32;", | ||
672 | "Convert 0b11111111 (i32) to 0xFF", | ||
673 | ); | ||
674 | } | ||
675 | |||
676 | #[test] | ||
677 | fn convert_octal_integer_with_underscores_and_suffix() { | ||
678 | let before = "const _: i32 = 0o3_77i32<|>;"; | ||
679 | |||
680 | check_assist_by_label( | ||
681 | convert_integer_literal, | ||
682 | before, | ||
683 | "const _: i32 = 0b11111111i32;", | ||
684 | "Convert 0o377 (i32) to 0b11111111", | ||
685 | ); | ||
686 | |||
687 | check_assist_by_label( | ||
688 | convert_integer_literal, | ||
689 | before, | ||
690 | "const _: i32 = 255i32;", | ||
691 | "Convert 0o377 (i32) to 255", | ||
692 | ); | ||
693 | |||
694 | check_assist_by_label( | ||
695 | convert_integer_literal, | ||
696 | before, | ||
697 | "const _: i32 = 0xFFi32;", | ||
698 | "Convert 0o377 (i32) to 0xFF", | ||
699 | ); | ||
700 | } | ||
701 | } | ||
diff --git a/crates/assists/src/lib.rs b/crates/assists/src/lib.rs index cbac53e71..a2bec818c 100644 --- a/crates/assists/src/lib.rs +++ b/crates/assists/src/lib.rs | |||
@@ -128,6 +128,7 @@ mod handlers { | |||
128 | mod auto_import; | 128 | mod auto_import; |
129 | mod change_return_type_to_result; | 129 | mod change_return_type_to_result; |
130 | mod change_visibility; | 130 | mod change_visibility; |
131 | mod convert_integer_literal; | ||
131 | mod early_return; | 132 | mod early_return; |
132 | mod expand_glob_import; | 133 | mod expand_glob_import; |
133 | mod extract_struct_from_enum_variant; | 134 | mod extract_struct_from_enum_variant; |
@@ -172,6 +173,7 @@ mod handlers { | |||
172 | auto_import::auto_import, | 173 | auto_import::auto_import, |
173 | change_return_type_to_result::change_return_type_to_result, | 174 | change_return_type_to_result::change_return_type_to_result, |
174 | change_visibility::change_visibility, | 175 | change_visibility::change_visibility, |
176 | convert_integer_literal::convert_integer_literal, | ||
175 | early_return::convert_to_guarded_return, | 177 | early_return::convert_to_guarded_return, |
176 | expand_glob_import::expand_glob_import, | 178 | expand_glob_import::expand_glob_import, |
177 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, | 179 | extract_struct_from_enum_variant::extract_struct_from_enum_variant, |
diff --git a/crates/assists/src/tests.rs b/crates/assists/src/tests.rs index ba1fb543b..2b687decf 100644 --- a/crates/assists/src/tests.rs +++ b/crates/assists/src/tests.rs | |||
@@ -15,18 +15,30 @@ pub(crate) fn with_single_file(text: &str) -> (RootDatabase, FileId) { | |||
15 | 15 | ||
16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { | 16 | pub(crate) fn check_assist(assist: Handler, ra_fixture_before: &str, ra_fixture_after: &str) { |
17 | let ra_fixture_after = trim_indent(ra_fixture_after); | 17 | let ra_fixture_after = trim_indent(ra_fixture_after); |
18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after)); | 18 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), None); |
19 | } | ||
20 | |||
21 | // There is no way to choose what assist within a group you want to test against, | ||
22 | // so this is here to allow you choose. | ||
23 | pub(crate) fn check_assist_by_label( | ||
24 | assist: Handler, | ||
25 | ra_fixture_before: &str, | ||
26 | ra_fixture_after: &str, | ||
27 | label: &str, | ||
28 | ) { | ||
29 | let ra_fixture_after = trim_indent(ra_fixture_after); | ||
30 | check(assist, ra_fixture_before, ExpectedResult::After(&ra_fixture_after), Some(label)); | ||
19 | } | 31 | } |
20 | 32 | ||
21 | // FIXME: instead of having a separate function here, maybe use | 33 | // FIXME: instead of having a separate function here, maybe use |
22 | // `extract_ranges` and mark the target as `<target> </target>` in the | 34 | // `extract_ranges` and mark the target as `<target> </target>` in the |
23 | // fixture? | 35 | // fixture? |
24 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { | 36 | pub(crate) fn check_assist_target(assist: Handler, ra_fixture: &str, target: &str) { |
25 | check(assist, ra_fixture, ExpectedResult::Target(target)); | 37 | check(assist, ra_fixture, ExpectedResult::Target(target), None); |
26 | } | 38 | } |
27 | 39 | ||
28 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { | 40 | pub(crate) fn check_assist_not_applicable(assist: Handler, ra_fixture: &str) { |
29 | check(assist, ra_fixture, ExpectedResult::NotApplicable); | 41 | check(assist, ra_fixture, ExpectedResult::NotApplicable, None); |
30 | } | 42 | } |
31 | 43 | ||
32 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { | 44 | fn check_doc_test(assist_id: &str, before: &str, after: &str) { |
@@ -65,7 +77,7 @@ enum ExpectedResult<'a> { | |||
65 | Target(&'a str), | 77 | Target(&'a str), |
66 | } | 78 | } |
67 | 79 | ||
68 | fn check(handler: Handler, before: &str, expected: ExpectedResult) { | 80 | fn check(handler: Handler, before: &str, expected: ExpectedResult, assist_label: Option<&str>) { |
69 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); | 81 | let (db, file_with_caret_id, range_or_offset) = RootDatabase::with_range_or_offset(before); |
70 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); | 82 | let text_without_caret = db.file_text(file_with_caret_id).to_string(); |
71 | 83 | ||
@@ -77,7 +89,12 @@ fn check(handler: Handler, before: &str, expected: ExpectedResult) { | |||
77 | let mut acc = Assists::new_resolved(&ctx); | 89 | let mut acc = Assists::new_resolved(&ctx); |
78 | handler(&mut acc, &ctx); | 90 | handler(&mut acc, &ctx); |
79 | let mut res = acc.finish_resolved(); | 91 | let mut res = acc.finish_resolved(); |
80 | let assist = res.pop(); | 92 | |
93 | let assist = match assist_label { | ||
94 | Some(label) => res.into_iter().find(|resolved| resolved.assist.label == label), | ||
95 | None => res.pop(), | ||
96 | }; | ||
97 | |||
81 | match (assist, expected) { | 98 | match (assist, expected) { |
82 | (Some(assist), ExpectedResult::After(after)) => { | 99 | (Some(assist), ExpectedResult::After(after)) => { |
83 | let mut source_change = assist.source_change; | 100 | let mut source_change = assist.source_change; |
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index 27d15adb0..7f6e98a54 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -204,6 +204,19 @@ pub(crate) fn frobnicate() {} | |||
204 | } | 204 | } |
205 | 205 | ||
206 | #[test] | 206 | #[test] |
207 | fn doctest_convert_integer_literal() { | ||
208 | check_doc_test( | ||
209 | "convert_integer_literal", | ||
210 | r#####" | ||
211 | const _: i32 = 10<|>; | ||
212 | "#####, | ||
213 | r#####" | ||
214 | const _: i32 = 0b1010; | ||
215 | "#####, | ||
216 | ) | ||
217 | } | ||
218 | |||
219 | #[test] | ||
207 | fn doctest_convert_to_guarded_return() { | 220 | fn doctest_convert_to_guarded_return() { |
208 | check_doc_test( | 221 | check_doc_test( |
209 | "convert_to_guarded_return", | 222 | "convert_to_guarded_return", |