Skip to content

Commit

Permalink
feat(fmt): ternary expression (foundry-rs#2120)
Browse files Browse the repository at this point in the history
* ternary expr

* upd readme
  • Loading branch information
rkrasiuk authored Jun 25, 2022
1 parent e3afcd9 commit d06692d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fmt/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ See [Expression](https://github.com/hyperledger-labs/solang/blob/413841b5c759eb8
- [ ] NamedFunctionCall
- [x] New
- [x] Delete
- [ ] Ternary
- [x] Ternary
- [x] Type
- [x] Address
- [x] Address Payable
Expand Down
26 changes: 26 additions & 0 deletions fmt/src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,6 +1536,31 @@ impl<'a, W: Write> Visitor for Formatter<'a, W> {
Ok(())
})?;
}
Expression::Ternary(loc, cond, first_expr, second_expr) => {
let mut chunks = vec![];

chunks.push(
self.chunked(loc.start(), Some(first_expr.loc().start()), |fmt| {
cond.visit(fmt)
})?,
);
chunks.push(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)
})?);

if !self.try_on_single_line(|fmt| fmt.write_chunks_separated(&chunks, "", false))? {
self.grouped(|fmt| fmt.write_chunks_separated(&chunks, "", true))?;
}
}
Expression::Variable(ident) => {
write_chunk!(self, loc.end(), "{}", ident.name)?;
}
Expand Down Expand Up @@ -2773,4 +2798,5 @@ mod tests {
test_directory! { RevertNamedArgsStatement }
test_directory! { ReturnStatement }
test_directory! { TryStatement }
test_directory! { TernaryExpression }
}
23 changes: 23 additions & 0 deletions fmt/testdata/TernaryExpression/fmt.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
contract TernaryExpression {
function test() external {
bool condition;
bool someVeryVeryLongConditionUsedInTheTernaryExpression;

condition ? 0 : 1;

someVeryVeryLongConditionUsedInTheTernaryExpression
? 1234567890
: 987654321;

condition /* comment1 */ /* comment2 */
? 1001 /* comment3 */ /* comment4 */
: 2002;

// comment5
someVeryVeryLongConditionUsedInTheTernaryExpression
? 1
// comment6
// comment7
: 0; // comment8
}
}
19 changes: 19 additions & 0 deletions fmt/testdata/TernaryExpression/original.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
contract TernaryExpression {
function test() external {
bool condition;
bool someVeryVeryLongConditionUsedInTheTernaryExpression;

condition ? 0 : 1;

someVeryVeryLongConditionUsedInTheTernaryExpression ? 1234567890 : 987654321;

condition /* comment1 */ ? /* comment2 */ 1001 /* comment3 */ : /* comment4 */ 2002;

// comment5
someVeryVeryLongConditionUsedInTheTernaryExpression ? 1
// comment6
:
// comment7
0; // comment8
}
}

0 comments on commit d06692d

Please sign in to comment.