Skip to content

Binary operation grouping and indentation review #1133

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Janther
Copy link
Contributor

@Janther Janther commented May 2, 2025

To visually display the operation hierarchy when adding parentheses is too much, prettier groups and indents binary operations (visually matching the behaviour of adding parentheses).
The hierarchy is as follows:

  1. Exponentiation **
  2. Multiplication *, /, %
  3. Addition +, -
  4. Shift Operation <<, >>
  5. Bit Operation |, &, ^
  6. Inequality <, >, <=, >=
  7. Equality ==, !=
  8. logical &&, ||

Many of these cases were already taken care of by adding parentheses in unclear cases or when everything would fit in a single line.

// Original
x = a * b + c;
x = a + b * c;
x = veryLongNameA * veryLongNameB + veryLongNameC;
x = veryLongNameA + veryLongNameB * veryLongNameC;
x = veryVeryLongNameA * veryVeryLongNameB + veryVeryLongNameC;
x = veryVeryLongNameA + veryVeryLongNameB * veryVeryLongNameC;
// just to compare with the parentheses behaviour
x = a * b / c;
x = veryLongNameA * veryLongNameB / veryLongNameC;
x = veryVeryLongNameA * veryVeryLongNameB / veryVeryLongNameC;

// Current Format
x = a * b + c;
x = a + b * c;
x =
    veryLongNameA *
    veryLongNameB +
    veryLongNameC;
x =
    veryLongNameA +
    veryLongNameB *
    veryLongNameC;
x =
    veryVeryLongNameA *
    veryVeryLongNameB +
    veryVeryLongNameC;
x =
    veryVeryLongNameA +
    veryVeryLongNameB *
    veryVeryLongNameC;
// just to compare with the parentheses behaviour
x = (a * b) / c;
x =
    (veryLongNameA * veryLongNameB) /
    veryLongNameC;
x =
    (veryVeryLongNameA *
        veryVeryLongNameB) /
    veryVeryLongNameC;

// New Format
x = a * b + c;
x = a + b * c;
x =
    veryLongNameA * veryLongNameB +
    veryLongNameC;
x =
    veryLongNameA +
    veryLongNameB * veryLongNameC;
x =
    veryVeryLongNameA *
        veryVeryLongNameB +
    veryVeryLongNameC;
x =
    veryVeryLongNameA +
    veryVeryLongNameB *
        veryVeryLongNameC;
// just to compare with the parentheses behaviour
x = (a * b) / c;
x =
    (veryLongNameA * veryLongNameB) /
    veryLongNameC;
x =
    (veryVeryLongNameA *
        veryVeryLongNameB) /
    veryVeryLongNameC;

Since all of these behaviours follow the same rule there was a some work put standardising the printers in such a way that the same printer is used for all operations and the only difference is where in the hierarchy the current operator lands.

this was part of #1097 that had too many topics at once.

@Janther Janther requested a review from fvictorio May 2, 2025 12:22
@Janther Janther force-pushed the binaryOperation-review branch 2 times, most recently from dabd67b to 940a796 Compare May 6, 2025 20:52
@Janther Janther force-pushed the binaryOperation-review branch from 940a796 to 5120041 Compare May 9, 2025 12:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant