Skip to content

Commit a80efc2

Browse files
committed
examples
1 parent 02122a6 commit a80efc2

7 files changed

Lines changed: 74 additions & 62 deletions

File tree

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

Lines changed: 1 addition & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public final class Level extends Doc {
5454
/**
5555
* The depth of nested levels in the current level tree from which we explore both breaking vs not breaking the current level.
5656
*/
57-
private static final int LAST_LEVELS_TO_EXPLORE = 12;
57+
private static final int LAST_LEVELS_TO_EXPLORE = 8;
5858

5959
private static final Collector<Level, ?, Optional<Level>> GET_LAST_COLLECTOR = Collectors.reducing((u, v) -> v);
6060

@@ -518,59 +518,6 @@ private static Optional<State> tryBreakInnerLevel_checkInner(
518518
exp -> innerLevel.tryInlineSuffix(
519519
commentsHelper, maxWidth, state, exp, isSimpleInlining))
520520
.map(Exploration::markAccepted))
521-
.breakOnlyIfInnerLevelsThenFitOnOneLine(keepIndentWhenInlined -> {
522-
// This case currently only matches lambda _expressions_ (without curlies)
523-
State state1 =
524-
keepIndentWhenInlined ? state.withIndentIncrementedBy(innerLevel.getPlusIndent()) : state;
525-
526-
String humanDescription = "end tryBreakLastLevel chain -> breakOnlyIfInnerLevelsThenFitOnOneLine";
527-
LevelNode levelNode = explorationNode.newChildNode(innerLevel, state1);
528-
return levelNode
529-
.maybeExplore(humanDescription, state1, exp -> {
530-
// Not all levels would look good if inlined in this position, so we accept
531-
// levels that are meant to look good even if partially inlined, e.g. method
532-
// chains, which will catch things like builders, but not other kinds of levels like
533-
// constant expressions.
534-
// See the palantir-expression-lambdas.input test for an example of what this is
535-
// trying to avoid.
536-
537-
// For this, need to actually check the last inner level of `lastLevel` (2 levels down).
538-
if (innerLevel.docs.isEmpty() || !(getLast(innerLevel.docs) instanceof Level)) {
539-
return Optional.empty();
540-
}
541-
Level lastLevel2 = ((Level) getLast(innerLevel.docs));
542-
switch (lastLevel2.getBreakabilityIfLastLevel()) {
543-
case ABORT:
544-
case CHECK_INNER:
545-
return Optional.empty();
546-
case ACCEPT_INLINE_CHAIN:
547-
Exploration broken =
548-
innerLevel.breakNormally(state, levelNode, commentsHelper, maxWidth);
549-
return innerLevel.handle_breakOnlyIfInnerLevelsThenFitOnOneLine(
550-
commentsHelper,
551-
maxWidth,
552-
state1,
553-
broken.state(),
554-
keepIndentWhenInlined,
555-
explorationNode);
556-
case ACCEPT_INLINE_CHAIN_IF_SIMPLE_OTHERWISE_CHECK_INNER:
557-
// We will allow inlining this kind of level but only if the level itself is
558-
// not simple.
559-
if (lastLevel2.openOp.complexity() == Complexity.SIMPLE) {
560-
return Optional.empty();
561-
}
562-
return innerLevel.tryInlinePrefixOntoCurrentLine(
563-
commentsHelper,
564-
maxWidth,
565-
state1,
566-
keepIndentWhenInlined,
567-
explorationNode);
568-
default:
569-
throw new RuntimeException("Unknown breakabilityIfLastLevel: " + lastLevel2);
570-
}
571-
})
572-
.map(Exploration::markAccepted);
573-
})
574521
// We don't know how to fit the inner level on the same line, so bail out.
575522
.otherwise_(Optional.empty());
576523
}

