diff options
Diffstat (limited to 'crates/assists/src/handlers/convert_integer_literal.rs')
-rw-r--r-- | crates/assists/src/handlers/convert_integer_literal.rs | 395 |
1 files changed, 13 insertions, 382 deletions
diff --git a/crates/assists/src/handlers/convert_integer_literal.rs b/crates/assists/src/handlers/convert_integer_literal.rs index c8af80701..667115382 100644 --- a/crates/assists/src/handlers/convert_integer_literal.rs +++ b/crates/assists/src/handlers/convert_integer_literal.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use syntax::{ast, ast::Radix, AstNode}; | 1 | use syntax::{ast, ast::Radix, AstToken}; |
2 | 2 | ||
3 | use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | 3 | use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; |
4 | 4 | ||
@@ -15,14 +15,16 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | |||
15 | // ``` | 15 | // ``` |
16 | pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { | 16 | pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
17 | let literal = ctx.find_node_at_offset::<ast::Literal>()?; | 17 | let literal = ctx.find_node_at_offset::<ast::Literal>()?; |
18 | let (radix, value) = literal.int_value()?; | 18 | let literal = match literal.kind() { |
19 | ast::LiteralKind::IntNumber(it) => it, | ||
20 | _ => return None, | ||
21 | }; | ||
22 | let radix = literal.radix(); | ||
23 | let value = literal.value()?; | ||
24 | let suffix = literal.suffix(); | ||
19 | 25 | ||
20 | let range = literal.syntax().text_range(); | 26 | let range = literal.syntax().text_range(); |
21 | let group_id = GroupLabel("Convert integer base".into()); | 27 | let group_id = GroupLabel("Convert integer base".into()); |
22 | let suffix = match literal.kind() { | ||
23 | ast::LiteralKind::IntNumber { suffix } => suffix, | ||
24 | _ => return None, | ||
25 | }; | ||
26 | 28 | ||
27 | for &target_radix in Radix::ALL { | 29 | for &target_radix in Radix::ALL { |
28 | if target_radix == radix { | 30 | if target_radix == radix { |
@@ -36,16 +38,11 @@ pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> | |||
36 | Radix::Hexadecimal => format!("0x{:X}", value), | 38 | Radix::Hexadecimal => format!("0x{:X}", value), |
37 | }; | 39 | }; |
38 | 40 | ||
39 | let label = format!( | 41 | let label = format!("Convert {} to {}{}", literal, converted, suffix.unwrap_or_default()); |
40 | "Convert {} to {}{}", | ||
41 | literal, | ||
42 | converted, | ||
43 | suffix.as_deref().unwrap_or_default() | ||
44 | ); | ||
45 | 42 | ||
46 | // Appends the type suffix back into the new literal if it exists. | 43 | // Appends the type suffix back into the new literal if it exists. |
47 | if let Some(suffix) = &suffix { | 44 | if let Some(suffix) = suffix { |
48 | converted.push_str(&suffix); | 45 | converted.push_str(suffix); |
49 | } | 46 | } |
50 | 47 | ||
51 | acc.add_group( | 48 | acc.add_group( |
@@ -132,34 +129,6 @@ mod tests { | |||
132 | ); | 129 | ); |
133 | } | 130 | } |
134 | 131 | ||
135 | // Decimal numbers under 3 digits have a special case where they return early because we can't fit a | ||
136 | // other base's prefix, so we have a separate test for that. | ||
137 | #[test] | ||
138 | fn convert_small_decimal_integer() { | ||
139 | let before = "const _: i32 = 10<|>;"; | ||
140 | |||
141 | check_assist_by_label( | ||
142 | convert_integer_literal, | ||
143 | before, | ||
144 | "const _: i32 = 0b1010;", | ||
145 | "Convert 10 to 0b1010", | ||
146 | ); | ||
147 | |||
148 | check_assist_by_label( | ||
149 | convert_integer_literal, | ||
150 | before, | ||
151 | "const _: i32 = 0o12;", | ||
152 | "Convert 10 to 0o12", | ||
153 | ); | ||
154 | |||
155 | check_assist_by_label( | ||
156 | convert_integer_literal, | ||
157 | before, | ||
158 | "const _: i32 = 0xA;", | ||
159 | "Convert 10 to 0xA", | ||
160 | ); | ||
161 | } | ||
162 | |||
163 | #[test] | 132 | #[test] |
164 | fn convert_hexadecimal_integer() { | 133 | fn convert_hexadecimal_integer() { |
165 | let before = "const _: i32 = 0xFF<|>;"; | 134 | let before = "const _: i32 = 0xFF<|>;"; |
@@ -239,7 +208,7 @@ mod tests { | |||
239 | } | 208 | } |
240 | 209 | ||
241 | #[test] | 210 | #[test] |
242 | fn convert_decimal_integer_with_underscores() { | 211 | fn convert_integer_with_underscores() { |
243 | let before = "const _: i32 = 1_00_0<|>;"; | 212 | let before = "const _: i32 = 1_00_0<|>;"; |
244 | 213 | ||
245 | check_assist_by_label( | 214 | check_assist_by_label( |
@@ -265,111 +234,7 @@ mod tests { | |||
265 | } | 234 | } |
266 | 235 | ||
267 | #[test] | 236 | #[test] |
268 | fn convert_small_decimal_integer_with_underscores() { | 237 | fn convert_integer_with_suffix() { |
269 | let before = "const _: i32 = 1_0<|>;"; | ||
270 | |||
271 | check_assist_by_label( | ||
272 | convert_integer_literal, | ||
273 | before, | ||
274 | "const _: i32 = 0b1010;", | ||
275 | "Convert 1_0 to 0b1010", | ||
276 | ); | ||
277 | |||
278 | check_assist_by_label( | ||
279 | convert_integer_literal, | ||
280 | before, | ||
281 | "const _: i32 = 0o12;", | ||
282 | "Convert 1_0 to 0o12", | ||
283 | ); | ||
284 | |||
285 | check_assist_by_label( | ||
286 | convert_integer_literal, | ||
287 | before, | ||
288 | "const _: i32 = 0xA;", | ||
289 | "Convert 1_0 to 0xA", | ||
290 | ); | ||
291 | } | ||
292 | |||
293 | #[test] | ||
294 | fn convert_hexadecimal_integer_with_underscores() { | ||
295 | let before = "const _: i32 = 0x_F_F<|>;"; | ||
296 | |||
297 | check_assist_by_label( | ||
298 | convert_integer_literal, | ||
299 | before, | ||
300 | "const _: i32 = 0b11111111;", | ||
301 | "Convert 0x_F_F to 0b11111111", | ||
302 | ); | ||
303 | |||
304 | check_assist_by_label( | ||
305 | convert_integer_literal, | ||
306 | before, | ||
307 | "const _: i32 = 0o377;", | ||
308 | "Convert 0x_F_F to 0o377", | ||
309 | ); | ||
310 | |||
311 | check_assist_by_label( | ||
312 | convert_integer_literal, | ||
313 | before, | ||
314 | "const _: i32 = 255;", | ||
315 | "Convert 0x_F_F to 255", | ||
316 | ); | ||
317 | } | ||
318 | |||
319 | #[test] | ||
320 | fn convert_binary_integer_with_underscores() { | ||
321 | let before = "const _: i32 = 0b1111_1111<|>;"; | ||
322 | |||
323 | check_assist_by_label( | ||
324 | convert_integer_literal, | ||
325 | before, | ||
326 | "const _: i32 = 0o377;", | ||
327 | "Convert 0b1111_1111 to 0o377", | ||
328 | ); | ||
329 | |||
330 | check_assist_by_label( | ||
331 | convert_integer_literal, | ||
332 | before, | ||
333 | "const _: i32 = 255;", | ||
334 | "Convert 0b1111_1111 to 255", | ||
335 | ); | ||
336 | |||
337 | check_assist_by_label( | ||
338 | convert_integer_literal, | ||
339 | before, | ||
340 | "const _: i32 = 0xFF;", | ||
341 | "Convert 0b1111_1111 to 0xFF", | ||
342 | ); | ||
343 | } | ||
344 | |||
345 | #[test] | ||
346 | fn convert_octal_integer_with_underscores() { | ||
347 | let before = "const _: i32 = 0o3_77<|>;"; | ||
348 | |||
349 | check_assist_by_label( | ||
350 | convert_integer_literal, | ||
351 | before, | ||
352 | "const _: i32 = 0b11111111;", | ||
353 | "Convert 0o3_77 to 0b11111111", | ||
354 | ); | ||
355 | |||
356 | check_assist_by_label( | ||
357 | convert_integer_literal, | ||
358 | before, | ||
359 | "const _: i32 = 255;", | ||
360 | "Convert 0o3_77 to 255", | ||
361 | ); | ||
362 | |||
363 | check_assist_by_label( | ||
364 | convert_integer_literal, | ||
365 | before, | ||
366 | "const _: i32 = 0xFF;", | ||
367 | "Convert 0o3_77 to 0xFF", | ||
368 | ); | ||
369 | } | ||
370 | |||
371 | #[test] | ||
372 | fn convert_decimal_integer_with_suffix() { | ||
373 | let before = "const _: i32 = 1000i32<|>;"; | 238 | let before = "const _: i32 = 1000i32<|>;"; |
374 | 239 | ||
375 | check_assist_by_label( | 240 | check_assist_by_label( |
@@ -395,240 +260,6 @@ mod tests { | |||
395 | } | 260 | } |
396 | 261 | ||
397 | #[test] | 262 | #[test] |
398 | fn convert_small_decimal_integer_with_suffix() { | ||
399 | let before = "const _: i32 = 10i32<|>;"; | ||
400 | |||
401 | check_assist_by_label( | ||
402 | convert_integer_literal, | ||
403 | before, | ||
404 | "const _: i32 = 0b1010i32;", | ||
405 | "Convert 10i32 to 0b1010i32", | ||
406 | ); | ||
407 | |||
408 | check_assist_by_label( | ||
409 | convert_integer_literal, | ||
410 | before, | ||
411 | "const _: i32 = 0o12i32;", | ||
412 | "Convert 10i32 to 0o12i32", | ||
413 | ); | ||
414 | |||
415 | check_assist_by_label( | ||
416 | convert_integer_literal, | ||
417 | before, | ||
418 | "const _: i32 = 0xAi32;", | ||
419 | "Convert 10i32 to 0xAi32", | ||
420 | ); | ||
421 | } | ||
422 | |||
423 | #[test] | ||
424 | fn convert_hexadecimal_integer_with_suffix() { | ||
425 | let before = "const _: i32 = 0xFFi32<|>;"; | ||
426 | |||
427 | check_assist_by_label( | ||
428 | convert_integer_literal, | ||
429 | before, | ||
430 | "const _: i32 = 0b11111111i32;", | ||
431 | "Convert 0xFFi32 to 0b11111111i32", | ||
432 | ); | ||
433 | |||
434 | check_assist_by_label( | ||
435 | convert_integer_literal, | ||
436 | before, | ||
437 | "const _: i32 = 0o377i32;", | ||
438 | "Convert 0xFFi32 to 0o377i32", | ||
439 | ); | ||
440 | |||
441 | check_assist_by_label( | ||
442 | convert_integer_literal, | ||
443 | before, | ||
444 | "const _: i32 = 255i32;", | ||
445 | "Convert 0xFFi32 to 255i32", | ||
446 | ); | ||
447 | } | ||
448 | |||
449 | #[test] | ||
450 | fn convert_binary_integer_with_suffix() { | ||
451 | let before = "const _: i32 = 0b11111111i32<|>;"; | ||
452 | |||
453 | check_assist_by_label( | ||
454 | convert_integer_literal, | ||
455 | before, | ||
456 | "const _: i32 = 0o377i32;", | ||
457 | "Convert 0b11111111i32 to 0o377i32", | ||
458 | ); | ||
459 | |||
460 | check_assist_by_label( | ||
461 | convert_integer_literal, | ||
462 | before, | ||
463 | "const _: i32 = 255i32;", | ||
464 | "Convert 0b11111111i32 to 255i32", | ||
465 | ); | ||
466 | |||
467 | check_assist_by_label( | ||
468 | convert_integer_literal, | ||
469 | before, | ||
470 | "const _: i32 = 0xFFi32;", | ||
471 | "Convert 0b11111111i32 to 0xFFi32", | ||
472 | ); | ||
473 | } | ||
474 | |||
475 | #[test] | ||
476 | fn convert_octal_integer_with_suffix() { | ||
477 | let before = "const _: i32 = 0o377i32<|>;"; | ||
478 | |||
479 | check_assist_by_label( | ||
480 | convert_integer_literal, | ||
481 | before, | ||
482 | "const _: i32 = 0b11111111i32;", | ||
483 | "Convert 0o377i32 to 0b11111111i32", | ||
484 | ); | ||
485 | |||
486 | check_assist_by_label( | ||
487 | convert_integer_literal, | ||
488 | before, | ||
489 | "const _: i32 = 255i32;", | ||
490 | "Convert 0o377i32 to 255i32", | ||
491 | ); | ||
492 | |||
493 | check_assist_by_label( | ||
494 | convert_integer_literal, | ||
495 | before, | ||
496 | "const _: i32 = 0xFFi32;", | ||
497 | "Convert 0o377i32 to 0xFFi32", | ||
498 | ); | ||
499 | } | ||
500 | |||
501 | #[test] | ||
502 | fn convert_decimal_integer_with_underscores_and_suffix() { | ||
503 | let before = "const _: i32 = 1_00_0i32<|>;"; | ||
504 | |||
505 | check_assist_by_label( | ||
506 | convert_integer_literal, | ||
507 | before, | ||
508 | "const _: i32 = 0b1111101000i32;", | ||
509 | "Convert 1_00_0i32 to 0b1111101000i32", | ||
510 | ); | ||
511 | |||
512 | check_assist_by_label( | ||
513 | convert_integer_literal, | ||
514 | before, | ||
515 | "const _: i32 = 0o1750i32;", | ||
516 | "Convert 1_00_0i32 to 0o1750i32", | ||
517 | ); | ||
518 | |||
519 | check_assist_by_label( | ||
520 | convert_integer_literal, | ||
521 | before, | ||
522 | "const _: i32 = 0x3E8i32;", | ||
523 | "Convert 1_00_0i32 to 0x3E8i32", | ||
524 | ); | ||
525 | } | ||
526 | |||
527 | #[test] | ||
528 | fn convert_small_decimal_integer_with_underscores_and_suffix() { | ||
529 | let before = "const _: i32 = 1_0i32<|>;"; | ||
530 | |||
531 | check_assist_by_label( | ||
532 | convert_integer_literal, | ||
533 | before, | ||
534 | "const _: i32 = 0b1010i32;", | ||
535 | "Convert 1_0i32 to 0b1010i32", | ||
536 | ); | ||
537 | |||
538 | check_assist_by_label( | ||
539 | convert_integer_literal, | ||
540 | before, | ||
541 | "const _: i32 = 0o12i32;", | ||
542 | "Convert 1_0i32 to 0o12i32", | ||
543 | ); | ||
544 | |||
545 | check_assist_by_label( | ||
546 | convert_integer_literal, | ||
547 | before, | ||
548 | "const _: i32 = 0xAi32;", | ||
549 | "Convert 1_0i32 to 0xAi32", | ||
550 | ); | ||
551 | } | ||
552 | |||
553 | #[test] | ||
554 | fn convert_hexadecimal_integer_with_underscores_and_suffix() { | ||
555 | let before = "const _: i32 = 0x_F_Fi32<|>;"; | ||
556 | |||
557 | check_assist_by_label( | ||
558 | convert_integer_literal, | ||
559 | before, | ||
560 | "const _: i32 = 0b11111111i32;", | ||
561 | "Convert 0x_F_Fi32 to 0b11111111i32", | ||
562 | ); | ||
563 | |||
564 | check_assist_by_label( | ||
565 | convert_integer_literal, | ||
566 | before, | ||
567 | "const _: i32 = 0o377i32;", | ||
568 | "Convert 0x_F_Fi32 to 0o377i32", | ||
569 | ); | ||
570 | |||
571 | check_assist_by_label( | ||
572 | convert_integer_literal, | ||
573 | before, | ||
574 | "const _: i32 = 255i32;", | ||
575 | "Convert 0x_F_Fi32 to 255i32", | ||
576 | ); | ||
577 | } | ||
578 | |||
579 | #[test] | ||
580 | fn convert_binary_integer_with_underscores_and_suffix() { | ||
581 | let before = "const _: i32 = 0b1111_1111i32<|>;"; | ||
582 | |||
583 | check_assist_by_label( | ||
584 | convert_integer_literal, | ||
585 | before, | ||
586 | "const _: i32 = 0o377i32;", | ||
587 | "Convert 0b1111_1111i32 to 0o377i32", | ||
588 | ); | ||
589 | |||
590 | check_assist_by_label( | ||
591 | convert_integer_literal, | ||
592 | before, | ||
593 | "const _: i32 = 255i32;", | ||
594 | "Convert 0b1111_1111i32 to 255i32", | ||
595 | ); | ||
596 | |||
597 | check_assist_by_label( | ||
598 | convert_integer_literal, | ||
599 | before, | ||
600 | "const _: i32 = 0xFFi32;", | ||
601 | "Convert 0b1111_1111i32 to 0xFFi32", | ||
602 | ); | ||
603 | } | ||
604 | |||
605 | #[test] | ||
606 | fn convert_octal_integer_with_underscores_and_suffix() { | ||
607 | let before = "const _: i32 = 0o3_77i32<|>;"; | ||
608 | |||
609 | check_assist_by_label( | ||
610 | convert_integer_literal, | ||
611 | before, | ||
612 | "const _: i32 = 0b11111111i32;", | ||
613 | "Convert 0o3_77i32 to 0b11111111i32", | ||
614 | ); | ||
615 | |||
616 | check_assist_by_label( | ||
617 | convert_integer_literal, | ||
618 | before, | ||
619 | "const _: i32 = 255i32;", | ||
620 | "Convert 0o3_77i32 to 255i32", | ||
621 | ); | ||
622 | |||
623 | check_assist_by_label( | ||
624 | convert_integer_literal, | ||
625 | before, | ||
626 | "const _: i32 = 0xFFi32;", | ||
627 | "Convert 0o3_77i32 to 0xFFi32", | ||
628 | ); | ||
629 | } | ||
630 | |||
631 | #[test] | ||
632 | fn convert_overflowing_literal() { | 263 | fn convert_overflowing_literal() { |
633 | let before = "const _: i32 = | 264 | let before = "const _: i32 = |
634 | 111111111111111111111111111111111111111111111111111111111111111111111111<|>;"; | 265 | 111111111111111111111111111111111111111111111111111111111111111111111111<|>;"; |