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

Code before:
    val sameLine =
      node.children
        .last { it.type == NodeType.FUNCTION_LITERAL_BODY }
        .let { body ->
          val expr = body.children.find { it.type.isExpression }!!
          isSameLineExpr(expr)
        }
    val sep = if (sameLine) Space else spaceOrLine()
    val bodySep = getSeparator(params.last(), rest.first(), sep)

    val nodes = formatGeneric(params, sep)
    val restNodes = listOf(bodySep) + formatGeneric(rest, sep)
    return Group(newId(), nodes + listOf(Group(newId(), restNodes)))
  }

  private fun formatFunctionLiteralBody(node: Node): FormatNode {
    val expr = node.children.find { it.type.isExpression }!!
    val nodes = formatGeneric(node.children, null)
    return if (isSameLineExpr(expr)) Group(newId(), nodes) else Indent(nodes)
  }

  private fun formatLetExpr(node: Node): FormatNode {
    val nodes =
      formatGenericWithGen(
        node.children,
        { prev, next ->
          when {
            next.type == NodeType.LET_PARAMETER_DEFINITION -> Space
            prev.type == NodeType.LET_PARAMETER_DEFINITION -> {
              val linesBetween = prev.linesBetween(next)
              if (linesBetween > 0) forceLine() else spaceOrLine()
            }
            else -> spaceOrLine()
          }
        },
      ) { node, next ->
        if (next == null) {
          if (node.type == NodeType.LET_EXPR) {
            // unpack the lets
            val group = formatLetExpr(node) as Group
            Nodes(group.nodes)
          } else indent(format(node))
        } else format(node)
      }
    return Group(newId(), nodes)
  }

  private fun formatLetParameterDefinition(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { prev, next ->
        if (prev.isTerminal("(") || next.isTerminal(")")) line() else spaceOrLine()

Review comment (suggestion):
Same comment as the if: you can just check once:
```kotlin
val separator = if (node.isMultiline()) forceLine() else spaceOrLine()
// ...
{ _, next -> if (next.type == NodeType.LET_PARAMETER_DEFINITION) Space else separator },
```

Code after:
        .last { it.type == NodeType.FUNCTION_LITERAL_BODY }
        .let { body ->
          val expr = body.children.find { it.type.isExpression }!!
          isSameLineExpr(expr)
        }
    val sep = if (sameLine) Space else spaceOrLine()
    val bodySep = getSeparator(params.last(), rest.first(), sep)

    val nodes = formatGeneric(params, sep)
    val restNodes = listOf(bodySep) + formatGeneric(rest, sep)
    return Group(newId(), nodes + listOf(Group(newId(), restNodes)))
  }

  private fun formatFunctionLiteralBody(node: Node): FormatNode {
    val expr = node.children.find { it.type.isExpression }!!
    val nodes = formatGeneric(node.children, null)
    return if (isSameLineExpr(expr)) Group(newId(), nodes) else Indent(nodes)
  }

  private fun formatLetExpr(node: Node): FormatNode {
    val separator = if (node.isMultiline()) forceLine() else spaceOrLine()
    val nodes =
      formatGenericWithGen(
        node.children,
        { _, next -> if (next.type == NodeType.LET_PARAMETER_DEFINITION) Space else separator },
      ) { node, next ->
        if (next == null) {
          if (node.type == NodeType.LET_EXPR) {
            // unpack the lets
            val group = formatLetExpr(node) as Group
            Nodes(group.nodes)
          } else indent(format(node))
        } else format(node)
      }
    return Group(newId(), nodes)
  }

  private fun formatLetParameterDefinition(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { prev, next ->
        if (prev.isTerminal("(") || next.isTerminal(")")) line() else spaceOrLine()
      }
    return Group(newId(), nodes)
  }

  private fun formatLetParameter(node: Node): FormatNode {
    return indent(formatClassProperty(node))
  }

  private fun formatBinaryOpExpr(node: Node): FormatNode {
    val flat = flattenBinaryOperatorExprs(node)