Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@
import com.google.common.collect.PeekingIterator;
import com.google.common.collect.Range;
import com.google.common.collect.RangeSet;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.common.collect.TreeRangeSet;
import com.google.errorprone.annotations.CheckReturnValue;
Expand Down Expand Up @@ -523,7 +522,7 @@
if (afterFirstToken) {
builder.forcedBreak();
}
builder.open(row.iterator().next().getKind() == NEW_ARRAY || cols == 1 ? ZERO : plusFour);

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 525 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
boolean firstInRow = true;
for (ExpressionTree item : row) {
if (!firstInRow) {
Expand Down Expand Up @@ -698,6 +697,11 @@

@Override
public Void visitNewClass(NewClassTree node, Void unused) {
visitNewClass(node, ZERO);
return null;
}

private void visitNewClass(NewClassTree node, Indent huggedPlusIndent) {
sync(node);
builder.open(ZERO);
if (node.getEnclosingExpression() != null) {
Expand All @@ -713,16 +717,18 @@
visitModifiers(
node.getClassBody().getModifiers(), Direction.HORIZONTAL, Optional.empty());
visitAnnotations(annotations, BreakOrNot.NO, BreakOrNot.YES);
doNotHug(node);
}
scan(node.getIdentifier(), null);
addArguments(node.getArguments(), plusFour);
addArguments(
node.getArguments(),
plusFour,
huggedPlusIndent,
/* allowBlockLambdaHug= */ node.getClassBody() == null);
builder.close();
if (node.getClassBody() != null) {
addBodyDeclarations(
node.getClassBody().getMembers(), BracesOrNot.YES, FirstDeclarationsOrNot.YES);
}
return null;
}

@Override
Expand Down Expand Up @@ -765,7 +771,7 @@
CollapseEmptyOrNot.YES,
AllowLeadingBlankLine.YES,
AllowTrailingBlankLine.YES);
if (node.getStatement().getKind() == BLOCK) {

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 774 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
builder.space();
} else {
builder.breakOp(" ");
Expand Down Expand Up @@ -826,10 +832,11 @@
builder.guessToken("(");
builder.guessToken(")");
} else {
if (init.getClassBody() != null) {
doNotHug(init);
}
addArguments(init.getArguments(), plusFour);
addArguments(
init.getArguments(),
plusFour,
ZERO,
/* allowBlockLambdaHug= */ init.getClassBody() == null);
}
if (init.getClassBody() != null) {
addBodyDeclarations(
Expand Down Expand Up @@ -1090,11 +1097,11 @@
builder.fenceComments();
builder.open(
node.getInitializer().size() > 1
&& node.getInitializer().get(0).getKind() == Tree.Kind.EXPRESSION_STATEMENT

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1100 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
? plusFour
: ZERO);
if (!node.getInitializer().isEmpty()) {
if (node.getInitializer().get(0).getKind() == VARIABLE) {

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1104 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
PeekingIterator<StatementTree> it =
Iterators.peekingIterator(node.getInitializer().iterator());
visitVariables(
Expand Down Expand Up @@ -1159,7 +1166,7 @@
while (true) {
expressions.add(node.getCondition());
statements.add(node.getThenStatement());
if (node.getElseStatement() != null && node.getElseStatement().getKind() == IF) {

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1169 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
node = (IfTree) node.getElseStatement();
} else {
break;
Expand Down Expand Up @@ -1195,7 +1202,7 @@
CollapseEmptyOrNot.valueOf(onlyClause),
AllowLeadingBlankLine.YES,
AllowTrailingBlankLine.valueOf(trailingClauses));
followingBlock = statements.get(i).getKind() == BLOCK;

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1205 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
afterFirstToken = true;
}
if (node.getElseStatement() != null) {
Expand Down Expand Up @@ -1450,7 +1457,7 @@
}

private void visitAnnotationArgument(AssignmentTree node) {
boolean isArrayInitializer = node.getExpression().getKind() == NEW_ARRAY;

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 21 on ubuntu-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs

Check warning on line 1460 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / Publish snapshot

[PreferInstanceofOverGetKind] Prefer instanceof over getKind() checks where possible, as these work well with pattern matching instanceofs
sync(node);
builder.open(isArrayInitializer ? ZERO : plusFour);
scan(node.getVariable(), null);
Expand Down Expand Up @@ -3035,7 +3042,7 @@
builder.close();
}

/** Collapse chains of {@code .} operators, across multiple {@link ASTNode} types. */

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on ubuntu-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

Check warning on line 3045 in core/src/main/java/com/google/googlejavaformat/java/JavaInputAstVisitor.java

View workflow job for this annotation

GitHub Actions / JDK 25 on macos-latest

[NotJavadoc] This element has both markdown `///` and traditional `/**` javadoc comments; only the last one will be used.

/// Output a "." node.
///
Expand Down Expand Up @@ -3083,10 +3090,15 @@
token(".");
} else {
builder.open(plusFour);
if (!stack.isEmpty()) {
doNotHug(node);
ExpressionTree base = getArrayBase(node);
if (!stack.isEmpty() && base instanceof NewClassTree newClass) {
// Constructor calls starting a chain with trailing dereferences (e.g.
// `new Thread(() -> { ... }).start()`) stay on the first line, so indent a hugged
// lambda argument by an extra +4 to distinguish it from the trailing dereferences.
visitNewClass(newClass, plusFour);
} else {
scan(base, null);
}
scan(getArrayBase(node), null);
builder.breakOp();
needDot = true;
}
Expand Down Expand Up @@ -3187,18 +3199,14 @@
token(".");
length++;
}
if (!hasBroken && i < items.size() - 1) {
doNotHug(e);
}
if (!fillFirstArgument(e, items, trailingDereferences ? ZERO : minusFour)) {
BreakTag tyargTag = genSym();
dotExpressionUpToArgs(e, Optional.of(tyargTag));
Indent tyargIndent = Indent.If.make(tyargTag, plusFour, ZERO);
// Calls that stay on the first line of the chain indent hugged arguments by an extra +4,
// to distinguish them from the trailing dereferences.
dotExpressionArgsAndParen(
e,
tyargIndent,
tyargTag,
(trailingDereferences || needDot) ? plusFour : ZERO,
(!hasBroken && i < items.size() - 1) ? plusFour : ZERO);
}
Expand Down Expand Up @@ -3232,8 +3240,8 @@
|| name.length() > 4
|| !methodInvocation.getTypeArguments().isEmpty()
|| methodInvocation.getArguments().size() != 1
// Huggable arguments are handled by addArguments, see canHugOnlyArgument.
|| isHuggable(getOnlyElement(methodInvocation.getArguments()))) {
// Huggable arguments are handled by addArguments.
|| canHugOnlyArgument(methodInvocation.getArguments(), /* allowBlockLambdaHug= */ true)) {
return false;
}
builder.open(ZERO);
Expand Down Expand Up @@ -3270,15 +3278,10 @@
}

Deque<Integer> unconsumedPrefixes = new ArrayDeque<>(ImmutableSortedSet.copyOf(prefixes));
boolean needDot0 = needDot;
BreakTag nameTag = genSym();
Indent beforeFirstBreak = needDot ? ZERO : plusFour;
for (int i = 0; i < items.size(); i++) {
ExpressionTree e = items.get(i);
// Don't hug a block lambda if its call stays on the first line of the chain and has trailing
// dereferences.
if (!needDot0 && (i == 0 || prefixes.contains(i)) && i < items.size() - 1) {
doNotHug(e);
}
Indent argsIndent = trailingDereferences ? plusFour : ZERO;
if (needDot) {
FillMode fillMode;
if (!unconsumedPrefixes.isEmpty() && i <= unconsumedPrefixes.peekFirst()) {
Expand All @@ -3287,8 +3290,16 @@
fillMode = FillMode.UNIFIED;
}

BreakTag nameTag = genSym();
builder.breakOp(fillMode, "", ZERO, Optional.of(nameTag));
token(".");
if (!trailingDereferences) {
argsIndent = Indent.If.make(nameTag, plusFour, ZERO);
}
if (beforeFirstBreak != ZERO) {
beforeFirstBreak =
unconsumedPrefixes.isEmpty() ? ZERO : Indent.If.make(nameTag, ZERO, beforeFirstBreak);
}
}
BreakTag tyargTag = genSym();
dotExpressionUpToArgs(e, Optional.of(tyargTag));
Expand All @@ -3297,11 +3308,8 @@
unconsumedPrefixes.removeFirst();
}

Indent tyargIndent = Indent.If.make(tyargTag, plusFour, ZERO);
Indent argsIndent = Indent.If.make(nameTag, plusFour, trailingDereferences ? plusFour : ZERO);
Indent huggedArgsIndent =
Indent.If.make(nameTag, ZERO, trailingDereferences ? plusFour : ZERO);
dotExpressionArgsAndParen(e, tyargIndent, argsIndent, huggedArgsIndent);
dotExpressionArgsAndParen(
e, tyargTag, argsIndent, i < items.size() - 1 ? beforeFirstBreak : ZERO);

needDot = true;
}
Expand Down Expand Up @@ -3374,14 +3382,21 @@
}

private void dotExpressionArgsAndParen(
ExpressionTree expression, Indent tyargIndent, Indent indent, Indent huggedIndent) {
ExpressionTree expression, BreakTag tyargTag, Indent indent, Indent huggedIndent) {
Deque<ExpressionTree> indices = getArrayIndices(expression);
expression = getArrayBase(expression);
switch (expression.getKind()) {
case METHOD_INVOCATION -> {
Indent tyargIndent = Indent.If.make(tyargTag, plusFour, ZERO);
Indent huggedArgsIndent =
huggedIndent == ZERO ? ZERO : Indent.If.make(tyargTag, ZERO, huggedIndent);
builder.open(tyargIndent);
MethodInvocationTree methodInvocation = (MethodInvocationTree) expression;
addArguments(methodInvocation.getArguments(), indent, huggedIndent);
addArguments(
methodInvocation.getArguments(),
indent,
huggedArgsIndent,
/* allowBlockLambdaHug= */ true);
builder.close();
}
default -> {}
Expand Down Expand Up @@ -3436,28 +3451,26 @@
token(">");
}

private void addArguments(List<? extends ExpressionTree> arguments, Indent plusIndent) {
addArguments(arguments, plusIndent, ZERO);
}

/// Add arguments to a method invocation, etc. The arguments indented `plusFour`, filled, from the
/// current indent. The arguments may be output two at a time if they seem to be arguments to a
/// map constructor, etc.
///
/// @param arguments the arguments
/// @param plusIndent the extra indent for the arguments
/// @param huggedPlusIndent the extra indent when a single expression lambda argument is hugged
/// @param huggedPlusIndent the extra indent when a single lambda argument is hugged
/// @param allowBlockLambdaHug whether a single block lambda argument may be hugged (false when a
/// class body immediately follows the argument list)
private void addArguments(
List<? extends ExpressionTree> arguments, Indent plusIndent, Indent huggedPlusIndent) {
List<? extends ExpressionTree> arguments,
Indent plusIndent,
Indent huggedPlusIndent,
boolean allowBlockLambdaHug) {
boolean tabular = arguments.size() % 2 == 0 && argumentsAreTabular(arguments) == 2;
boolean hug = canHugOnlyArgument(arguments);
boolean hug = canHugOnlyArgument(arguments, allowBlockLambdaHug);
if (hug) {
ExpressionTree argument = getOnlyElement(arguments);
builder.openHuggable(
plusIndent,
hasHuggedBlockLambda(arguments) ? ZERO : huggedPlusIndent,
hugPoint(argument),
/* fallbackIfFlatOnNextLine= */ true);
plusIndent, huggedPlusIndent, hugPoint(argument), /* fallbackIfFlatOnNextLine= */ true);
} else {
builder.open(plusIndent);
}
Expand Down Expand Up @@ -3521,57 +3534,17 @@
}
}

