From 3f360f0101db03232703c2aa02330ea2d95722b9 Mon Sep 17 00:00:00 2001 From: Cesar <142530682+cr-fuel@users.noreply.github.com> Date: Fri, 20 Oct 2023 16:43:48 -0300 Subject: [PATCH] Check if vector elements are too long to fit in a single line --- swayfmt/src/config/heuristics.rs | 11 ++++++-- swayfmt/src/constants.rs | 2 +- swayfmt/src/utils/language/punctuated.rs | 34 ++++++++++++++++++++---- swayfmt/tests/mod.rs | 23 +++++++++++++++- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/swayfmt/src/config/heuristics.rs b/swayfmt/src/config/heuristics.rs index 5af64221c62..7ceda21d853 100644 --- a/swayfmt/src/config/heuristics.rs +++ b/swayfmt/src/config/heuristics.rs @@ -3,8 +3,9 @@ use crate::{ config::user_opts::HeuristicsOptions, constants::{ DEFAULT_ATTR_FN_LIKE_WIDTH, DEFAULT_CHAIN_WIDTH, DEFAULT_COLLECTION_WIDTH, - DEFAULT_FN_CALL_WIDTH, DEFAULT_MAX_LINE_WIDTH, DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH, - DEFAULT_STRUCTURE_LIT_WIDTH, DEFAULT_STRUCTURE_VAR_WIDTH, + DEFAULT_FN_CALL_WIDTH, DEFAULT_MAX_LINE_WIDTH, DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD, + DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH, DEFAULT_STRUCTURE_LIT_WIDTH, + DEFAULT_STRUCTURE_VAR_WIDTH, }, }; use serde::{Deserialize, Serialize}; @@ -84,6 +85,7 @@ pub struct WidthHeuristics { // Maximum line length for single line if-else expressions. A value // of zero means always break if-else expressions. pub(crate) single_line_if_else_max_width: usize, + pub(crate) short_array_element_width: usize, } impl WidthHeuristics { @@ -97,6 +99,7 @@ impl WidthHeuristics { collection_width: usize::max_value(), chain_width: usize::max_value(), single_line_if_else_max_width: 0, + short_array_element_width: 0, } } @@ -109,6 +112,7 @@ impl WidthHeuristics { collection_width: max_width, chain_width: max_width, single_line_if_else_max_width: max_width, + short_array_element_width: max_width, } } @@ -135,6 +139,9 @@ impl WidthHeuristics { single_line_if_else_max_width: (DEFAULT_SINGLE_LINE_IF_ELSE_WIDTH as f32 * max_width_ratio) .round() as usize, + short_array_element_width: (DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD as f32 + * max_width_ratio) + .round() as usize, } } } diff --git a/swayfmt/src/constants.rs b/swayfmt/src/constants.rs index 174ca19f053..1a536cf601c 100644 --- a/swayfmt/src/constants.rs +++ b/swayfmt/src/constants.rs @@ -20,7 +20,7 @@ pub const DEFAULT_STRUCTURE_LIT_WIDTH: usize = 18; pub const DEFAULT_STRUCTURE_VAR_WIDTH: usize = 35; /// Default Maximum width of an array literal before falling back to vertical formatting. pub const DEFAULT_COLLECTION_WIDTH: usize = 60; -/// Defalt width threshold for an array element to be considered short. +/// Default width threshold for an array element to be considered short. pub const DEFAULT_SHORT_ARRAY_ELEM_WIDTH_THRESHOLD: usize = 10; /// Default max length of a chain to fit on a single line. pub const DEFAULT_CHAIN_WIDTH: usize = 60; diff --git a/swayfmt/src/utils/language/punctuated.rs b/swayfmt/src/utils/language/punctuated.rs index 923f9d73317..1c136983cfe 100644 --- a/swayfmt/src/utils/language/punctuated.rs +++ b/swayfmt/src/utils/language/punctuated.rs @@ -66,15 +66,39 @@ where formatter.write_indent_into_buffer(formatted_code)?; } - let mut iter = self.value_separator_pairs.iter().peekable(); + let mut is_value_too_long = false; + let value_separator_pairs = formatter.with_shape( + formatter.shape.with_default_code_line(), + |formatter| -> Result, FormatterError> { + self.value_separator_pairs + .iter() + .map(|(type_field, comma_token)| { + let mut field = FormattedCode::new(); + let mut comma = FormattedCode::new(); + type_field.format(&mut field, formatter)?; + comma_token.format(&mut comma, formatter)?; + if field.len() + > formatter.shape.width_heuristics.short_array_element_width + { + is_value_too_long = true; + } + Ok(( + field.trim_start().to_owned(), + comma.trim_start().to_owned(), + )) + }) + .collect() + }, + )?; + + let mut iter = value_separator_pairs.iter().peekable(); while let Some((type_field, comma_token)) = iter.next() { - type_field.format(formatted_code, formatter)?; - comma_token.format(formatted_code, formatter)?; + write!(formatted_code, "{}{}", type_field, comma_token)?; if iter.peek().is_none() && self.final_value_opt.is_none() { break; } - if should_write_multiline(formatted_code, formatter) { + if is_value_too_long || should_write_multiline(formatted_code, formatter) { writeln!(formatted_code)?; formatter.write_indent_into_buffer(formatted_code)?; } else { @@ -86,7 +110,7 @@ where write!(formatted_code, "{}", PunctKind::Comma.as_char())?; } if !formatted_code.ends_with('\n') { - writeln!(formatted_code, "")?; + writeln!(formatted_code)?; } } } diff --git a/swayfmt/tests/mod.rs b/swayfmt/tests/mod.rs index b55857ffb92..a8deabf8a6d 100644 --- a/swayfmt/tests/mod.rs +++ b/swayfmt/tests/mod.rs @@ -1163,7 +1163,9 @@ fn main() { fn main() { if pledge_history_index != 0 { // This is a comment - storage.pledge_history.insert((user, pledge_history_index), pledge); + storage + .pledge_history + .insert((user, pledge_history_index), pledge); } // This is also a comment, // but multiline @@ -1869,3 +1871,22 @@ trait MyComplexTrait { "#, ); } + +#[test] +fn long_array() { + check( + r#"library; + + fn main() { + let x = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22]; + }"#, + r#"library; +fn main() { + let x = [ + 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, + 17, 18, 19, 20, 21, 22, + ]; +} +"#, + ); +}