Skip to content

Handle Html indentation ourselves, rather than using the IDE formatter#12621

Closed
davidwengier wants to merge 19 commits intodotnet:mainfrom
davidwengier:LetsDoHtmlFormatting
Closed

Handle Html indentation ourselves, rather than using the IDE formatter#12621
davidwengier wants to merge 19 commits intodotnet:mainfrom
davidwengier:LetsDoHtmlFormatting

Conversation

@davidwengier
Copy link
Member

@davidwengier davidwengier commented Dec 24, 2025

Fixes #11916
Fixes #11647
Fixes #12223
Fixes #12554, though I also created #12622 for one outstanding issue from the comments in that issue

Currently we get Html formatting changes from the IDE, and then try to mangle them and C# formatting changes together. This is difficult, and problematic because our tests run VS's Html formatter, and VS Code's behaves differently. This PR makes us take over Html indentation directly, only honouring the IDEs formatting changes - ie, we do everything before the first non-whitespace character, the IDE tells us what to do for everything after the first non-whitespace character.

This means we're now consistent across IDEs, and fixes a bunch of bugs around some of the hairier aspects of C# formatting like multiline expressions, object initializers etc. where Roslyn doesn't do any formatting, but the Html formatter would, and also fixes bugs to do with RenderFragments/C# templates which are a mix of Html and Razor which really showed up the cracks in the system.

Commit-at-a-time is thoroughly recommended, as while the code changes aren't huge, reading them altogether is probably almost nonsensical, and I've put details in each commit where test outputs had to change to (hopefully) justify them.

…, when not in script or style blocks

ie, don't do indentation, just do formattingEmit braces for Html elements that cause indentation, and only record indentation within the right tags
Lucked in to finding this bug with the new test
Previously these didn't format right either, but the Html formatter hid the problem.
Again, previously the Html formatter was hiding this issue by aligning all non-Html at the same column.
Hopefully this isn't contentious, but the fact that we used to indent them always annoyed me anyway. Doing this now would be really annoying, and putting them at column 0 lines up with dotnet/Razor-Language-Design#12 anyway
The VS Html formatter would ignore text outside of Html elements, VS Code wouldn't, and now we're at least consistent.
Some of these look "wrong" at first glance, but actually perfectly match what Roslyn does with C# in the same situation now, which is what we wanted. Some of our test inputs are pathalogically unformatted causing some of this (eg, the .ToString() that moves to column 0) so I also added a test that shows for reasonably formatted input, the output is reasonable.
Previously the indentation came from a mix of Html and C#, now its just up to the Razor parser and how it deals with the bad tags.
This is fixing a bug that we had, and I couldn't fix, where render fragment contents were indented one extra level always. Now that we control everything, it all makes sense and RenderFragments are no longer weird or special (sort of... they're still lambdas)
@davidwengier davidwengier requested a review from a team as a code owner December 24, 2025 21:27
Copy link
Member Author

Choose a reason for hiding this comment

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

It probably goes without saying, but make sure you're viewing this diff with whitespace on :)

@davidwengier
Copy link
Member Author

Superseded by #12623, so I can open PRs targetting this

@davidwengier davidwengier deleted the LetsDoHtmlFormatting branch December 29, 2025 00:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant