Repository navigation
Add MultilineCaseExpression script generator option - #233
Open
trg-alasdair wants to merge 2 commits into
Open
trg-alasdair wants to merge 2 commits into
trg-alasdair wants to merge 2 commits into
Conversation
A CASE expression is always generated on one line, so a multi-branch CASE becomes a single long line, and a WHEN predicate joined by AND breaks under MultilineWherePredicatesList with the next WHEN appended after its continuation, producing a staircase. MultilineCaseExpression (default false) writes each WHEN clause and the ELSE on its own line, indented one level from CASE, and END on its own line aligned with CASE. It applies to simple and searched CASE. The alignment point is pushed only when the option is on, so the default output is unchanged. A trailing single-line comment on a branch now stays on that branch's line, because the deferred comment is flushed at the next line break.
trg-alasdair
requested review from
allancascante,
Karl Burtram (kburtram) and
Leila Lali (llali)
as code owners
September 30, 2026 16:21
Author
|
@microsoft-github-policy-service agree [company="{TRG Arts}"] |
Author
|
@microsoft-github-policy-service agree company="TRG Arts" |
Contributor
|
With MultilineCaseExpression and PreserveComments enabled, a comment before a WHEN causes that branch to lose its indentation. For example: I reproduced this with both line and block comments, for simple and searched CASE. The comment is emitted during GenerateFragmentIfNotNull(when); its newline restores the outer CASE alignment point, so the following WHEN starts at the wrong column. Could you preserve the branch indentation across comment-generated newlines and add regression tests for these cases? |
With MultilineCaseExpression and PreserveComments enabled: - A comment before a WHEN ended in a newline that restored the CASE column, so the WHEN lost its branch indentation. Each WHEN now pushes its own alignment point. - A comment before ELSE was written after the ELSE keyword, which dropped the else expression from the output. - A comment before END was dropped. Comments before ELSE and END are now written on their own lines at the branch column. Regression tests cover line and block comments for simple and searched CASE. EmitPendingCommentsOnOwnLines is the third scan of the trivia run, after EmitPendingGapComments and EmitCommentsUntilNextNonTriviaToken. It is a separate method so that the existing ones stay unchanged. The scans could be merged by adding an optional startLine parameter to EmitPendingGapComments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Fixes #226.
SqlScriptGeneratoralways writes aCASEexpression on one line. A multi-branchCASEbecomes one long line, and when aWHENpredicate containsAND,MultilineWherePredicatesListbreaks it and appends the nextWHENafter the continuation, which produces the staircase shown in the issue.This adds
MultilineCaseExpression(defaultfalse). When it istrue, eachWHENclause and theELSEare written on their own line, indented one level fromCASE, andENDis written on its own line aligned withCASE:CASE, wherever the expression appears. A nestedCASEaligns with its ownCASEkeyword.CASEstays on theCASEline.WHENpredicates keep their existing behavior underMultilineWherePredicatesList. Because eachWHENnow starts its own line, the continuations no longer compound.The setting is in
SqlScriptGeneratorOptions.xmlin a new "CASE expressions" group, and the generator change is confined toSqlScriptGeneratorVisitor.CaseExpression.cs.This change was written with the help of an AI coding assistant, and I reviewed and tested it.
Code Changes
CaseExpressionFormattingTests(12 tests), covering the default, simple and searchedCASE, noELSE, a single branch, nesting,COLLATE,IndentationSize, a trailing comment, and the example from Add a Multiline option for CASE expressions (WHEN/THEN/ELSE on their own lines) #226 with otherwise-default optionsUTSqlScriptDomon net8.0 (macOS)