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

Code before:
        }
      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(children[splitIndex])) 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("}")
    }
  }

  /**
   * Only considered trailing lamdba if there is only one lambda/new expr/amends expr in the list.
   *
   * E.g. avoid formatting `toMap()` weirdly:
   * ```
   * foo.toMap(
   *   (it) -> makeSomeKey(it),
   *   (it) -> makeSomeValue(it),
   * )
   * ```
   */
  private fun hasTrailingLambda(argList: Node): Boolean {
    val children = argList.firstProperChild()?.children ?: return false
    var seenArg = false
    var ret = false
    for (i in children.lastIndex downTo 0) {
      val child = children[i]
      if (!child.isProper()) continue
      if (child.type in SAME_LINE_EXPRS) {
        if (seenArg) {
          return false
        } else {

Review comment (bug):
This has the side effect of not adding a newline even if the last element is a single line block comment:
```pkl
items = allItems.filter((it) ->
            it /*foo*/
          )

// formats to
items =
  allItems.filter((it) -> it
  /*foo*/)
```

But this is such an edge case I don't think it worth writing special handling.

Code after:
        }
      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
    }
  }

  /**
   * Only considered trailing lamdba if there is only one lambda/new expr/amends expr in the list.
   *
   * E.g. avoid formatting `toMap()` weirdly:
   * ```
   * foo.toMap(
   *   (it) -> makeSomeKey(it),
   *   (it) -> makeSomeValue(it),
   * )
   * ```
   */
  private fun hasTrailingLambda(argList: Node): Boolean {
    val children = argList.firstProperChild()?.children ?: return false
    var seenArg = false
    var ret = false
    for (i in children.lastIndex downTo 0) {
      val child = children[i]
      if (!child.isProper()) continue
      if (child.type in SAME_LINE_EXPRS) {
        if (seenArg) {
          return false
        } else {