@@ -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()
0 commit comments