/**
* Returns true for constructs that may be hugged, i.e. laid out starting on the line of the
* preceding content: lambdas.
*/
private static boolean isHuggable(ExpressionTree tree) {
return tree instanceof LambdaExpressionTree;
}

/**
* Returns true if the argument list is a single argument that may be hugged, e.g. {@code f(x -> {
* ... })}. Lambdas preceded by other arguments aren't hugged: the preceding arguments would share
* a line with the lambda's head, and wouldn't line up with the lambda's body.
*/
private boolean canHugOnlyArgument(List<? extends ExpressionTree> arguments) {
return arguments.size() == 1
&& isHuggable(getOnlyElement(arguments))
&& !notHuggable.contains(getOnlyElement(arguments));
}

/**
* Constructs that may not be hugged even though {@link #isHuggable} is true for them; see {@link
* #doNotHug}.
*/
private final Set<Tree> notHuggable = Sets.newIdentityHashSet();

/**
* Prevents hugging a block lambda argument of {@code call}. Used in two cases:
*
* <ul>
* <li>a call on the first line of a dereference chain with more dereferences after it (e.g.
* {@code new Thread(() -> { ... }).start()})
* <li>a constructor or enum constant call with a class body (e.g. {@code new Thread(() -> { ...
* }) { ... }})
* </ul>
*/
private void doNotHug(ExpressionTree call) {
List<? extends ExpressionTree> arguments =
switch (getArrayBase(call)) {
case MethodInvocationTree invocation -> invocation.getArguments();
case NewClassTree newClass -> newClass.getArguments();
default -> ImmutableList.of();
};
if (hasHuggedBlockLambda(arguments)) {
notHuggable.add(getOnlyElement(arguments));
}
}

