aboutsummaryrefslogtreecommitdiff
path: root/crates/assists
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-11-06 18:10:17 +0000
committerGitHub <[email protected]>2020-11-06 18:10:17 +0000
commit0d5be44b6e0ccd3e806697dcfc2337a2c0ed5914 (patch)
tree1f048935ea6b51cc1e5f01d0613552264b31f02d /crates/assists
parent2c408c68a4a27e9a85ce85ec1342e3af58c1571f (diff)
parenteb460333907a44c37bf7287b31c653877c3358c2 (diff)
Merge #6482
6482: More orthogonal API r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/assists')
-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 c8af80701..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.int_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(