Skip to content

Commit afbf15c

Browse files
committed
Fixes
1 parent de72d31 commit afbf15c

File tree

4 files changed

+21
-9
lines changed

4 files changed

+21
-9
lines changed

pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,9 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
596596
twoBy2: Boolean = false,
597597
): FormatNode {
598598
val children = node.children
599-
val sep = if (node.isMultiline()) forceSpaceyLine() else spaceOrLine()
599+
val sep: (Node, Node) -> FormatNode = { prev, next ->
600+
if (prev.isTerminal(",") && prev.linesBetween(next) > 0) forceSpaceyLine() else spaceOrLine()
601+
}
600602
return if (twoBy2) {
601603
val pairs = pairArguments(children)
602604
val nodes =
@@ -609,17 +611,17 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
609611
}
610612
Indent(nodes)
611613
} else if (hasTrailingLambda) {
612-
// if the args have a trailing expression (lambda, new, amends) group them differently
614+
// if the args have a trailing lambda, group them differently
613615
val splitIndex = children.indexOfLast { it.type in SAME_LINE_EXPRS }
614616
val normalParams = children.subList(0, splitIndex)
615617
val lastParam = children.subList(splitIndex, children.size)
616618
val trailingNode = if (endsWithClosingBracket(children[splitIndex])) Empty else line()
617-
val lastNodes = formatGeneric(lastParam, sep)
619+
val lastNodes = formatGenericWithGen(lastParam, sep, null)
618620
if (normalParams.isEmpty()) {
619621
nodes(Group(newId(), lastNodes), trailingNode)
620622
} else {
621623
val separator = getSeparator(normalParams.last(), lastParam[0], Space)
622-
val paramNodes = formatGeneric(normalParams, sep)
624+
val paramNodes = formatGenericWithGen(normalParams, sep, null)
623625
nodes(Group(newId(), paramNodes), separator, Group(newId(), lastNodes), trailingNode)
624626
}
625627
} else {

pkl-formatter/src/test/files/FormatterSnippetTests/input/line-breaks3.pkl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,8 @@ res6 = foo.bar(
3434
res7 = foo + bar(
3535
3
3636
) + baz
37+
38+
res8 = Map(
39+
1, 2,
40+
3, 4
41+
)

pkl-formatter/src/test/files/FormatterSnippetTests/output/line-breaks.pkl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,7 @@ funcParam2 =
4949
funcParam3 = aFun(foo, 10 * 10, anotherVariable, 200000, (param1, param2) -> param1 * param2)
5050

5151
funcParam4 =
52-
aFun(foo,
53-
10 * 10,
54-
anotherVariable,
55-
if (true) 100000 else 200000, new Listing {
52+
aFun(foo, 10 * 10, anotherVariable, if (true) 100000 else 200000, new Listing {
5653
1
5754
2
5855
})

pkl-formatter/src/test/files/FormatterSnippetTests/output/line-breaks3.pkl

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,13 @@ res5 =
3434
// don't respect newlines right after opening paren and closing paren
3535
res6 = foo.bar(1, 2, 3)
3636

37-
// only respect newlines between binary operators (we still want to inline `bar(3)` here).
37+
// only respect newlines on either side of a binary operator (we still want to inline `bar(3)` here).
3838
res7 = foo + bar(3) + baz
39+
40+
res8 =
41+
Map(
42+
1, 2,
43+
3, 4,
44+
)
45+
46+
res9 = Map(1, 2, 3, 4)

0 commit comments

Comments
 (0)