diff options
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r-- | crates/assists/src/handlers/convert_integer_literal.rs | 23 |
1 files changed, 8 insertions, 15 deletions
diff --git a/crates/assists/src/handlers/convert_integer_literal.rs b/crates/assists/src/handlers/convert_integer_literal.rs index 6d477c045..5957834d3 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 | ||
@@ -14,15 +14,13 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; | |||
14 | // const _: i32 = 0b1010; | 14 | // const _: i32 = 0b1010; |
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>()?.as_int_number()?; |
18 | let (radix, value) = literal.as_int_number()?.value()?; | 18 | let radix = literal.radix(); |
19 | let value = literal.value()?; | ||
20 | let suffix = literal.suffix(); | ||
19 | 21 | ||
20 | let range = literal.syntax().text_range(); | 22 | let range = literal.syntax().text_range(); |
21 | let group_id = GroupLabel("Convert integer base".into()); | 23 | 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 | 24 | ||
27 | for &target_radix in Radix::ALL { | 25 | for &target_radix in Radix::ALL { |
28 | if target_radix == radix { | 26 | if target_radix == radix { |
@@ -36,16 +34,11 @@ pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> | |||
36 | Radix::Hexadecimal => format!("0x{:X}", value), | 34 | Radix::Hexadecimal => format!("0x{:X}", value), |
37 | }; | 35 | }; |
38 | 36 | ||
39 | let label = format!( | 37 | 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 | 38 | ||
46 | // Appends the type suffix back into the new literal if it exists. | 39 | // Appends the type suffix back into the new literal if it exists. |
47 | if let Some(suffix) = &suffix { | 40 | if let Some(suffix) = suffix { |
48 | converted.push_str(&suffix); | 41 | converted.push_str(suffix); |
49 | } | 42 | } |
50 | 43 | ||
51 | acc.add_group( | 44 | acc.add_group( |