private static boolean hasHuggedBlockLambda(List<? extends ExpressionTree> arguments) {
return arguments.size() == 1
&& getOnlyElement(arguments) instanceof LambdaExpressionTree lambda
&& lambda.getBodyKind() == LambdaExpressionTree.BodyKind.STATEMENT;
/// Returns true if the argument list is a single argument that may be hugged, e.g. `f(x -> { ...
/// })`. Lambdas preceded by other arguments aren't hugged: the preceding arguments would share a
/// line with the lambda's head, and wouldn't line up with the lambda's body.
private static boolean canHugOnlyArgument(
List<? extends ExpressionTree> arguments, boolean allowBlockLambdaHug) {
if (arguments.size() != 1) {
return false;
}
ExpressionTree argument = getOnlyElement(arguments);
return argument instanceof LambdaExpressionTree lambda
&& (allowBlockLambdaHug || lambda.getBodyKind() != LambdaExpressionTree.BodyKind.STATEMENT);
}

private void argList(List<? extends ExpressionTree> arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ class LambdaHug {
item.of(() -> Streams.concat(Stream.of("a"), Stream.of("b"), Stream.empty(), Stream.of("c", "d")).spliterator()).expect("a", "b", "c", "d");
task(event -> { handle(event); handleAgain(event); handleOnceMore(event); }).handleFailure(failure);
task(() -> Streams.concat(Stream.of("a"), Stream.of("b"), Stream.empty(), Stream.of("c", "d")).spliterator()).expect("a");
SomeVeryLongClassNameThatIsVeryLongAndForcesABreakBeforeMethod.someVeryLongStaticMethodNameThatWraps(x -> { handle(x); handleAgain(x); }).thenAccept(y -> { handle(y); handleAgain(y); });
com.google.very.lengthy.pkg.name.that.is.extremely.wide.and.forces.a.wrap.somewhere.before.MyClass.computeItems(x -> { handle(x); handleAgain(x); }).thenAccept(y -> { handle(y); handleAgain(y); });
new Receiver() { void f() {} }.computeItems(x -> { handle(x); handleAgain(x); }).stream().toList();
new Foo(() -> Streams.concat(Stream.of("a"), Stream.of("b"), Stream.empty(), Stream.of("c", "d")).spliterator()).expect("a");
MyClass.computeItems(x -> { handle(x); handleAgain(x); }).stream();
MyClass.computeItems(x -> Streams.concat(Stream.of("a"), Stream.of("b"), Stream.empty(), Stream.of("c", "d")).spliterator()).stream();
item.<VeryLongTypeArgumentOne, VeryLongTypeArgumentTwo, VeryLongTypeArgumentThree, VeryLongTypeArgumentFour>of(x -> { handle(x); handleAgain(x); }).register();
doAnswer(invocation -> { handle(invocation); return null; }).when(mock).run();
return trapExceptions(() -> { compute(); computeAgain(); }).then(x -> { handle(x); handleAgain(x); }).peek(cleanup);
}

void conditions() {
Expand All @@ -69,6 +78,8 @@ class LambdaHug {
do { process(); } while (anyMatch(c -> { return true; }));
if (anyMatch(c -> check(x -> { handle(x); handleAgain(x); }))) { return; }
if (anyMatch(param -> param.someLongMethodName(argumentNumberOne, argumentNumberTwo, argumentNumberThree))) { return; }
if (trapExceptions(() -> { compute(); return true; }).isOk()) { process(); }
if (other && trapExceptions(() -> { compute(); return true; }).isOk()) { process(); }
}

void nested() {
Expand Down
Loading
Loading