Skip to content

Conversation

@davidwengier
Copy link
Member

@davidwengier davidwengier commented Oct 26, 2025

Fixes #12310
Fixes #6632

This doesn't do a perfect job of formatting these, because there is an extra level of indentation in some (all?) cases, because the Html formatter doesn't see the @<tag> as a tag. It's a lot better than what we did previously (see screenshot in #12310), and likely needs #11916 for us to be able to do anything better, so I think this is still a net positive for users.

This means we do a much better, but not perfect, job of formatting render fragments.
@davidwengier davidwengier requested a review from a team as a code owner October 26, 2025 21:30
This is an unfortunate regression, but IMO the PR still makes for a better overall formatting experience
@davidwengier davidwengier requested a review from Copilot October 29, 2025 00:33
Copy link
Contributor

Copilot AI left a comment

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 addresses formatting issues for multi-line RenderFragment expressions in Razor code by improving how the C# formatter generates lambda expressions for these constructs. The key changes involve:

  • Adding special handling for multi-line RenderFragments to emit block-bodied lambda expressions instead of null statements
  • Tracking whether Roslyn places opening braces on new lines in lambda expressions based on formatting options
  • Enhancing the logic to skip opening braces that may be placed on separate lines by Roslyn

Reviewed Changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 3 comments.

File Description
HtmlFormattingTest.cs Updated test expectation to align with improved Caption attribute indentation
DocumentFormattingTest.cs Added semicolons to RenderFragment variable declarations and added 7 new formatting tests covering multi-line RenderFragment scenarios with various brace placement options
CSharpFormattingPass.cs Enhanced brace-skipping logic to check for first non-whitespace character and handle lambda opening braces, with updated comment explaining the behavior
CSharpFormattingPass.CSharpDocumentGenerator.cs Added core logic to detect multi-line RenderFragments, emit lambda expressions instead of null statements, track formatting options for brace placement, and properly close lambda blocks

formattedCSharpText.Lines[iFormatted + 1] is { Span.Length: > 0 } nextLine &&
nextLine.CharAt(0) == '{')
nextLine.GetFirstNonWhitespaceOffset() is { } firstNonWhitespace &&
nextLine.Start + firstNonWhitespace == nextLine.End - 1 &&
Copy link
Contributor

Choose a reason for hiding this comment

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

nextLine.Start + firstNonWhitespace == nextLine.End - 1 &&

Just checking we don't need the equivalent for the pretty similar if condition below covering original

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, good call, thanks. I think I just added this because I noticed it was missing, clearly didn't look far enough down the page :)

Copy link
Contributor

@ToddGrun ToddGrun left a comment

Choose a reason for hiding this comment

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

:shipit:

@davidwengier davidwengier merged commit 84f0e9e into dotnet:main Oct 29, 2025
11 checks passed
@davidwengier davidwengier deleted the BetterRenderFragmentFormatting branch October 29, 2025 23:21
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Razor Editor Formatting not working on RenderFragment methods Render fragments formatted to one indent level

2 participants