Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds multiline formatting for array #2593

Merged
merged 6 commits into from
Aug 20, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions sway-fmt-v2/src/utils/expr/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl Format for ExprArrayDescriptor {
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
Self::open_square_bracket(formatted_code, formatter)?;
match self {
Self::Sequence(punct_expr) => {
punct_expr.format(formatted_code, formatter)?;
Expand All @@ -105,6 +106,7 @@ impl Format for ExprArrayDescriptor {
length.format(formatted_code, formatter)?;
}
}
Self::close_square_bracket(formatted_code, formatter)?;

Ok(())
}
Expand All @@ -118,7 +120,7 @@ impl SquareBracket for ExprArrayDescriptor {
match formatter.shape.code_line.line_style {
LineStyle::Multiline => {
formatter.shape.block_indent(&formatter.config);
writeln!(line, "{}", Delimiter::Bracket.as_open_char())?;
write!(line, "{}", Delimiter::Bracket.as_open_char())?;
}
_ => write!(line, "{}", Delimiter::Bracket.as_open_char())?,
}
Expand All @@ -134,7 +136,7 @@ impl SquareBracket for ExprArrayDescriptor {
formatter.shape.block_unindent(&formatter.config);
write!(
line,
"\n{}{}",
"{}{}",
formatter.shape.indent.to_string(&formatter.config)?,
Delimiter::Bracket.as_close_char()
)?;
Expand Down
38 changes: 34 additions & 4 deletions sway-fmt-v2/src/utils/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
};
use std::fmt::Write;
use sway_ast::{
brackets::Parens,
brackets::{Parens, SquareBrackets},
keywords::{CommaToken, DotToken},
punctuated::Punctuated,
token::Delimiter,
Expand Down Expand Up @@ -112,9 +112,29 @@ impl Format for Expr {
CodeBlockContents::close_curly_brace(formatted_code, formatter)?;
}
Self::Array(array_descriptor) => {
ExprArrayDescriptor::open_square_bracket(formatted_code, formatter)?;
array_descriptor.get().format(formatted_code, formatter)?;
ExprArrayDescriptor::close_square_bracket(formatted_code, formatter)?;
// store previous state and update expr kind
let prev_state = formatter.shape.code_line;
formatter
.shape
.code_line
.update_expr_kind(ExprKind::Collection);

// get the length in chars of the code_line in a normal line format
let mut buf = FormattedCode::new();
let mut temp_formatter = Formatter::default();
format_array(array_descriptor, &mut buf, &mut temp_formatter)?;
let body_width = buf.chars().count() as usize;

formatter.shape.add_width(body_width);
formatter
.shape
.get_line_style(None, Some(body_width), &formatter.config);

format_array(array_descriptor, formatted_code, formatter)?;

// revert to previous state
formatter.shape.sub_width(body_width);
formatter.shape.update_line_settings(prev_state);
}
Self::Asm(asm_block) => asm_block.format(formatted_code, formatter)?,
Self::Return {
Expand Down Expand Up @@ -540,6 +560,16 @@ fn format_tuple(
Ok(())
}

fn format_array(
array_descriptor: &SquareBrackets<ExprArrayDescriptor>,
formatted_code: &mut FormattedCode,
formatter: &mut Formatter,
) -> Result<(), FormatterError> {
array_descriptor.get().format(formatted_code, formatter)?;

Ok(())
}

fn format_method_call(
target: &Expr,
dot_token: &DotToken,
Expand Down
66 changes: 66 additions & 0 deletions sway-fmt-v2/src/utils/expr/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,30 @@ fmt_test!( multiline_tuple "(\n \"reallyreallylongstring\",\n \"y
\"okaynowthatsjustaridiculouslylongstringrightthere\")"
);

fmt_test!( nested_tuple
"(
(
0x0000000000000000000000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000000000000000000000,
),
(
0x0000000000000000000000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000000000000000000000,
),
)",
intermediate_whitespace
" (
(
0x0000000000000000000000000000000000000000000000000000000000 ,
0x0000000000000000000000000000000000000000000000000000000000 ,
) ,
(
0x0000000000000000000000000000000000000000000000000000000000 ,
0x0000000000000000000000000000000000000000000000000000000000 ,
) ,
)"
);

fmt_test!( multiline_match_stmt "match foo {\n Foo::foo => {}\n Foo::bar => {}\n}",
intermediate_whitespace " match \n foo { \n\n Foo :: foo => { }\n Foo :: bar => { } \n}\n"
);
Expand Down Expand Up @@ -144,3 +168,45 @@ fmt_test!( match_branch_kind
{\n baz();\n
quux();\n }\n\n\n}"
);

fmt_test!( basic_array "[1, 2, 3, 4, 5]",
intermediate_whitespace " \n [ 1 , 2 , 3 , 4 , 5 ] \n"
);

fmt_test!( long_array
"[
\"hello_there_this_is_a_very_long_string\",
\"and_yet_another_very_long_string_just_because\",
\"would_you_look_at_that_another_long_string\",
]",
intermediate_whitespace
" [
\"hello_there_this_is_a_very_long_string\",
\"and_yet_another_very_long_string_just_because\"\n,
\"would_you_look_at_that_another_long_string\",
] \n"
);

fmt_test!( nested_array
"[
[
0x0000000000000000000000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000000000000000000000,
],
[
0x0000000000000000000000000000000000000000000000000000000000,
0x0000000000000000000000000000000000000000000000000000000000,
],
]",
intermediate_whitespace
" [
[
0x0000000000000000000000000000000000000000000000000000000000 ,
0x0000000000000000000000000000000000000000000000000000000000 ,
] ,
[
0x0000000000000000000000000000000000000000000000000000000000 ,
0x0000000000000000000000000000000000000000000000000000000000 ,
] ,
]"
);