Skip to content

Commit

Permalink
Check if vector elements are too long to fit in a single line
Browse files Browse the repository at this point in the history
  • Loading branch information
cr-fuel committed Oct 21, 2023
1 parent 7da1284 commit 3f360f0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 9 deletions.
11 changes: 9 additions & 2 deletions swayfmt/src/config/heuristics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
}
}

Expand All @@ -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,
}
}

Expand All @@ -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,
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion swayfmt/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
34 changes: 29 additions & 5 deletions swayfmt/src/utils/language/punctuated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<(String, String)>, 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 {
Expand All @@ -86,7 +110,7 @@ where
write!(formatted_code, "{}", PunctKind::Comma.as_char())?;
}
if !formatted_code.ends_with('\n') {
writeln!(formatted_code, "")?;
writeln!(formatted_code)?;
}
}
}
Expand Down
23 changes: 22 additions & 1 deletion swayfmt/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
];
}
"#,
);
}

0 comments on commit 3f360f0

Please sign in to comment.