feat(format): Doc-IR core, line_width (120) and where_wrap when_long

This commit is contained in:
Hein
2026-10-06 14:44:45 +02:00
parent 4ff729eeeb
commit 553609e988
7 changed files with 257 additions and 2 deletions
+26
View File
@@ -244,6 +244,9 @@ func dmlWhereClause(kwText string, body []cst.Tok, st config.Style) string {
}
nl := st.Newline
if st.WhereWrap == config.WrapWhenLong {
return dmlWhereWhenLong(kwText, conditions, st)
}
var b strings.Builder
b.WriteString(kwText)
for i, cond := range conditions {
@@ -261,6 +264,29 @@ func dmlWhereClause(kwText string, body []cst.Tok, st config.Style) string {
return b.String()
}
// dmlWhereWhenLong is the where_wrap: when_long layout: the whole clause stays
// on one line when it fits in line_width, otherwise it breaks exactly as
// where_wrap: always would.
func dmlWhereWhenLong(kwText string, conditions [][]cst.Tok, st config.Style) string {
prefix := ""
if st.WhereAndOrIndent {
prefix = st.Indent
}
parts := []Doc{Text(kwText)}
for i, cond := range conditions {
text := dmlInline(cond, st)
pfx := prefix
if i == 0 {
pfx += " " // align with AND/OR token width
}
// Continuation lines of a multi-line condition (subquery) keep the
// same prefix writeListItem would give them.
text = strings.ReplaceAll(text, st.Newline, st.Newline+pfx)
parts = append(parts, IfBreak(Concat(Text(st.Newline), Text(pfx)), Text(" ")), Text(text))
}
return Render(Group(Concat(parts...)), st.LineWidth, st.Indent, st.Newline)
}
// dmlSplitAndOr splits toks at depth-0 AND/OR tokens, keeping the AND/OR with
// the following condition.
func dmlSplitAndOr(toks []cst.Tok) [][]cst.Tok {