⭐️ A friendly language for building type-safe, scalable systems!
1

Configure Feed

Select the types of activity you want to include in your feed.

nice thousands separator

authored by

Giacomo Cavalieri and committed by
Louis Pilfold
(Jul 15, 2026, 10:58 AM +0100) 5c775ced 7a882cbb

+42 -1
+22 -1
language-server/src/code_action.rs
··· 13156 13156 let converted_number = match base { 13157 13157 Base::Binary => format!("{minus}0b{:b}", int), 13158 13158 Base::Octal => format!("{minus}0o{:o}", int), 13159 - Base::Decimal => format!("{minus}{}", int), 13160 13159 Base::Hexadecimal => format!("{minus}0x{:x}", int), 13160 + Base::Decimal => { 13161 + format!("{minus}{}", format_int_with_thousands_separator(&int)) 13162 + } 13161 13163 }; 13162 13164 let title = format!("Convert to `{converted_number}`"); 13163 13165 self.edits.replace(location, converted_number); ··· 13173 13175 } 13174 13176 action 13175 13177 } 13178 + } 13179 + 13180 + fn format_int_with_thousands_separator(int: &BigInt) -> String { 13181 + if int <= &BigInt::from(9999) { 13182 + return int.to_string(); 13183 + } 13184 + 13185 + // We get chunks of three digits. If we start from 1234567 13186 + // we will have `765`, `432`, `1` 13187 + (int.to_string().chars().rev().chunks(3).into_iter()) 13188 + // Each chunk is turned into a string and those are joined with a 13189 + // separator. 13190 + .map(|chunk| chunk.collect::<EcoString>()) 13191 + .join("_") 13192 + // And finally reverse everything to bring it back to normal, the number 13193 + // is spelled in reverse right now! 13194 + .chars() 13195 + .rev() 13196 + .collect() 13176 13197 } 13177 13198 13178 13199 impl<'ast> ast::visit::Visit<'ast> for ConvertIntToDifferentBase<'ast> {
+9
language-server/src/tests/action.rs
··· 15817 15817 find_position_of("0x6f").to_selection() 15818 15818 ); 15819 15819 } 15820 + 15821 + #[test] 15822 + fn convert_to_int_has_nicely_separated_digits() { 15823 + assert_code_action!( 15824 + "Convert to `1_234_567`", 15825 + "pub fn main() { 0b100101101011010000111 }", 15826 + find_position_of("0b100101101011010000111").to_selection() 15827 + ); 15828 + }
+11
language-server/src/tests/snapshots/gleam_language_server__tests__action__convert_to_int_has_nicely_separated_digits.snap
··· 1 + --- 2 + source: language-server/src/tests/action.rs 3 + expression: "pub fn main() { 0b100101101011010000111 }" 4 + --- 5 + ----- BEFORE ACTION 6 + pub fn main() { 0b100101101011010000111 } 7 + 8 + 9 + 10 + ----- AFTER ACTION 11 + pub fn main() { 1_234_567 }