Handle Html indentation ourselves, rather than using the IDE formatter#12621
Closed
davidwengier wants to merge 19 commits intodotnet:mainfrom
Closed
Handle Html indentation ourselves, rather than using the IDE formatter#12621davidwengier wants to merge 19 commits intodotnet:mainfrom
davidwengier wants to merge 19 commits intodotnet:mainfrom
Conversation
…, 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
commented
Dec 24, 2025
Member
Author
There was a problem hiding this comment.
It probably goes without saying, but make sure you're viewing this diff with whitespace on :)
Without the Html formatter, the new output looks better IMO
Member
Author
|
Superseded by #12623, so I can open PRs targetting this |
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 #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.