aboutsummaryrefslogtreecommitdiff
path: root/crates/assists/src/handlers
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-11-06 18:01:25 +0000
committerAleksey Kladov <[email protected]>2020-11-06 18:09:01 +0000
commiteb460333907a44c37bf7287b31c653877c3358c2 (patch)
tree1f048935ea6b51cc1e5f01d0613552264b31f02d /crates/assists/src/handlers
parent735aaa7b39b4d3d789ad75c167bbf322a65ca257 (diff)
More orthogonal API
Diffstat (limited to 'crates/assists/src/handlers')
-rw-r--r--crates/assists/src/handlers/convert_integer_literal.rs23
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 @@
1use syntax::{ast, ast::Radix, AstNode}; 1use syntax::{ast, ast::Radix, AstToken};
2 2
3use crate::{AssistContext, AssistId, AssistKind, Assists, GroupLabel}; 3use 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// ```
16pub(crate) fn convert_integer_literal(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 16pub(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(