Skip to content

Commit

Permalink
Fix column limit logic to work as expected with initializers (#138)
Browse files Browse the repository at this point in the history
Method chains in initializers may once again be laid onto a single next line, if they are short enough. This fixes a regression from #123 (0.3.11) where the column limit for method chains would prevent us from attempting to put the initializer on the 2nd line first and see if that works.
  • Loading branch information
dansanduleac authored and bulldozer-bot[bot] committed Jan 21, 2020
1 parent d9a0043 commit 15b46e4
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 28 deletions.
8 changes: 8 additions & 0 deletions changelog/@unreleased/pr-138.v2.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: fix
fix:
description: 'Method chains in initializers may once again be laid onto a single
next line, if they are short enough. This fixes a regression from #123 (0.3.11)
where the column limit for method chains would prevent us from attempting to put
the initializer on the 2nd line first and see if that works.'
links:
- https://github.com/palantir/palantir-java-format/pull/138
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private State computeBroken(
}

/** Lay out a Break-separated group of Docs in the current Level. */
private static State computeBreakAndSplit(
private State computeBreakAndSplit(
CommentsHelper commentsHelper,
int maxWidth,
State state,
Expand All @@ -467,9 +467,12 @@ private static State computeBreakAndSplit(
Obs.ExplorationNode explorationNode) {
float breakWidth = optBreakDoc.isPresent() ? optBreakDoc.get().getWidth() : 0.0F;
float splitWidth = getWidth(split);

boolean shouldBreak = (optBreakDoc.isPresent() && optBreakDoc.get().fillMode() == FillMode.UNIFIED)
|| state.mustBreak()
|| state.column() + breakWidth + splitWidth > maxWidth;
|| Double.isInfinite(breakWidth)
|| !tryToFitOnOneLine(maxWidth, state.withColumn(state.column() + (int) breakWidth), split)
.isPresent();

if (optBreakDoc.isPresent()) {
state = optBreakDoc.get().computeBreaks(state, shouldBreak);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,16 @@ class B20701054 {
ImmutableList<String> x = ImmutableList.INSTANCE.add(1).build();
ImmutableList<String> x = ImmutableList.INSTANCE.add(1).add(2).build();
ImmutableList<String> x = ImmutableList.INSTANCE.add(1).add(2).add(3).build();
ImmutableList<String> x = ImmutableList.INSTANCE
.add(1)
.add(2)
.add(3)
.add(4)
.build();
ImmutableList<String> x =
ImmutableList.INSTANCE.add(1).add(2).add(3).add(4).build();

ImmutableList<String> x = ImmutableList.builder().add(1).build();
ImmutableList<String> x = ImmutableList.builder().add(1).add(2).build();
ImmutableList<String> x = ImmutableList.builder().add(1).add(2).add(3).build();
ImmutableList<String> x = ImmutableList.builder()
.add(1)
.add(2)
.add(3)
.add(4)
.build();
ImmutableList<String> x = ImmutableList.builder()
.add(1)
.add(2)
.add(3)
.add(4)
.add(5)
.build();
ImmutableList<String> x =
ImmutableList.builder().add(1).add(2).add(3).add(4).build();
ImmutableList<String> x =
ImmutableList.builder().add(1).add(2).add(3).add(4).add(5).build();
ImmutableList<String> x = ImmutableList.builder()
.add(1)
.add(2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ class B21465217 {
BufferedOutputStream bout = new BufferedOutputStream(out2);
OutputStreamWriter writer = new OutputStreamWriter(bout, UTF_8___________________________)) {}

try (Writer sourceWriter = env.getFiler()
.createSourceFile(qualifiedNamezzzzzzzz)
.openWriter()) {
try (Writer sourceWriter =
env.getFiler().createSourceFile(qualifiedNamezzzzzzzz).openWriter()) {
sourceWriter.append(fileContents);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class B22610221 {
{
for (Map.Entry<Key<Object>, Object> entry : FOO.bar()
.bazs()
.<Object>thing(someThing)
.entrySet()) {
for (Map.Entry<Key<Object>, Object> entry :
FOO.bar().bazs().<Object>thing(someThing).entrySet()) {
output.put(entry.getKey(), entry.getValue());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PalantirChainNextLine {
void foo() {
Iterator<Map.Entry<ThingId, DelegateOtherThing>> delegateIterator = delegates
.entrySet()
.iterator();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class PalantirChainNextLine {
void foo() {
Iterator<Map.Entry<ThingId, DelegateOtherThing>> delegateIterator =
delegates.entrySet().iterator();
}
}

0 comments on commit 15b46e4

Please sign in to comment.