diff --git a/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt b/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt index 75c6a282..c89f8046 100644 --- a/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt +++ b/pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt @@ -229,9 +229,14 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe if (children.size == 1) return format(children[0]) val firstNode = node.firstProperChild()!! return if (firstNode.text() == "Map") { - val nodes = mutableListOf() - nodes += format(firstNode) - nodes += formatArgumentList(children[1], twoBy2 = true) + val nodes = + formatGenericWithGen(children, null) { node, _ -> + if (node.type == NodeType.ARGUMENT_LIST) { + formatArgumentList(node, twoBy2 = true) + } else { + format(node) + } + } Nodes(nodes) } else { Nodes(formatGeneric(children, null)) @@ -591,7 +596,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe twoBy2: Boolean = false, ): FormatNode { val children = node.children - val shouldMultiline = shouldMultlineNodes(node) { it.isTerminal(",") } + val shouldMultiline = shouldMultilineNodes(node) { it.isTerminal(",") } val sep: (Node, Node) -> FormatNode = { _, _ -> if (shouldMultiline) forceSpaceyLine() else spaceOrLine() } @@ -626,7 +631,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe } } - private fun shouldMultlineNodes(node: Node, predicate: (Node) -> Boolean): Boolean { + private fun shouldMultilineNodes(node: Node, predicate: (Node) -> Boolean): Boolean { for (idx in 0..() + while (tmp.isNotEmpty() && tmp.last().type.isAffix) { + // trailing comments should not be paired + suffixes += tmp.removeLast() + } res += Node(NodeType.ARGUMENT_LIST_ELEMENTS, tmp) + while (suffixes.isNotEmpty()) { + res += suffixes.removeFirst() + } res += node commas = 0 tmp = mutableListOf() } else { tmp += node } + } else if (tmp.isEmpty() && node.type.isAffix) { + // leading comments should not be paired + res += node } else { tmp += node } @@ -1021,7 +1037,7 @@ internal class Builder(sourceText: String, private val grammarVersion: GrammarVe private fun formatBinaryOpExpr(node: Node): FormatNode { val flat = flattenBinaryOperatorExprs(node) - val shouldMultiline = shouldMultlineNodes(node) { it.type == NodeType.OPERATOR } + val shouldMultiline = shouldMultilineNodes(node) { it.type == NodeType.OPERATOR } val nodes = formatGeneric(flat) { prev, next -> val sep = if (shouldMultiline) forceSpaceyLine() else spaceOrLine() diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/input/map-function.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/input/map-function.pkl index 07f987a9..ad4682a0 100644 --- a/pkl-formatter/src/test/files/FormatterSnippetTests/input/map-function.pkl +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/input/map-function.pkl @@ -2,3 +2,19 @@ foo = Map(1000, "some random string", 20000, "another random string", 30000, "yet another random string") incorrect = Map("This has", 1000000, "an incorrect number", 2000000, "of parameters", 30000000, "passed to Map") + +bar = + Map( + // leading + 1, + 2, + // between + 3, + 4, // trailing + 5, + 6, + 7, // mid + 8, + 9, + 10 + ) diff --git a/pkl-formatter/src/test/files/FormatterSnippetTests/output/map-function.pkl b/pkl-formatter/src/test/files/FormatterSnippetTests/output/map-function.pkl index 1d4f6e74..7e40cdbe 100644 --- a/pkl-formatter/src/test/files/FormatterSnippetTests/output/map-function.pkl +++ b/pkl-formatter/src/test/files/FormatterSnippetTests/output/map-function.pkl @@ -12,3 +12,15 @@ incorrect = "of parameters", 30000000, "passed to Map", ) + +bar = + Map( + // leading + 1, 2, + // between + 3, 4, // trailing + 5, 6, + 7, // mid + 8, + 9, 10, + )