Repository: apple/pkl
File: pkl-formatter/src/main/kotlin/org/pkl/formatter/Builder.kt (Kotlin)

Code before:
        if (node.type == NodeType.ARGUMENT_LIST_ELEMENTS) {
          formatArgumentListElements(node, hasTrailingLambda, twoBy2 = twoBy2)
        } else format(node)
      }
    return Group(groupId, nodes)
  }

  private fun formatArgumentListElements(
    node: Node,
    hasTrailingLambda: Boolean = false,
    twoBy2: Boolean = false,
  ): FormatNode {
    val children = node.children
    return if (twoBy2) {
      val pairs = pairArguments(children)
      val nodes =
        formatGenericWithGen(pairs, spaceOrLine()) { node, _ ->
          if (node.type == NodeType.ARGUMENT_LIST_ELEMENTS) {
            Group(newId(), formatGeneric(node.children, spaceOrLine()))
          } else {
            format(node)
          }
        }
      Indent(nodes)
    } else if (hasTrailingLambda) {
      // if the args have a trailing expression (lambda, new, amends) group them differently
      val splitIndex = children.indexOfLast { it.type in SAME_LINE_EXPRS }
      val normalParams = children.subList(0, splitIndex)
      val lastParam = children.subList(splitIndex, children.size)
      val trailingNode = if (endsWithClosingBracket(lastParam.last())) Empty else line()
      val lastNodes = formatGeneric(lastParam, spaceOrLine())
      if (normalParams.isEmpty()) {
        group(Group(newId(), lastNodes), trailingNode)
      } else {
        val separator = getSeparator(normalParams.last(), lastParam[0], Space)
        val paramNodes = formatGeneric(normalParams, spaceOrLine())
        group(Group(newId(), paramNodes), separator, Group(newId(), lastNodes), trailingNode)
      }
    } else {
      Indent(formatGeneric(children, spaceOrLine()))
    }
  }

  private tailrec fun endsWithClosingBracket(node: Node): Boolean {
    return if (node.children.isNotEmpty()) {
      endsWithClosingBracket(node.children.last())
    } else {
      node.isTerminal("}") || node.type.isAffix
    }
  }


Review comment (refactor):
This logic is arbitrary and error prone.
It doesn't work for many different cases or works by chance:
```pkl
// doesn't multiline
p1 = foo(1
        ,2
        ,3)

// here it does
p2 = foo(1,
        2
        ,3)

// doesn't multiline
p3 = foo(1, 2, 3, it
        .asd)

// works by chance, because we respect newlines after comments
p4 = foo(1, 2, 3, /*foo*/
        it)
```
I think we should go back to the drawing board with this logic.
Maybe remove this part and merge the rest. This could be a subsequent PR.

Code after:
              if (grammarVersion == GrammarVersion.V1) {
                node
              } else {
                ifWrap(groupId, nodes(Text(","), node), node)
              }
            } else node
          } else spaceOrLine()
        },
      ) { node, _ ->
        if (node.type == NodeType.ARGUMENT_LIST_ELEMENTS) {
          formatArgumentListElements(node, hasTrailingLambda, twoBy2 = twoBy2)
        } else format(node)
      }
    return Group(groupId, nodes)
  }

  private fun formatArgumentListElements(
    node: Node,
    hasTrailingLambda: Boolean = false,
    twoBy2: Boolean = false,
  ): FormatNode {
    val children = node.children
    val shouldMultiline = shouldMultlineNodes(node) { it.isTerminal(",") }
    val sep: (Node, Node) -> FormatNode = { _, _ ->
      if (shouldMultiline) forceSpaceyLine() else spaceOrLine()
    }
    return if (twoBy2) {
      val pairs = pairArguments(children)
      val nodes =
        formatGenericWithGen(pairs, sep) { node, _ ->
          if (node.type == NodeType.ARGUMENT_LIST_ELEMENTS) {
            Group(newId(), formatGeneric(node.children, spaceOrLine()))
          } else {
            format(node)
          }
        }
      Indent(nodes)
    } else if (hasTrailingLambda) {
      // if the args have a trailing lambda, group them differently
      val splitIndex = children.indexOfLast { it.type in SAME_LINE_EXPRS }
      val normalParams = children.subList(0, splitIndex)
      val lastParam = children.subList(splitIndex, children.size)
      val trailingNode = if (endsWithClosingBracket(lastParam.last())) Empty else line()
      val lastNodes = formatGenericWithGen(lastParam, sep, null)
      if (normalParams.isEmpty()) {
        group(Group(newId(), lastNodes), trailingNode)
      } else {
        val separator = getSeparator(normalParams.last(), lastParam[0], Space)
        val paramNodes = formatGenericWithGen(normalParams, sep, null)
        group(Group(newId(), paramNodes), separator, Group(newId(), lastNodes), trailingNode)
      }