palantir-java-format/src/main/java/com/palantir/javaformat/java/JavaInputAstVisitor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ protected void visitLambdaExpression(LambdaExpressionTree node, boolean statemen
12551255
.breakBehaviour(
12561256
statementBody
12571257
? BreakBehaviours.preferBreakingLastInnerLevel(true)
1258-
: BreakBehaviours.breakOnlyIfInnerLevelsThenFitOnOneLine(false))
1258+
: BreakBehaviours.preferBreakingLastInnerLevel(false))
12591259
.breakabilityIfLastLevel(
12601260
statementBody
12611261
? LastLevelBreakability.ACCEPT_INLINE_CHAIN

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,11 @@ class B20128760 {
7070
Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx {}
7171

7272
{
73-
Stream<ItemKey> itemIdsStream = stream(members).flatMap(m -> m.getFieldValues().entrySet().stream()
74-
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
75-
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue()).stream()
76-
.map(id -> new ItemKey(fieldsById.get(fv.getKey()).getItemTypeId(), id))));
73+
Stream<ItemKey> itemIdsStream = stream(members)
74+
.flatMap(m -> m.getFieldValues().entrySet().stream()
75+
.filter(fv -> itemLinkFieldIds.contains(fv.getKey()))
76+
.flatMap(fv -> FieldDTO.deserializeStringToListOfStrings(fv.getValue()).stream()
77+
.map(id ->
78+
new ItemKey(fieldsById.get(fv.getKey()).getItemTypeId(), id))));
7779
}
7880
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
public class Foo {
2+
3+
public static void main(String[] args) {
4+
assertThat(resultThatIsQuiteLong)
5+
.containsExactly(SomeResonse.getValue("1".repeat((int) (MyClass.CHAR_LIMIT * 0.5))));
6+
someArgResulsts.forEach((key, value) -> value.sync().ifPresent(myvalue -> {
7+
myvalue.put(foo, bar, baz);
8+
}));
9+
EntryStream.of(SOME_NAME_TO_JSON_STRING).forKeyValue((tableName, jsonString) -> doReturn(
10+
getSelectJsonResponse(some(other(myValueTahatIsLong(jsonString)))))
11+
.when(session)
12+
.execute((Statement) argThat(statement ->
13+
statement instanceof Statement && statement.toString().contains(tableName))));
14+
return someService.runWithLock(
15+
foo,
16+
Set.of("Running bar"),
17+
() -> getOperationLock(MyClass.DOWNLOAD).runWithLock(MyClass.DOWNLOAD, () -> {
18+
return doStuff.builder()
19+
.method1()
20+
.method2()
21+
.build();
22+
}));
23+
}
24+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class Foo {
2+
3+
public static void main(String[] args) {
4+
assertThat(resultThatIsQuiteLong)
5+
.containsExactly(SomeResonse.getValue("1".repeat((int) (MyClass.CHAR_LIMIT * 0.5))));
6+
someArgResulsts.forEach((key, value) -> value.sync().ifPresent(myvalue -> {
7+
myvalue.put(foo, bar, baz);
8+
}));
9+
EntryStream.of(SOME_NAME_TO_JSON_STRING)
10+
.forKeyValue((tableName, jsonString) -> doReturn(
11+
getSelectJsonResponse(some(other(myValueTahatIsLong(jsonString)))))
12+
.when(session)
13+
.execute((Statement) argThat(statement -> statement instanceof Statement
14+
&& statement.toString().contains(tableName))));
15+
return someService.runWithLock(
16+
foo,
17+
Set.of("Running bar"),
18+
() -> getOperationLock(MyClass.DOWNLOAD).runWithLock(MyClass.DOWNLOAD, () -> {
19+
return doStuff.builder().method1().method2().build();
20+
}));
21+
}
22+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,11 @@ public class Foo {
55
.baz(argargargargargargargargargargargargargargargargargargargargarg, param -> doStuff()
66
.method1()
77
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh));
8+
9+
thisIsMyObject
10+
.callingMethod1()
11+
.callingMethod2(argargargargargargargargargargargargargargargargargargargargarg, param -> doStuff()
12+
.method1()
13+
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh));
814
}
915
}
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
public class Foo {
22
public static void main(String[] args) {
3-
foo.bar().baz(argargargargargargargargargargargargargargargargargargargargarg, param -> doStuff()
4-
.method1()
5-
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh));
3+
foo.bar()
4+
.baz(
5+
argargargargargargargargargargargargargargargargargargargargarg,
6+
param -> doStuff()
7+
.method1()
8+
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh));
9+
10+
thisIsMyObject
11+
.callingMethod1()
12+
.callingMethod2(
13+
argargargargargargargargargargargargargargargargargargargargarg,
14+
param -> doStuff()
15+
.method1()
16+
.method2(arljhfaldjfhalfjhalfjahflahflahfaljfhadlfjhljsahljhfsaljfhlfajhfljh));
617
}
718
}

0 commit comments

Comments
 (0)