Skip to content

Conversation

@bioball
Copy link
Member

@bioball bioball commented Oct 27, 2025

Fixes an issue where line comments preceding let bodies are not indented.

Turn this:

bar3 =
  let (bar = new Dynamic {})
  // some coment
    (bar) {
      qux = 4
    }

Into this:

bar3 =
  let (bar = new Dynamic {})
    // some coment
    (bar) {
      qux = 4
    }

@bioball bioball force-pushed the line-comment-in-lets branch from 3920f46 to 2cb2529 Compare October 27, 2025 05:57
Comment on lines 941 to 993
node.children,
{ _, next -> if (next.type == NodeType.LET_PARAMETER_DEFINITION) Space else spaceOrLine() },
) { node, next ->
if (next == null) {
if (node.type == NodeType.LET_EXPR) {
when {
node.type == NodeType.LET_EXPR -> {
// unpack the lets
val group = formatLetExpr(node) as Group
Nodes(group.nodes)
} else indent(format(node))
} else format(node)
}
node.type == NodeType.LET_PARAMETER_DEFINITION ||
node.isTerminal("let") ||
next?.type == NodeType.LET_EXPR -> format(node)
else -> indent(format(node))
}
}
return Group(newId(), nodes)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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):

let (x = 1)
  // comment1
  // comment2
  x

Probably easier to check if the expression is another let and just indent based on that:

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)
  }

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, thanks!

@bioball bioball force-pushed the line-comment-in-lets branch from 6c14ac1 to 8e8e8d8 Compare October 28, 2025 18:33
Copy link
Contributor

@stackoverflow stackoverflow left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@bioball bioball merged commit bbeeffd into apple:main Oct 29, 2025
4 checks passed
@bioball bioball deleted the line-comment-in-lets branch October 29, 2025 00:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants