C#: Fix AutoFormat not inserting newlines in synthesized nodes#7189
Merged
knutwannheden merged 3 commits intomainfrom Mar 28, 2026
Merged
Conversation
Roslyn's Formatter.Format has a SuppressWrappingIfOnSingleLine heuristic
that preserves single-line formatting when all tokens are on one line.
This meant synthesized nodes with Space.Empty (produced by templates or
manual AST construction) stayed compressed on one line after formatting.
Fix: set WrappingPreserveSingleLine and WrappingKeepStatementsOnSingleLine
to false in the Roslyn options, so the formatter inserts structural newlines
(after {, before }, before else, etc.) even when the input has none.
…ement templates - AutoFormatManualIfElseWithMaybeAutoFormatAtBlockLevel: reproduces the AvoidNestingTernary pattern (manual AST construction with Space.Empty, conditions extracted from ternary, MaybeAutoFormat at Block level) - AutoFormatSplicedMultiStatementTemplate: multi-statement template that produces a SyntheticBlock, flattened and individually formatted - AutoFormatSplicedSubtreePreservesSurroundingCode: formatting quirks in surrounding code are preserved when only the spliced subtree is formatted - Rename RoslynFormatterNormalizesWhitespace to reflect new behavior
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.
Motivation
When a recipe constructs AST nodes with
Space.Empty(e.g., building an if/else chain from scratch, or usingCSharpTemplatewith compressed formatting),AutoFormatproduces single-line output likeif (a) { s = "A"; } else { s = "D"; }instead of properly formatted multi-line code.The root cause is Roslyn's
Formatter.Formathas aSuppressWrappingIfOnSingleLineheuristic that preserves single-line formatting when all tokens are on one line. Since synthesized nodes withSpace.Emptyprint without newlines, Roslyn sees "everything is on one line" and adds spaces but never inserts structural newlines.Summary
CSharpFormattingOptions.WrappingPreserveSingleLineandWrappingKeepStatementsOnSingleLinetofalseinFormatWithRoslyn, disabling the single-line preservation heuristic so Roslyn inserts structural newlines (after{, before}, beforeelse, etc.)BuildOptionshelper inRoslynFormatterto centralize Roslyn option configurationRewriteRunintegration tests:AvoidNestingTernarypattern (with surrounding formatting quirks preserved)SyntheticBlockthat gets flattened and individually formattedTest plan
AvoidNestingTernarytests in recipes-csharp (branchquiet-badger) pass — 5/5