From 44a5b14ca253728381368a6afce68017058c25d5 Mon Sep 17 00:00:00 2001 From: David Barsky Date: Fri, 14 Mar 2025 15:57:47 -0400 Subject: [PATCH] analysis-stats: add `UsizeWithUnderscore` for readability of large numbers --- .../rust-analyzer/src/cli/analysis_stats.rs | 31 ++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs index 97a319f03125..c5186d85d556 100644 --- a/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs +++ b/src/tools/rust-analyzer/crates/rust-analyzer/src/cli/analysis_stats.rs @@ -2,7 +2,7 @@ //! errors. use std::{ - env, + env, fmt, time::{SystemTime, UNIX_EPOCH}, }; @@ -156,6 +156,11 @@ impl flags::AnalysisStats { let item_tree_time = item_tree_sw.elapsed(); eprintln!("Source stats:"); + let dep_loc = UsizeWithUnderscore(dep_loc); + let deps_item_trees = UsizeWithUnderscore(deps_item_trees); + let workspace_loc = UsizeWithUnderscore(workspace_loc); + let workspace_item_trees = UsizeWithUnderscore(workspace_item_trees); + eprintln!(" dependency lines of code: {dep_loc}, item trees: {deps_item_trees}"); eprintln!(" workspace lines of code: {workspace_loc}, item trees: {workspace_item_trees}"); @@ -1250,6 +1255,30 @@ fn percentage(n: u64, total: u64) -> u64 { (n * 100).checked_div(total).unwrap_or(100) } +struct UsizeWithUnderscore(usize); + +impl fmt::Display for UsizeWithUnderscore { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let num_str = self.0.to_string(); + + if num_str.len() <= 3 { + return write!(f, "{}", num_str); + } + + let mut result = String::new(); + + for (count, ch) in num_str.chars().rev().enumerate() { + if count > 0 && count % 3 == 0 { + result.push('_'); + } + result.push(ch); + } + + let result = result.chars().rev().collect::(); + write!(f, "{}", result) + } +} + // FIXME(salsa-transition): bring this back whenever we implement // Salsa's memory usage tracking to work with tracked functions. // fn syntax_len(node: SyntaxNode) -> usize {