From 5c775ced3933879dfcddb139bf7fc534ea6c6000 Mon Sep 17 00:00:00 2001 From: Giacomo Cavalieri Date: Tue, 14 Jul 2026 14:27:15 +0200 Subject: [PATCH] nice thousands separator --- language-server/src/code_action.rs | 23 ++++++++++++++++++- language-server/src/tests/action.rs | 9 ++++++++ ...rt_to_int_has_nicely_separated_digits.snap | 11 +++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 language-server/src/tests/snapshots/gleam_language_server__tests__action__convert_to_int_has_nicely_separated_digits.snap diff --git a/language-server/src/code_action.rs b/language-server/src/code_action.rs index b05e1f2f3..94de66895 100644 --- a/language-server/src/code_action.rs +++ b/language-server/src/code_action.rs @@ -13156,8 +13156,10 @@ impl<'a> ConvertIntToDifferentBase<'a> { let converted_number = match base { Base::Binary => format!("{minus}0b{:b}", int), Base::Octal => format!("{minus}0o{:o}", int), - Base::Decimal => format!("{minus}{}", int), Base::Hexadecimal => format!("{minus}0x{:x}", int), + Base::Decimal => { + format!("{minus}{}", format_int_with_thousands_separator(&int)) + } }; let title = format!("Convert to `{converted_number}`"); self.edits.replace(location, converted_number); @@ -13175,6 +13177,25 @@ impl<'a> ConvertIntToDifferentBase<'a> { } } +fn format_int_with_thousands_separator(int: &BigInt) -> String { + if int <= &BigInt::from(9999) { + return int.to_string(); + } + + // We get chunks of three digits. If we start from 1234567 + // we will have `765`, `432`, `1` + (int.to_string().chars().rev().chunks(3).into_iter()) + // Each chunk is turned into a string and those are joined with a + // separator. + .map(|chunk| chunk.collect::()) + .join("_") + // And finally reverse everything to bring it back to normal, the number + // is spelled in reverse right now! + .chars() + .rev() + .collect() +} + impl<'ast> ast::visit::Visit<'ast> for ConvertIntToDifferentBase<'ast> { fn visit_typed_function(&mut self, fun: &'ast TypedFunction) { // We skip all the functions the cursor is not inside of. diff --git a/language-server/src/tests/action.rs b/language-server/src/tests/action.rs index 56c731f04..be60d4a8d 100644 --- a/language-server/src/tests/action.rs +++ b/language-server/src/tests/action.rs @@ -15817,3 +15817,12 @@ fn convert_negative_int_hexadecimal_to_decimal() { find_position_of("0x6f").to_selection() ); } + +#[test] +fn convert_to_int_has_nicely_separated_digits() { + assert_code_action!( + "Convert to `1_234_567`", + "pub fn main() { 0b100101101011010000111 }", + find_position_of("0b100101101011010000111").to_selection() + ); +} diff --git a/language-server/src/tests/snapshots/gleam_language_server__tests__action__convert_to_int_has_nicely_separated_digits.snap b/language-server/src/tests/snapshots/gleam_language_server__tests__action__convert_to_int_has_nicely_separated_digits.snap new file mode 100644 index 000000000..c0dadc0a7 --- /dev/null +++ b/language-server/src/tests/snapshots/gleam_language_server__tests__action__convert_to_int_has_nicely_separated_digits.snap @@ -0,0 +1,11 @@ +--- +source: language-server/src/tests/action.rs +expression: "pub fn main() { 0b100101101011010000111 }" +--- +----- BEFORE ACTION +pub fn main() { 0b100101101011010000111 } + ↑ + + +----- AFTER ACTION +pub fn main() { 1_234_567 } -- 2.51.2