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

Code before:
  private fun formatClassBody(node: Node): FormatNode {
    val children = node.children
    if (children.size == 2) {
      // no members
      return Nodes(formatGeneric(children, null))
    }
    return Group(newId(), formatGeneric(children, forceLine()))
  }

  private fun formatClassBodyElements(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { prev, next ->
        val lineDiff = prev.linesBetween(next)
        if (lineDiff > 1 || lineDiff == 0) TWO_NEWLINES else forceLine()
      }
    return Indent(nodes)
  }

  private fun formatClassProperty(node: Node): FormatNode {
    val sameLine =
      node.children
        .lastOrNull { it.isExpressionOrPropertyBody() }
        ?.let {
          if (it.type.isExpression) isSameLineExpr(it) else isSameLineExpr(it.children.last())
        } ?: false
    val nodes =
      groupNonPrefixes(node) { children ->
        val nodes =
          formatGenericWithGen(children, { _, _ -> if (sameLine) Space else spaceOrLine() }) {
            node,
            _ ->
            if ((node.isExpressionOrPropertyBody()) && !sameLine) {
              indent(format(node))
            } else format(node)
          }
        groupOnSpace(nodes)
      }
    return Nodes(nodes)
  }

  private fun Node.isExpressionOrPropertyBody(): Boolean =
    type.isExpression ||
      type == NodeType.CLASS_PROPERTY_BODY ||
      type == NodeType.OBJECT_PROPERTY_BODY

  private fun formatClassPropertyHeader(node: Node): FormatNode {
    return Group(newId(), formatGeneric(node.children, spaceOrLine()))
  }

  private fun formatClassPropertyHeaderBegin(node: Node): FormatNode {
    return Group(newId(), formatGeneric(node.children, spaceOrLine()))

Review comment (suggestion):
We should never concat nodes together without a separator, as we don't know where an affix can appear.
Same for line 323.
```suggestion
          leadingNodes + getSeparator(leadingNodes.last, trailingNodes, null) + trailingNodes
```

Code after:
                lambdaCount++
              }
            }
            methodCallCount == 0 -> {
              indexBeforeFirstMethodCall = flat.lastIndex
            }
          }
        }
      }
    }

    gatherFacts(node)

    val separator: (Node, Node) -> FormatNode? = { prev, next ->
      when {
        prev.type == NodeType.OPERATOR -> null
        next.type == NodeType.OPERATOR -> if (lambdaCount > 1) forceLine() else line()
        else -> spaceOrLine()
      }
    }

    val nodes =
      when {
        methodCallCount == 1 && isMethodCall(flat.lastProperNode()!!) -> {
          // lift argument list into its own node
          val (callChain, argsList) = splitFunctionCallNode(flat)
          val leadingNodes = indentAfterFirstNewline(formatGeneric(callChain, separator), true)
          val trailingNodes = formatGeneric(argsList, separator)
          val sep = getBaseSeparator(callChain.last(), argsList.first())
          if (sep != null) {
            leadingNodes + sep + trailingNodes
          } else {
            leadingNodes + trailingNodes
          }
        }
        methodCallCount > 0 && indexBeforeFirstMethodCall > 0 -> {
          val leading = flat.subList(0, indexBeforeFirstMethodCall)
          val trailing = flat.subList(indexBeforeFirstMethodCall, flat.size)
          val leadingNodes = indentAfterFirstNewline(formatGeneric(leading, separator), true)
          val trailingNodes = formatGeneric(trailing, separator)
          leadingNodes + line() + trailingNodes
        }
        else -> formatGeneric(flat, separator)
      }

    val shouldGroup = node.children.size == flat.size
    return Group(newId(), indentAfterFirstNewline(nodes, shouldGroup))
  }

  /**
   * Split a function call node to extract its identifier into the leading group. For example,