fix(body): prevent joining continuation lines to comments
* Ensure comment-only lines are flushed before adding new statements * Adjust logic to handle col-0 continuation lines correctly
This commit is contained in:
@@ -471,6 +471,30 @@ func formatBodyStatements(text string, st config.Style) string {
|
||||
fw := lowerASCII(firstBodyKeyword(stripped))
|
||||
isColZero := indent == ""
|
||||
joinToPrev := isColZero && parenDepth == 0 && len(stmt) > 0 && !sqlClauseKw[fw]
|
||||
// Don't join a col-0 continuation to a comment-only preceding bline:
|
||||
// the comment has no structural keyword so `continue ;` at col-0 would
|
||||
// disappear into the comment text and be invisible to the lexer.
|
||||
if joinToPrev {
|
||||
if lowerASCII(firstBodyKeyword(stmt[len(stmt)-1].text)) == "" {
|
||||
joinToPrev = false
|
||||
}
|
||||
}
|
||||
// Pre-flush pending comment-only blines before adding a new non-comment
|
||||
// non-joined bline. Without this, a comment + `end if;` end up in the
|
||||
// same stmt, `fw` comes from the comment (empty string), depth is never
|
||||
// decremented, and the formatter diverges on the second pass.
|
||||
if !joinToPrev && fw != "" && len(stmt) > 0 {
|
||||
allComments := true
|
||||
for _, ll := range stmt {
|
||||
if lowerASCII(firstBodyKeyword(ll.text)) != "" {
|
||||
allComments = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if allComments {
|
||||
flush()
|
||||
}
|
||||
}
|
||||
|
||||
if joinToPrev {
|
||||
last := &stmt[len(stmt)-1]
|
||||
|
||||
Reference in New Issue
Block a user