Skip to content

Commit

Permalink
feat: support experimentalOperatorPosition option
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkiesel committed Feb 12, 2025
1 parent 79c4d9f commit 2fa40bd
Show file tree
Hide file tree
Showing 10 changed files with 425 additions and 18 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"lerna": "8.1.9",
"lint-staged": "15.2.10",
"mocha": "10.8.2",
"prettier": "3.4.2",
"prettier": "3.5.0",
"sinon": "19.0.2"
}
}
1 change: 1 addition & 0 deletions packages/prettier-plugin-java/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
dist
test-samples/**/*
scripts/single-printer-run
samples/**/*
Expand Down
7 changes: 6 additions & 1 deletion packages/prettier-plugin-java/src/printers/expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,12 @@ export class ExpressionsPrettierVisitor extends BaseCstPrettierPrinter {
);
}

const content = binary(nodes, tokens, true);
const content = binary(
nodes,
tokens,
this.prettierOptions.experimentalOperatorPosition,
true
);

return hasTokens && params?.addParenthesisToWrapStatement
? group(
Expand Down
26 changes: 16 additions & 10 deletions packages/prettier-plugin-java/src/printers/printer-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,12 @@ export function putIntoBraces(
);
}

export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc {
export function binary(
nodes: Doc[],
tokens: IToken[],
operatorPosition?: "start" | "end",
isRoot = false
): Doc {
let levelOperator: string | undefined;
let levelPrecedence: number | undefined;
let level: Doc[] = [];
Expand All @@ -619,23 +624,24 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc {
? nextOperator.length
: 1;
const operator = concat(tokens.splice(0, tokenLength));
level.push(nodes.shift()!);
if (
levelOperator !== undefined &&
needsParentheses(levelOperator, nextOperator)
) {
level.push(nodes.shift()!);
level = [
concat(["(", group(indent(join(line, level))), ") ", operator])
];
level = [concat(["(", group(indent(level)), ")"])];
}
if (operatorPosition === "start") {
level.push(line, operator, " ");
} else {
level.push(join(" ", [nodes.shift()!, operator]));
level.push(" ", operator, line);
}
levelOperator = nextOperator;
levelPrecedence = nextPrecedence;
} else if (nextPrecedence < levelPrecedence) {
level.push(nodes.shift()!);
if (isRoot) {
const content = group(indent(join(line, level)));
const content = group(indent(level));
nodes.unshift(
levelOperator !== undefined &&
needsParentheses(levelOperator, nextOperator)
Expand All @@ -646,10 +652,10 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc {
levelOperator = undefined;
levelPrecedence = undefined;
} else {
return group(join(line, level));
return group(level);
}
} else {
const content = binary(nodes, tokens);
const content = binary(nodes, tokens, operatorPosition);
nodes.unshift(
levelOperator !== undefined &&
needsParentheses(nextOperator, levelOperator)
Expand All @@ -659,7 +665,7 @@ export function binary(nodes: Doc[], tokens: IToken[], isRoot = false): Doc {
}
}
level.push(nodes.shift()!);
return group(join(line, level));
return group(level);
}

export function getOperators(ctx: BinaryExpressionCtx) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
import path from "path";
import url from "url";
import { testSample } from "../../test-utils.js";
import { testSample, testSampleWithOptions } from "../../test-utils.js";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));

describe("prettier-java", () => {
testSample(__dirname);
testSampleWithOptions({
testFolder: path.resolve(__dirname, "operator-position-start"),
prettierOptions: { experimentalOperatorPosition: "start" }
});
testSampleWithOptions({
testFolder: path.resolve(__dirname, "operator-position-end"),
prettierOptions: { experimentalOperatorPosition: "end" }
});
testSample(path.resolve(__dirname, "operator-position-end"));
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
public class BinaryOperations {

public void binaryOperation() {
int alpha = (left) << right;
boolean beta = (left) < right;
}

@Annotation("This operation with two very long string should break" + "in a very nice way")
public String binaryOperationThatShouldBreak() {
System.out.println("This operation with two very long string should break" + "in a very nice way");
return "This operation with two very long string should break" + "in a very nice way";
}

@Annotation("This operation should" + "not break")
public String binaryOperationThatShouldNotBreak() {
System.out.println("This operation should" + "not break");
return "This operation should" + "not break";
}

public int ternaryOperationThatShouldBreak() {
int shortInteger = thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
return thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne ? thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne : thisIsAShortInteger;
}

public int ternaryOperationThatShouldBreak2() {
int shortInteger = thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
return thisIsAVeryLongInteger ? thisIsAnotherVeryLongOne : thisIsAnotherVeryLongIntegerThatIsEvenLongerThanFirstOne;
}

public int ternaryOperationThatShouldNotBreak() {
int a = b ? b : c;
return b ? b : c;
}

public boolean binaryOperationWithComments() {
boolean a = one || two >> 1 // one
// two
// three
|| // five
// four
three;

boolean b = one || two >> 1 // one
// two
// three
||
three;

boolean c = one || two >> 1 // one
// two
// three
|| three;

return a || b || c;
}

public void method() {
new Foo(stuff, thing, "auaaaaaaaaa some very long stuff", "some more").bar(10);
foo(stuff, thing, "some very longuuuuuuuuuuuuuu stuff", "some more").bar(10);

// Issue 381
new MethodWrappingFollowingContstructor().aLongEnoughMethodNameToForceThingsToWrap();
}

public void binaryExpressionWithCast() {
double availability12 = (double) successfulCount / (successfulCount + failureCount);
availability12 = (double) successfulCount / (successfulCount + failureCount);
}

void declarationVsAssignment() {
var lineLengthInAssignmentMoreThanPrintWidth = "1234567890" + "1234567890" + "1234567890" + "1234567890" + "1234567890" + "1234567890";
lineLengthInAssignmentMoreThanPrintWidth = "1234567890" + "1234567890" + "1234567890" + "1234567890" + "1234567890" + "1234567890";

aaaaaaaaaa += bbbbbbbbbbb + ccccccccccc + ddddddddddd + eeeeeeeeee + ffffffffff + gggggggggg;
aaaaaaaaaa %= bbbbbbbbbbb + ccccccccccc + ddddddddddd + eeeeeeeeee + ffffffffff + gggggggggg;
aaaaaaaaaa <<= bbbbbbbbbbb + ccccccccccc + ddddddddddd + eeeeeeeeee + ffffffffff + gggggggggg;
aaaaaaaaaa &= bbbbbbbbbbb + ccccccccccc + ddddddddddd + eeeeeeeeee + ffffffffff + gggggggggg;

var aaaaaaaaaa = bbbbbbbbbb || cccccccccc ? dddddddddd + eeeeeeeeee : ffffffffff + gggggggggg;
aaaaaaaaaa = bbbbbbbbbb || cccccccccc ? dddddddddd + eeeeeeeeee : ffffffffff + gggggggggg;

var something = MyClass.staticFunction(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd);
something = MyClass.staticFunction(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd);

var something = MyClass.staticFunction(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd) + 0;
something = MyClass.staticFunction(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd) + 0;

var something12 = new MyClass(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd);
something12 = new MyClass(aaaaaaaaaa, bbbbbbbbbbb, ccccccccccc, ddddddddddd);
}

void parentheses() {
var result = a + b >>> 1;
var sizeIndex = index - 1 >>> level & MASK;
var from = offset > left ? 0 : left - offset >> level;
var to = right - offset >> level + 1;
if (rawIndex < 1 << list._level + SHIFT) {}
var res = size < SIZE ? 0 : size - 1 >>> SHIFT << SHIFT;
sign = 1 - 2 * b[3] >> 7;
exponent = b[3] << 1 & 0xff | b[2] >> 7 - 127;
mantissa = b[2] & 0x7f << 16 | b[1] << 8 | b[0];

2 / 3 * 10 / 2 + 2;
2 * 3 * 10 / 2 + 2;
var rotateX = RANGE / rect.height * refY - RANGE / 2 * getXMultiplication(rect.width);
var rotateY = RANGE / rect.width * refX - RANGE / 2 * getYMultiplication(rect.width);

a % 10 - 5;
a - 10 % 5;
a * b % 10;
a % b * 10;
a % 10 > 5;
a % 10 == 0;

1 << 2 >>> 3 >> 4;
1 >>> 2 >> 3 << 4;

1 << 2 + 3;
1 >> 2 - 3;
1 >>> 2 * 3;
1 / 2 << 3;
1 + 2 >> 3;
1 - 2 >>> 3;

x == y == z;
x != y == z;
x == y != z;
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) {}
}
}
Loading

0 comments on commit 2fa40bd

Please sign in to comment.