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

Code before:
    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)
    val nodes =
      formatGeneric(flat) { prev, next ->
        if (prev.type == NodeType.OPERATOR) {
          when (prev.text()) {
            "-" -> spaceOrLine()
            else -> Space
          }
        } else if (next.type == NodeType.OPERATOR) {
          when (next.text()) {
            "-" -> Space
            else -> spaceOrLine()
          }
        } else spaceOrLine()
      }

Review comment (suggestion):
Almost there, but you can't rely on `next?.type == LET_EXPR` because you might have more than one comment (in fact you should add this case to the tests):
```pkl
let (x = 1)
  // comment1
  // comment2
  x
```
Probably easier to check if the expression is another let and just indent based on that:
```kotlin
private fun formatLetExpr(node: Node): FormatNode {
    val endsWithLet = node.children.last().type == NodeType.LET_EXPR
    val nodes =
      formatGenericWithGen(
        node.children,
        { _, next -> if (next.type == NodeType.LET_PARAMETER_DEFINITION) Space else spaceOrLine() },
      ) { node, _ ->
        when {
          node.type == NodeType.LET_EXPR -> {
            // unpack the lets
            val group = formatLetExpr(node) as Group
            Nodes(group.nodes)
          }
          endsWithLet -> format(node)
          node.type.isExpression || node.type.isAffix -> indent(format(node))
          else -> format(node)
        }
      }
    return Group(newId(), nodes)
  }
```

Code after:
    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 endsWithLet = node.children.last().type == NodeType.LET_EXPR
    val nodes =
      formatGenericWithGen(
        node.children,
        { _, next -> if (next.type == NodeType.LET_PARAMETER_DEFINITION) Space else separator },
      ) { node, _ ->
        when {
          node.type == NodeType.LET_EXPR -> {
            // unpack the lets
            val group = formatLetExpr(node) as Group
            Nodes(group.nodes)
          }
          endsWithLet -> format(node)
          node.type.isExpression || node.type.isAffix -> 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)
    val nodes =
      formatGeneric(flat) { prev, next ->
        if (prev.type == NodeType.OPERATOR) {
          when (prev.text()) {
            "-" -> spaceOrLine()
            else -> Space
          }
        } else if (next.type == NodeType.OPERATOR) {
          when (next.text()) {
            "-" -> Space