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

Code before:
          noNewlines = prevNoNewlines
          isInStringInterpolation = false
          continue
        }
        val elem = cursor.next()
        if (elem.type == NodeType.TERMINAL && elem.text().endsWith("(")) {
          isInStringInterpolation = true
        }
        add(format(elem))
        prev = elem
      }
    }
  }

  private fun formatSingleLineString(node: Node): FormatNode {
    return Group(newId(), formatStringParts(node.children))
  }

  private fun formatMultilineString(node: Node): FormatNode {
    val nodes = formatStringParts(node.children)
    return MultilineStringGroup(node.children.last().span.colBegin, nodes)
  }

  private fun formatIf(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { prev, next ->
        when {
          next.type == NodeType.IF_ELSE_EXPR && next.children[0].type == NodeType.IF_EXPR -> Space
          next.type == NodeType.IF_THEN_EXPR ||
            next.type == NodeType.IF_ELSE_EXPR ||
            next.isTerminal("else") ->
            if (prev.linesBetween(next) > 0) forceLine() else spaceOrLine()
          else -> spaceOrLine()
        }
      }
    return Group(newId(), nodes)
  }

  private fun formatIfHeader(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { _, next ->
        if (next.type == NodeType.IF_CONDITION) Space else spaceOrLine()
      }
    return Group(newId(), nodes)
  }

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

Review comment (refactor):
This could be simplified by only checking once:
```kotlin
private fun formatIf(node: Node): FormatNode {
  val separator = if (node.span.lineBegin() < node.span.lineEnd) forceLine() else spaceOrLine()
  val nodes =
    formatGeneric(node.children) { _, next ->
      if (next.type == NodeType.IF_ELSE_EXPR && next.children[0].type == NodeType.IF_EXPR) {
        Space
      } else separator
    }
  return Group(newId(), nodes)
}
```
Probably a good idea to extract the checking if a node is multiline to a function:
```kotlin
private fun Node.isMultiline(): Boolean = span.lineBegin < span.lineEnd
```

Code after:
          noNewlines = prevNoNewlines
          isInStringInterpolation = false
          continue
        }
        val elem = cursor.next()
        if (elem.type == NodeType.TERMINAL && elem.text().endsWith("(")) {
          isInStringInterpolation = true
        }
        add(format(elem))
        prev = elem
      }
    }
  }

  private fun formatSingleLineString(node: Node): FormatNode {
    return Group(newId(), formatStringParts(node.children))
  }

  private fun formatMultilineString(node: Node): FormatNode {
    val nodes = formatStringParts(node.children)
    return MultilineStringGroup(node.children.last().span.colBegin, nodes)
  }

  private fun formatIf(node: Node): FormatNode {
    val separator = if (node.isMultiline()) forceLine() else spaceOrLine()
    val nodes =
      formatGeneric(node.children) { _, next ->
        // produce `else if` in the case of nested if.
        // note: don't need to handle if `next.children[0]` is an affix because that can't be
        // emitted as `else if` anyway.
        if (next.type == NodeType.IF_ELSE_EXPR && next.children[0].type == NodeType.IF_EXPR) Space
        else separator
      }
    return Group(newId(), nodes)
  }

  private fun formatIfHeader(node: Node): FormatNode {
    val nodes =
      formatGeneric(node.children) { _, next ->
        if (next.type == NodeType.IF_CONDITION) Space else spaceOrLine()
      }
    return Group(newId(), nodes)
  }

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