Skip to content

Commit ecf2d8b

Browse files
Fix Map formatting (#1312)
1 parent 445d94c commit ecf2d8b

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
229229
if (children.size == 1) return format(children[0])
230230
val firstNode = node.firstProperChild()!!
231231
return if (firstNode.text() == "Map") {
232-
val nodes = mutableListOf<FormatNode>()
233-
nodes += format(firstNode)
234-
nodes += formatArgumentList(children[1], twoBy2 = true)
232+
val nodes =
233+
formatGenericWithGen(children, null) { node, _ ->
234+
if (node.type == NodeType.ARGUMENT_LIST) {
235+
formatArgumentList(node, twoBy2 = true)
236+
} else {
237+
format(node)
238+
}
239+
}
235240
Nodes(nodes)
236241
} else {
237242
Nodes(formatGeneric(children, null))
@@ -591,7 +596,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
591596
twoBy2: Boolean = false,
592597
): FormatNode {
593598
val children = node.children
594-
val shouldMultiline = shouldMultlineNodes(node) { it.isTerminal(",") }
599+
val shouldMultiline = shouldMultilineNodes(node) { it.isTerminal(",") }
595600
val sep: (Node, Node) -> FormatNode = { _, _ ->
596601
if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
597602
}
@@ -626,7 +631,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
626631
}
627632
}
628633

629-
private fun shouldMultlineNodes(node: Node, predicate: (Node) -> Boolean): Boolean {
634+
private fun shouldMultilineNodes(node: Node, predicate: (Node) -> Boolean): Boolean {
630635
for (idx in 0..<node.children.lastIndex) {
631636
val prev = node.children[idx]
632637
val next = node.children[idx + 1]
@@ -684,13 +689,24 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
684689
if (node.isTerminal(",")) {
685690
commas++
686691
if (commas == 2) {
692+
val suffixes = mutableListOf<Node>()
693+
while (tmp.isNotEmpty() && tmp.last().type.isAffix) {
694+
// trailing comments should not be paired
695+
suffixes += tmp.removeLast()
696+
}
687697
res += Node(NodeType.ARGUMENT_LIST_ELEMENTS, tmp)
698+
while (suffixes.isNotEmpty()) {
699+
res += suffixes.removeFirst()
700+
}
688701
res += node
689702
commas = 0
690703
tmp = mutableListOf()
691704
} else {
692705
tmp += node
693706
}
707+
} else if (tmp.isEmpty() && node.type.isAffix) {
708+
// leading comments should not be paired
709+
res += node
694710
} else {
695711
tmp += node
696712
}
@@ -1021,7 +1037,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe
10211037

10221038
private fun formatBinaryOpExpr(node: Node): FormatNode {
10231039
val flat = flattenBinaryOperatorExprs(node)
1024-
val shouldMultiline = shouldMultlineNodes(node) { it.type == NodeType.OPERATOR }
1040+
val shouldMultiline = shouldMultilineNodes(node) { it.type == NodeType.OPERATOR }
10251041
val nodes =
10261042
formatGeneric(flat) { prev, next ->
10271043
val sep = if (shouldMultiline) forceSpaceyLine() else spaceOrLine()

pkl-formatter/src/test/files/FormatterSnippetTests/input/map-function.pkl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,19 @@
22
foo = Map(1000, "some random string", 20000, "another random string", 30000, "yet another random string")
33

44
incorrect = Map("This has", 1000000, "an incorrect number", 2000000, "of parameters", 30000000, "passed to Map")
5+
6+
bar =
7+
Map(
8+
// leading
9+
1,
10+
2,
11+
// between
12+
3,
13+
4, // trailing
14+
5,
15+
6,
16+
7, // mid
17+
8,
18+
9,
19+
10
20+
)

pkl-formatter/src/test/files/FormatterSnippetTests/output/map-function.pkl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,15 @@ incorrect =
1212
"of parameters", 30000000,
1313
"passed to Map",
1414
)
15+
16+
bar =
17+
Map(
18+
// leading
19+
1, 2,
20+
// between
21+
3, 4, // trailing
22+
5, 6,
23+
7, // mid
24+
8,
25+
9, 10,
26+
)

0 commit comments

Comments
 (0)