Unnamed repository; edit this file 'description' to name the repository.
feat: add assist to convert char literal
| -rw-r--r-- | crates/ide-assists/src/handlers/convert_char_literal.rs | 46 | ||||
| -rw-r--r-- | crates/ide-assists/src/lib.rs | 2 |
2 files changed, 48 insertions, 0 deletions
diff --git a/crates/ide-assists/src/handlers/convert_char_literal.rs b/crates/ide-assists/src/handlers/convert_char_literal.rs new file mode 100644 index 0000000000..7269ade006 --- /dev/null +++ b/crates/ide-assists/src/handlers/convert_char_literal.rs @@ -0,0 +1,46 @@ +use syntax::{AstToken, ast}; + +use crate::{AssistContext, AssistId, Assists, GroupLabel}; + +// Assist: convert_char_literal +// +// Converts character literals between different representations. Currently supports normal character -> ASCII / Unicode escape. +pub(crate) fn convert_char_literal(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> { + if !ctx.has_empty_selection() { + return None; + } + + let literal = ctx.find_node_at_offset::<ast::Literal>()?; + let literal = match literal.kind() { + ast::LiteralKind::Char(it) => it, + _ => return None, + }; + + let value = literal.value().ok()?; + let text = literal.syntax().text().to_string(); + let range = literal.syntax().text_range(); + let group_id = GroupLabel("Convert char representation".into()); + + let mut add_assist = |converted: String| { + // Skip no-op assists (e.g. `'const C: char = '\\x61';'` already matches the ASCII form). + if converted == text { + return; + } + let label = format!("Convert {text} to {converted}"); + acc.add_group( + &group_id, + AssistId::refactor_rewrite("convert_char_literal"), + label, + range, + |builder| builder.replace(range, converted), + ); + }; + + if value.is_ascii() { + add_assist(format!("'\\x{:02x}'", value as u32)); + } + + add_assist(format!("'\\u{{{:x}}}'", value as u32)); + + Some(()) +} diff --git a/crates/ide-assists/src/lib.rs b/crates/ide-assists/src/lib.rs index 4b4aa94279..ca468905fb 100644 --- a/crates/ide-assists/src/lib.rs +++ b/crates/ide-assists/src/lib.rs @@ -119,6 +119,7 @@ mod handlers { mod change_visibility; mod convert_bool_then; mod convert_bool_to_enum; + mod convert_char_literal; mod convert_closure_to_fn; mod convert_comment_block; mod convert_comment_from_or_to_doc; @@ -256,6 +257,7 @@ mod handlers { convert_bool_then::convert_bool_then_to_if, convert_bool_then::convert_if_to_bool_then, convert_bool_to_enum::convert_bool_to_enum, + convert_char_literal::convert_char_literal, convert_closure_to_fn::convert_closure_to_fn, convert_comment_block::convert_comment_block, convert_comment_from_or_to_doc::convert_comment_from_or_to_doc, |