fix(body): handle CASE depth in body statement formatting
CI / Test (push) Successful in 28s
CI / Build (push) Successful in 25s

This commit is contained in:
Hein
2026-07-02 14:40:54 +02:00
parent 4fab2fe652
commit 44fb77efd6
+25 -1
View File
@@ -379,6 +379,7 @@ func formatBodyStatements(text string, st config.Style) string {
stmt []bline stmt []bline
parenDepth int parenDepth int
blockDepth int // 0=col-0 (BEGIN/END/EXCEPTION), 1=body, 2=nested… blockDepth int // 0=col-0 (BEGIN/END/EXCEPTION), 1=body, 2=nested…
caseDepth int // depth of open CASE…END expressions (WHEN…THEN is not a block opener)
inException bool inException bool
pendingBlanks int pendingBlanks int
depthInc bool // increment blockDepth after next flush depthInc bool // increment blockDepth after next flush
@@ -529,18 +530,41 @@ func formatBodyStatements(text string, st config.Style) string {
} }
if parenDepth == 0 && tok.Kind == lexer.Ident { if parenDepth == 0 && tok.Kind == lexer.Ident {
lastD0Kw = lowerASCII(tok.Text) lastD0Kw = lowerASCII(tok.Text)
switch lastD0Kw {
case "case":
caseDepth++
case "end":
if caseDepth > 0 {
caseDepth--
}
}
} }
} }
if parenDepth == 0 && len(stmt) > 0 { if parenDepth == 0 && len(stmt) > 0 {
switch lastD0Kw { switch lastD0Kw {
case "then", "loop", "begin": case "then":
// A THEN ending a CASE…WHEN branch is not a PL/pgSQL block
// opener; only one matching END closes the whole CASE, so
// treating each WHEN…THEN as a block open would permanently
// inflate blockDepth.
if caseDepth == 0 {
fw0 := lowerASCII(firstBodyKeyword(stmt[0].text))
if fw0 != "elsif" && fw0 != "elseif" {
depthInc = true
}
flush()
}
case "loop", "begin":
fw0 := lowerASCII(firstBodyKeyword(stmt[0].text)) fw0 := lowerASCII(firstBodyKeyword(stmt[0].text))
if fw0 != "elsif" && fw0 != "elseif" { if fw0 != "elsif" && fw0 != "elseif" {
depthInc = true depthInc = true
} }
flush() flush()
case "else", "exception": case "else", "exception":
if caseDepth > 0 {
break
}
flush() flush()
} }
} }