From 5d24dfbdfbf13410e74e2af5d53e0df5f7d0eb2e Mon Sep 17 00:00:00 2001 From: Pascal Seitz Date: Sun, 11 Feb 2018 19:44:47 +0100 Subject: [PATCH] scale WidthHeuristics by max_width scale WidthHeuristics by max_width --- rustfmt-config/src/config_type.rs | 3 ++- rustfmt-config/src/lib.rs | 2 +- rustfmt-config/src/options.rs | 20 ++++++++++---------- 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/rustfmt-config/src/config_type.rs b/rustfmt-config/src/config_type.rs index 51642570fdfd..81d1c11f56cc 100644 --- a/rustfmt-config/src/config_type.rs +++ b/rustfmt-config/src/config_type.rs @@ -359,7 +359,8 @@ macro_rules! create_config { fn set_heuristics(&mut self) { if self.use_small_heuristics.2 { - self.set().width_heuristics(WidthHeuristics::default()); + let max_width = self.max_width.2; + self.set().width_heuristics(WidthHeuristics::scaled(max_width)); } else { self.set().width_heuristics(WidthHeuristics::null()); } diff --git a/rustfmt-config/src/lib.rs b/rustfmt-config/src/lib.rs index cbfd236dc37e..3b6dca769d96 100644 --- a/rustfmt-config/src/lib.rs +++ b/rustfmt-config/src/lib.rs @@ -150,7 +150,7 @@ create_config! { file_lines: FileLines, FileLines::all(), false, "Lines to format; this is not supported in rustfmt.toml, and can only be specified \ via the --file-lines option"; - width_heuristics: WidthHeuristics, WidthHeuristics::default(), false, + width_heuristics: WidthHeuristics, WidthHeuristics::scaled(100), false, "'small' heuristic values"; } diff --git a/rustfmt-config/src/options.rs b/rustfmt-config/src/options.rs index bff6d2298d3b..396c5e4fc9f2 100644 --- a/rustfmt-config/src/options.rs +++ b/rustfmt-config/src/options.rs @@ -220,17 +220,17 @@ impl WidthHeuristics { single_line_if_else_max_width: 0, } } -} - -impl Default for WidthHeuristics { - fn default() -> WidthHeuristics { + // scale the default WidthHeuristics according to max_width + pub fn scaled(max_width: usize) -> WidthHeuristics { + let mut max_width_ratio: f32 = max_width as f32 / 100.0; // 100 is the default width -> default ratio is 1 + max_width_ratio = (max_width_ratio * 10.0).round() / 10.0; // round to the closest 0.1 WidthHeuristics { - fn_call_width: 60, - struct_lit_width: 18, - struct_variant_width: 35, - array_width: 60, - chain_width: 60, - single_line_if_else_max_width: 50, + fn_call_width: (60.0 * max_width_ratio).round() as usize, + struct_lit_width: (18.0 * max_width_ratio).round() as usize, + struct_variant_width: (35.0 * max_width_ratio).round() as usize, + array_width: (60.0 * max_width_ratio).round() as usize, + chain_width: (60.0 * max_width_ratio).round() as usize, + single_line_if_else_max_width: (50.0 * max_width_ratio).round() as usize, } } }