More fixes for Razor multiline attribute formatting - #84656
Merged
davidwengier merged 3 commits intoAug 1, 2026
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3ca9d679-8979-4220-8c0f-4d62df700c61
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3ca9d679-8979-4220-8c0f-4d62df700c61
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 3ca9d679-8979-4220-8c0f-4d62df700c61
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adjusts Razor’s C# formatting-document generation so multiline attribute expressions—especially collection expressions—format deterministically and don’t “drift” indentation across repeated format passes. It also significantly expands regression coverage in the cohosting formatting test suite.
Changes:
- Updates the C# formatting document generator to account for collection-expression-specific parsing/indentation behaviors when formatting multiline attribute expressions.
- Adds collection-expression-aware scaffolding in start-tag/attribute handling to ensure Roslyn formats these expressions in a stable context.
- Adds extensive new unit tests covering stability and “unindented input → formatted output” scenarios across nesting and attribute-indent styles.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Razor/src/Razor/test/Microsoft.CodeAnalysis.Razor.CohostingShared.UnitTests/Formatting/DocumentFormattingTest.cs | Adds/renames many formatting regression tests for multiline explicit expressions and collection expressions in attributes (stable + formatting scenarios). |
| src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs | Updates the formatting-document generation logic to handle multiline collection expressions in attributes without indentation drift and with correct Roslyn parsing context. |
chsienki
approved these changes
Jul 31, 2026
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.
Fixes dotnet/razor#13121 again
My previous fix didn't have enough testing, and then collection expressions came along and broke it in more ways. This time I've added a bunch more test coverage, most of which didn't need many changes, and also collection expression coverage/support, which needs specific handling lest Roslyn think a collection expression is an attribute list, due to the standalone way we write out C# expressions from attributes.
Some of the tests
expecteddon't look that good, but I'm okay with it because a) it mirrors roslyn, which doesn't indent anything but the first line of a multiline expression in most cases and b) the results are at least stable, which is proven by multiple new tests, so worst case the user can manually fix up the results, and the formatter won't undo their work.Microsoft Reviewers: Open in CodeFlow