Handle Html indentation ourselves, rather than using the IDE formatter#12623
Merged
davidwengier merged 46 commits intomainfrom Jan 7, 2026
Merged
Handle Html indentation ourselves, rather than using the IDE formatter#12623davidwengier merged 46 commits intomainfrom
davidwengier merged 46 commits intomainfrom
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)
Without the Html formatter, the new output looks better IMO
…es added by Roslyn
ToddGrun
reviewed
Jan 5, 2026
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs
Outdated
Show resolved
Hide resolved
ToddGrun
reviewed
Jan 5, 2026
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs
Show resolved
Hide resolved
Member
Author
|
Okay, even weirder, those same tests that failed in CI but passed on my laptop, fail on my desktop PC. Must have been doing something wrong on the laptop. All good though, in particular |
…xImplicitExpressionFormatting
…Formatting_NetFx/DocumentFormattingTest.cs
#12624) Fixes #12622 plus a few others from #12554 Fixes #12631 We already did most of this, just for explicit expressions (ie `@( ... )`) so this brings it to implicit expressions too (ie `@ ...`) when they're multiline. We also used to emit a `/* */` at the end of the first line, in case there was some non-C# content on the line, and now we check for it so that we can handle Roslyn moving an open brace, or anything else really.
…ing' into dev/dawengie/HtmlAttributeIndentOption # Conflicts: # src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpFormattingPass.CSharpDocumentGenerator.cs
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
Fixes #8228
Constituent PRs also fix:
Fixes #12622 plus a few others from #12554
Fixes #12631
Fixes #12635
Fixes #6551
Fixes #8413
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 - except the last 4 commits which were just "oh crap tests fail in CI" fixes.