Skip to content

Commit f7453db

Browse files
authored
Fix crash when formatting method references (#1630)
Fix crash when formatting method references
1 parent 976e9ff commit f7453db

3 files changed

Lines changed: 31 additions & 9 deletions

File tree

palantir-java-format/src/main/java/com/palantir/javaformat/doc/CountWidthUntilBreakVisitor.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,11 @@ public Float visitLevel(Level level) {
6767
if (found.isPresent()) {
6868
return visit(level.getDocs().get(found.getAsInt()));
6969
}
70-
// Otherwise, assert that we encountered a break and move on.
71-
if (StartsWithBreakVisitor.INSTANCE.visit(level) != Result.YES) {
72-
// Avoid computing expensive level.representation() if we aren't throwing it in an exception.
73-
throw new IllegalStateException(String.format(
74-
"Didn't find expected break at the beginning of level.\n%s",
75-
level.representation(State.startingState())));
76-
}
77-
78-
return 0f;
70+
// Level starts with tokens directly (e.g. method reference qualifier) — sum doc widths up to the first break.
71+
return level.getDocs().stream()
72+
.takeWhile(doc -> !(doc instanceof Break))
73+
.map(Doc::getWidth)
74+
.reduce(0f, Float::sum);
7975
}
8076

8177
/**

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/A.input

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,18 @@ class A {
8080
2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
8181
+ 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
8282
+ 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
83+
return StreamEx.of(itemsToCreate)
84+
.append(itemsToEdit)
85+
.append(itemsToDelete)
86+
.distinct()
87+
.mapToEntry(item -> getArgsGenerator()
88+
.getNewArgs(
89+
extendedItems.getOrDefault(item, Set.of()),
90+
getOwningResource(
91+
item,
92+
getModificationContext()::getRestrictionStatusOrDefault,
93+
RestrictionStatus::getPackageId,
94+
getModificationContext())))
95+
.toImmutableMap();
8396
}
8497
}

palantir-java-format/src/test/resources/com/palantir/javaformat/java/testdata/A.output

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,18 @@ class A {
7070
something = 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
7171
+ 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2
7272
+ 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2 + 2;
73+
return StreamEx.of(itemsToCreate)
74+
.append(itemsToEdit)
75+
.append(itemsToDelete)
76+
.distinct()
77+
.mapToEntry(item -> getArgsGenerator()
78+
.getNewArgs(
79+
extendedItems.getOrDefault(item, Set.of()),
80+
getOwningResource(
81+
item,
82+
getModificationContext()::getRestrictionStatusOrDefault,
83+
RestrictionStatus::getPackageId,
84+
getModificationContext())))
85+
.toImmutableMap();
7386
}
7487
}

0 commit comments

Comments
 (0)