Add a Multiline option for CASE expressions (WHEN/THEN/ELSE on their own lines)
Bewertung
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Anfängerfreundlichkeit
- 56/100
Rechercherichtung
Beginne damit, SqlScriptGeneratorOptions und das Verhalten von Sql170ScriptGenerator für die vorhandenen Multiline*-Optionen zu lesen, und vergleiche anschließend, wie CASE-Ausdrücke ausgegeben werden. Verfolge die relevanten Generator-Tests, falls vorhanden, und überprüfe, dass der Standard die Inline-Ausgabe beibehält, während MultilineCaseExpression die WHEN- und ELSE-Zweige in separaten eingerückten Zeilen platziert.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Beschreibung
Is your feature request related to a problem? Please describe
When Sql170ScriptGenerator regenerates a script, a CASE expression is always emitted inline — every
WHEN/THEN/ELSE is joined onto one logical line. For multi-branch CASE expressions this makes the
output substantially harder to read than the input, and where a WHEN predicate has multiple conjuncts
the hanging indent compounds: each AND is indented to the width of the preceding WHEN, and the next
WHEN is then appended after that indented continuation, producing a staircase.
Input (longest line 81 chars):
SELECT CASE
WHEN o.status = 'P' AND o.paid_amount >= o.total_amount THEN 'settled'
WHEN o.status = 'P' AND o.paid_amount > 0 THEN 'part-paid'
WHEN o.status = 'C' THEN 'cancelled'
ELSE 'open'
END AS settlement_state,
CASE WHEN o.due_date < SYSUTCDATETIME() THEN 1 ELSE 0 END AS is_overdue
FROM orders AS o
WHERE o.tenant_id = 42;
Output with MultilineSelectElementsList = true, IndentationSize = 4 (longest line 185 chars):
SELECT CASE WHEN o.status = 'P'
AND o.paid_amount >= o.total_amount THEN 'settled' WHEN o.status = 'P'
AND o.paid_amount > 0 THEN 'part-paid' WHEN o.status = 'C' THEN 'cancelled' ELSE 'open' END AS settlement_state,
CASE WHEN o.due_date < SYSUTCDATETIME() THEN 1 ELSE 0 END AS is_overdue
FROM orders AS o
WHERE o.tenant_id = 42;
An 81-character input becomes a 185-character line. There is currently no option that affects this:
of the 46 properties on SqlScriptGeneratorOptions, none matches Case or When, and none provides a
maximum line width. AlignClauseBodies, ClauseBodyAlignment and the Multiline* options do not reach
inside a CASE.
Describe the solution you'd like
A MultilineCaseExpression option (default false, preserving current output) that places each WHEN
branch and the ELSE on its own line, indented one level from the CASE:
SELECT CASE
WHEN o.status = 'P' AND o.paid_amount >= o.total_amount THEN 'settled'
WHEN o.status = 'P' AND o.paid_amount > 0 THEN 'part-paid'
WHEN o.status = 'C' THEN 'cancelled'
ELSE 'open'
END AS settlement_state,
This fits the existing naming and behaviour of the Multiline* family —
MultilineSelectElementsList, MultilineWherePredicatesList, MultilineInValuesList,
MultilineViewColumnsList, MultilineSetClauseItems, MultilineInsertTargetsList,
MultilineInsertSourcesList, MultilineProcedureParametersList — so it should need no new concepts.
A single-branch CASE staying inline when the option is off (as
CASE WHEN … THEN 1 ELSE 0 END above) seems the right default; whether a one-branch CASE should also
break under the option is a judgement call I have no strong view on.
Describe alternatives you've considered
- Existing options —
AlignClauseBodies,ClauseBodyAlignment = Indented,
MultilineSelectElementsList, and reducedIndentationSize. None affects the interior of aCASE;
the best combination I found did not shorten these lines. - A general maximum-line-width option. More broadly useful, but a much larger change and a different
model from the current per-construct toggles, so I have not requested it here. - Post-processing the generated text. Re-wrapping
CASEwith a regex outside the generator defeats
the point of using an AST-based formatter and is not reliable in the presence of string literals.
Additional context
Microsoft.SqlServer.TransactSql.ScriptDom180.78.1 (assembly 18.0.0.0),net8.0; behaviour is the
same onSql160ScriptGenerator,Sql170ScriptGeneratorandSql180ScriptGenerator.- Related open requests of the same shape, both asking for a construct to be broken across lines:
#189 (INSERT columns) and #21 (stored-procedure parameters). - Happy to test a change against a large real-world T-SQL corpus if that is useful.
- Vorherrschende Sprache
- GAP
- Sterne
- 278
- Forks
- 46
- Ø Merge
- 9 T. 23 Std.
- Gemergte PRs (30 T.)
- 2
Entwicklungsumgebung
- Kein Dockerfile und keine Docker-Compose-Datei
- Hat eine Pull-Request-Vorlage
- Beitragsleitfaden lesen
Erste Schritte
- Lesen Sie das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreiben Sie ins Issue, dass Sie es übernehmen — das erspart doppelte Arbeit.
- Forken Sie das Repository und arbeiten Sie in einem Branch.
- Öffnen Sie einen Pull Request, der die Issue-Nummer nennt.
Mehr aus microsoft/SqlScriptDOM
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 84/100
microsoft/SqlScriptDOM#228 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 62/100
microsoft/SqlScriptDOM#183 ·
-
Schwierigkeit 4/5 3-5 Tage Anfängerfreundlichkeit 48/100
microsoft/SqlScriptDOM#225 ·
-
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 58/100
microsoft/SqlScriptDOM#224 ·
-
BACKUP_PRIORITY does not parse while using clause ADD REPLICA ONEvtl. vergeben @ZEUSXXIV hat das vor 48 Tagen übernommen. Offen
Schwierigkeit 3/5 1-2 Tage Anfängerfreundlichkeit 68/100
microsoft/SqlScriptDOM#222 ·
Alle Issues in microsoft/SqlScriptDOM
Ähnliche Issues
-
Schwierigkeit 1/5 Unter einer Stunde Anfängerfreundlichkeit 91/100
-
agent-research-finding bug
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 78/100
jordansmall/nixvim-config#99 ·
Maintainer antworten meist innerhalb von 1 Tag
-
Schwierigkeit 2/5 Ein halber Tag Anfängerfreundlichkeit 70/100
Maintainer antworten meist innerhalb von 1 Tag
-
[New Skill] enforcing-dbt-lowercase-styleEvtl. vergeben @sebafreiman hat das heute übernommen. Offenenhancement
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 72/100
dbt-labs/dbt-agent-skills#169 ·
-
Schwierigkeit 2/5 1-3 Stunden Anfängerfreundlichkeit 65/100