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 @@ -148,9 +148,9 @@
int column = state.column();
int columnBeforeLastBreak = 0; // Not activated by default
for (Doc doc : docs) {
if (doc instanceof Break && ((Break) doc).hasColumnLimit()) {

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 27)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 25)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 26)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / Analyze

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 151 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
columnBeforeLastBreak = column;
} else if (doc instanceof Level) {

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 27)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 25)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 26)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / Analyze

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 153 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
// Levels might have nested levels that have a 'columnLimitBeforeLastBreak' set, so recurse.
State newState = state.withColumn(column);
Level innerLevel = (Level) doc;
Expand Down Expand Up @@ -339,9 +339,19 @@

// Add the width of tokens, breaks before the lastLevel. We must always have space for
// these.
List<Doc> leadingDocs = docs.subList(0, docs.indexOf(lastLevel));
int lastLevelIndex = docs.indexOf(lastLevel);
List<Doc> leadingDocs = docs.subList(0, lastLevelIndex);
float leadingWidth = getWidth(leadingDocs);

// A forced break after the lastLevel, such as the ones around a // comment that sits before this level's
// closing token, cannot be laid out flat by tryToLayOutLevelOnOneLine: the comment would swallow every token
// after it on the line. Such a level breaks normally instead, as tryBreakInnerLevel refuses it for the same
// reason. (A forced break before the lastLevel makes leadingWidth infinite and fails the check below.)
List<Doc> trailingDocs = docs.subList(lastLevelIndex + 1, docs.size());
if (Float.isInfinite(getWidth(trailingDocs))) {
return Optional.empty();
}

// Potentially add the width of prefixes we want to consider as part of the width that
// must fit on the same line, so that we don't accidentally break prefixes when we could
// have avoided doing so.
Expand Down Expand Up @@ -560,7 +570,7 @@
return Optional.empty();
}
Level lastLevel2 = ((Level) getLast(innerLevel.docs));
switch (lastLevel2.getBreakabilityIfLastLevel()) {

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 27)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 25)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 26)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / Analyze

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-aarch64)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-x86-64)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-aarch64)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch

Check warning on line 573 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-x86-64)

[StatementSwitchToExpressionSwitch] This statement switch can be converted to a new-style arrow switch
case ABORT:
case CHECK_INNER:
return Optional.empty();
Expand Down Expand Up @@ -592,6 +602,9 @@
* Mark breaks in this level as not broken, but lay out the inner levels normally, according to their own
* {@link BreakBehaviour}. The resulting {@link State#mustBreak} will be true if this level did not fit on exactly
* one line.
*
* <p>The callers make sure that none of this level's own breaks is forced: a forced break laid out flat would put
* the tokens after a {@code //} comment inside the comment.
*/
private State tryToLayOutLevelOnOneLine(
CommentsHelper commentsHelper,
Expand Down Expand Up @@ -620,7 +633,7 @@
ImmutableSplitsBreaks.Builder builder = ImmutableSplitsBreaks.builder();
ImmutableList.Builder<Doc> currentSplit = ImmutableList.builder();
for (Doc doc : docs) {
if (doc instanceof Break) {

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 27)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 25)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 26)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / Analyze

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 636 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
builder.addSplits(currentSplit.build());
currentSplit = ImmutableList.builder();
builder.addBreaks((Break) doc);
Expand Down Expand Up @@ -777,7 +790,7 @@
private int computeMaxDepth(Iterable<Doc> docs) {
int maxChildDepth = 0;
for (Doc doc : docs) {
if (doc instanceof Level) {

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 27)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 25)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / build (JDK 26)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / Analyze

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (linux-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-aarch64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.

Check warning on line 793 in open-java-format/src/main/java/com/palantir/javaformat/doc/Level.java

View workflow job for this annotation

GitHub Actions / native (macos-x86-64)

[PatternMatchingInstanceof] This code can be simplified to use a pattern-matching instanceof.
Level childLevel = (Level) doc;
maxChildDepth = Math.max(maxChildDepth, childLevel.getMaxDepth());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import java.util.stream.IntStream;

class Repro {
int find(Item[] items) {
return IntStream.range(0, items.length)
.filter(i -> (items[i].getName().equals("alpha") || items[i].getName().equals("beta")
|| items[i].getName().equals("gamma")
// || (items[i].getName().equals("delta") && items.length > i
// && items[i + 1].getName().equals("epsilon"))
)).findFirst().orElse(-1);
}

void plainCall() {
check(i -> (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(i) || bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(i)
// || c(i)
));
}

void blockComment() {
check(i -> (aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(i) || bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(i)
/* block */
));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import java.util.stream.IntStream;

class Repro {
int find(Item[] items) {
return IntStream.range(0, items.length)
.filter(i ->
(items[i].getName().equals("alpha")
|| items[i].getName().equals("beta")
|| items[i].getName().equals("gamma")
// || (items[i].getName().equals("delta") && items.length > i
// && items[i + 1].getName().equals("epsilon"))
))
.findFirst()
.orElse(-1);
}

void plainCall() {
check(i ->
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(i)
|| bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(i)
// || c(i)
));
}

void blockComment() {
check(i ->
(aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa(i)
|| bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(i)
/* block */
));
}
}
Loading