Skip to content

More fixes for Razor multiline attribute formatting - #84656

Merged
davidwengier merged 3 commits into
dotnet:mainfrom
davidwengier:test-multiline-attribute-formatting
Aug 1, 2026
Merged

More fixes for Razor multiline attribute formatting#84656
davidwengier merged 3 commits into
dotnet:mainfrom
davidwengier:test-multiline-attribute-formatting

Conversation

@davidwengier

@davidwengier davidwengier commented Jul 28, 2026

Copy link
Copy Markdown
Member

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 expected don'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

davidwengier and others added 3 commits July 28, 2026 17:52
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
Copilot AI review requested due to automatic review settings July 28, 2026 08:02
@davidwengier
davidwengier requested a review from a team as a code owner July 28, 2026 08:02
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@davidwengier
davidwengier merged commit 31722b8 into dotnet:main Aug 1, 2026
27 checks passed
@davidwengier
davidwengier deleted the test-multiline-attribute-formatting branch August 1, 2026 00:29
@jjonescz jjonescz added this to the 18.11 milestone Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Multiline explicit expressions in attributes don't format correctly

4 participants