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

feat: improve binary expression formatting #594

Conversation

jtkiesel
Copy link
Contributor

@jtkiesel jtkiesel commented Aug 5, 2023

What changed with this PR:

Binary expressions are now broken, indented, and wrapped with parentheses similar to the way Prettier JavaScript does. I tried to mimic the behavior of Prettier JavaScript as much as possible, except that this PR never removes parentheses around binary expressions, only adds them.

Example

Input

class Example {

  void example() {
    2 / 3 * 10 / 2 + 2;
    2 * 3 * 10 / 2 + 2;
    a % 10 - 5;
    a - 10 % 5;
    a * b % 10;
    a % b * 10;
    a % 10 > 5;
    1 << 2 >>> 3 >> 4;
    1 << 2 + 3;
    1 / 2 << 3;
    x == y == z;
    1 & 2 == 3;

    if (aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd && eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh || iiiiiiiiii) {}

    if (a * b + c << d < e == f & g ^ h | i && j || k && l | m ^ n & o != p > q >> r - s / t) {}

    if (aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd && eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh || iiiiiiiiii + jjjjjjjjjj == kkkkkkkkkk + llllllllll && mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp || qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt && uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx) {}
  }
}

Output

class Example {

  void example() {
    ((2 / 3) * 10) / 2 + 2;
    (2 * 3 * 10) / 2 + 2;
    (a % 10) - 5;
    a - (10 % 5);
    (a * b) % 10;
    (a % b) * 10;
    a % 10 > 5;
    ((1 << 2) >>> 3) >> 4;
    1 << (2 + 3);
    (1 / 2) << 3;
    (x == y) == z;
    1 & (2 == 3);

    if (
      (aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
        eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh) ||
      iiiiiiiiii
    ) {}

    if (
      (((((a * b + c) << d < e == f) & g) ^ h) | i && j) ||
      (k && l | (m ^ (n & (o != p > q >> (r - s / t)))))
    ) {}

    if (
      (aaaaaaaaaa + bbbbbbbbbb == cccccccccc + dddddddddd &&
        eeeeeeeeee + ffffffffff == gggggggggg + hhhhhhhhhh) ||
      (iiiiiiiiii + jjjjjjjjjj == kkkkkkkkkk + llllllllll &&
        mmmmmmmmmm + nnnnnnnnnn == oooooooooo + pppppppppp) ||
      (qqqqqqqqqq + rrrrrrrrrr == ssssssssss + tttttttttt &&
        uuuuuuuuuu + vvvvvvvvvv == wwwwwwwwww + xxxxxxxxxxx)
    ) {}
  }
}

Relative issues or prs:

None

@jtkiesel jtkiesel force-pushed the feat/improve-binary-expression-formatting branch from 9ea175c to 896a03c Compare August 5, 2023 06:51
@clementdessoude
Copy link
Contributor

Wow ! Impressive job @jtkiesel ! I had a first look today, and it seems really good, but as this part is complex, I would like to have another look tomorrow if possible :)

Thanks a lot !

@pascalgrimaud @jdubois, @jtkiesel is doing a great job on Prettier Java this last few weeks, maybe it would be possible to assign a bounty on one or several of the PRs, WDYT ?

@DanielFran DanielFran added $$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $200 labels Aug 12, 2023
@clementdessoude clementdessoude merged commit b98cf67 into jhipster:main Aug 15, 2023
6 checks passed
@jtkiesel jtkiesel deleted the feat/improve-binary-expression-formatting branch August 16, 2023 02:01
@jtkiesel
Copy link
Contributor Author

@DanielFran
Copy link
Member

@jtkiesel approved

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
$$ bug-bounty $$ https://www.jhipster.tech/bug-bounties/ $200
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants