Skip to content

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 3 times, most recently from 940a796 to 5120041 Compare May 9, 2025 12:19
@Janther Janther closed this May 17, 2025
@Janther Janther deleted the binaryOperation-review branch May 17, 2025 07:30
@Janther Janther restored the binaryOperation-review branch May 17, 2025 07:31
@Janther Janther reopened this May 17, 2025
@Janther Janther force-pushed the binaryOperation-review branch from 5120041 to 770942f Compare June 9, 2025 10:33
@Janther Janther merged commit afe81b0 into main Jun 9, 2025
7 checks passed
@Janther Janther deleted the binaryOperation-review branch June 9, 2025 10:35
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.

3 participants