Skip to content

Commit

Permalink
Fix formatting of single multiline call in assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasWanke committed Mar 27, 2023
1 parent f250796 commit 926a8c8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 21 deletions.
63 changes: 44 additions & 19 deletions compiler/formatter/src/format.rs
Expand Up @@ -35,6 +35,13 @@ impl FormattingInfo {
is_single_expression_in_assignment_body: false,
}
}
pub fn with_dedent(&self) -> Self {
Self {
indentation: self.indentation.with_dedent(),
trailing_comma_condition: None,
is_single_expression_in_assignment_body: false,
}
}
pub fn with_trailing_comma_condition(&self, condition: TrailingCommaCondition) -> Self {
Self {
indentation: self.indentation,
Expand All @@ -44,11 +51,25 @@ impl FormattingInfo {
}
pub fn for_single_expression_in_assignment_body(&self) -> Self {
Self {
indentation: self.indentation,
indentation: self.indentation.with_indent(),
trailing_comma_condition: None,
is_single_expression_in_assignment_body: true,
}
}
pub fn resolve_for_sandwich_like(&self, previous_width: &Width) -> Self {
Self {
indentation: if self.is_single_expression_in_assignment_body
&& previous_width
.last_line_fits(self.indentation, &SinglelineWidth::PARENTHESIS.into())
{
self.indentation.with_dedent()
} else {
self.indentation
},
trailing_comma_condition: None,
is_single_expression_in_assignment_body: false,
}
}
}

pub fn format_csts<'a>(
Expand Down Expand Up @@ -360,17 +381,7 @@ pub(crate) fn format_cst<'a>(
return receiver;
}

let info_for_arguments = if info.is_single_expression_in_assignment_body
&& previous_width
.last_line_fits(info.indentation, &receiver.min_width(info.indentation))
{
info.to_owned()
} else {
info.with_indent()
};

let previous_width_for_arguments =
Width::multiline(None, info_for_arguments.indentation.width());
let previous_width_for_arguments = Width::multiline(None, info.indentation.width());
let last_argument_index = arguments.len() - 1;
let mut arguments = arguments
.iter()
Expand All @@ -380,7 +391,7 @@ pub(crate) fn format_cst<'a>(
edits,
&previous_width_for_arguments,
argument,
&info_for_arguments,
&info.with_indent(),
index == last_argument_index,
)
})
Expand All @@ -395,11 +406,10 @@ pub(crate) fn format_cst<'a>(
if previous_width.last_line_fits(info.indentation, &min_width) {
(true, info.to_owned(), TrailingWhitespace::Space)
} else {
let indentation = info_for_arguments.indentation;
(
false,
info_for_arguments,
TrailingWhitespace::Indentation(indentation),
info.with_indent(),
TrailingWhitespace::Indentation(info.indentation.with_indent()),
)
};

Expand All @@ -424,11 +434,17 @@ pub(crate) fn format_cst<'a>(
&last_argument.argument,
MaybeSandwichLikeArgument::SandwichLike(_)
);
let info_for_last_argument =
if info.is_single_expression_in_assignment_body && is_singleline {
argument_info.with_dedent()
} else {
argument_info.clone()

Check failure on line 441 in compiler/formatter/src/format.rs

View workflow job for this annotation

GitHub Actions / Check

redundant clone
};
let (last_argument_width, whitespace) = last_argument
.format(
edits,
&(previous_width + &width),
&argument_info,
&info_for_last_argument,
is_singleline,
)
.split();
Expand Down Expand Up @@ -698,7 +714,9 @@ pub(crate) fn format_cst<'a>(
body,
closing_curly_brace,
} => {
let opening_curly_brace = format_cst(edits, previous_width, opening_curly_brace, info);
let info = info.resolve_for_sandwich_like(previous_width);

let opening_curly_brace = format_cst(edits, previous_width, opening_curly_brace, &info);

let previous_width_for_inner =
Width::multiline(None, info.indentation.with_indent().width());
Expand Down Expand Up @@ -767,7 +785,7 @@ pub(crate) fn format_cst<'a>(
edits,
&Width::multiline(None, info.indentation.width()),
closing_curly_brace,
info,
&info,
)
.split();

Expand Down Expand Up @@ -1773,6 +1791,13 @@ mod test {
"foo = bar {\n baz\n blub\n}",
"foo = bar {\n baz\n blub\n}\n",
);
// looooooooooooooooongIdentifier =
// function
// loooooooooooooooooooooooooooooooooooooooooooooooongArgument
test(
"looooooooooooooooongIdentifier = function loooooooooooooooooooooooooooooooooooooooooooooooongArgument",
"looooooooooooooooongIdentifier =\n function\n loooooooooooooooooooooooooooooooooooooooooooooooongArgument\n",
);

// Function definition

Expand Down
6 changes: 4 additions & 2 deletions compiler/formatter/src/format_collection.rs
Expand Up @@ -17,12 +17,14 @@ pub fn format_collection<'a>(
is_comma_required_for_single_item: bool,
info: &FormattingInfo,
) -> FormattedCst<'a> {
let opening_punctuation = format_cst(edits, previous_width, opening_punctuation, info);
let info = info.resolve_for_sandwich_like(previous_width);

let opening_punctuation = format_cst(edits, previous_width, opening_punctuation, &info);
let closing_punctuation = format_cst(
edits,
&Width::multiline(None, info.indentation.width()),
closing_punctuation,
info,
&info,
);

let mut min_width = Width::Singleline(info.indentation.width())
Expand Down
3 changes: 3 additions & 0 deletions compiler/formatter/src/width.rs
Expand Up @@ -20,6 +20,9 @@ impl Indentation {
pub fn with_indent(self) -> Self {
Self(self.0 + 1)
}
pub fn with_dedent(self) -> Self {
Self(self.0 - 1)
}
}
impl Display for Indentation {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
Expand Down

1 comment on commit 926a8c8

@jwbot
Copy link
Collaborator

@jwbot jwbot commented on 926a8c8 Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Compiler

Benchmark suite Current: 926a8c8 Previous: eb02a1f Ratio
Time: Compiler/hello_world 19756036 ns/iter (± 2011897) 19914795 ns/iter (± 905259) 0.99
Time: Compiler/fibonacci 180630821 ns/iter (± 1942941) 186966516 ns/iter (± 1556543) 0.97
Time: VM Runtime/hello_world 17981934 ns/iter (± 596925) 17942540 ns/iter (± 226567) 1.00
Time: VM Runtime/fibonacci/15 198507609 ns/iter (± 1262923) 201151148 ns/iter (± 3327528) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.