Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
rkrasiuk authored and iFrostizz committed Nov 9, 2022
1 parent 13c0bc9 commit f7f7c9f
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
25 changes: 10 additions & 15 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -944,9 +944,7 @@ impl<'a, W: Write> Formatter<'a, W> {
self.write_postfix_comments_before(expr.loc().start())?;
self.write_prefix_comments_before(expr.loc().start())?;

let fits_on_single_line =
self.try_on_single_line(|fmt| fmt.indented(1, |fmt| expr.visit(fmt)))?;
if self.is_beginning_of_line() && fits_on_single_line {
if self.try_on_single_line(|fmt| fmt.indented(1, |fmt| expr.visit(fmt)))? {
return Ok(())
}

Expand Down Expand Up @@ -2005,26 +2003,23 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
self.visit_assignment(right)?;
}
Expression::Ternary(loc, cond, first_expr, second_expr) => {
let mut chunks = vec![];
cond.visit(self)?;

chunks.push(
self.chunked(loc.start(), Some(first_expr.loc().start()), |fmt| {
cond.visit(fmt)
})?,
);
chunks.push(self.chunked(
let first_expr = self.chunked(
first_expr.loc().start(),
Some(second_expr.loc().start()),
|fmt| {
write_chunk!(fmt, "?")?;
first_expr.visit(fmt)
},
)?);
chunks.push(self.chunked(second_expr.loc().start(), Some(loc.end()), |fmt| {
write_chunk!(fmt, ":")?;
second_expr.visit(fmt)
})?);
)?;
let second_expr =
self.chunked(second_expr.loc().start(), Some(loc.end()), |fmt| {
write_chunk!(fmt, ":")?;
second_expr.visit(fmt)
})?;

let chunks = vec![first_expr, second_expr];
if !self.try_on_single_line(|fmt| fmt.write_chunks_separated(&chunks, "", false))? {
self.grouped(|fmt| fmt.write_chunks_separated(&chunks, "", true))?;
}
Expand Down
14 changes: 14 additions & 0 deletions fmt/testdata/TernaryExpression/fmt.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,19 @@ contract TernaryExpression {
// comment6
// comment7
: 0; // comment8

uint256 amount = msg.value > 0
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
? msg.value
// comment9
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
// comment10
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);
}
}
14 changes: 14 additions & 0 deletions fmt/testdata/TernaryExpression/original.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,19 @@ contract TernaryExpression {
:
// comment7
0; // comment8

uint256 amount = msg.value > 0
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint256 amount = msg.value > 0
? msg.value
// comment9
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);

uint amount = msg.value > 0
// comment10
? msg.value
: parseAmount(IERC20(asset).balanceOf(msg.sender), msg.data);
}
}

0 comments on commit f7f7c9f

Please sign in to comment.