Skip to content

Conversation

@davidwengier
Copy link
Member

@davidwengier davidwengier commented Dec 28, 2025

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

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.

…, 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
@davidwengier
Copy link
Member Author

Had to update a few test baselines due to fixing the off-by-one error in HtmlFormattingPass, and more annoyingly, seems a couple of tests now fail in CI, both debug and release, but pass on my laptop 🤦‍♂️

Will have to investigate

#else
_builder.AppendLine($"//");
#endif
return CreateLineInfo(htmlIndentLevel: htmlIndentLevel, additionalIndentation: additionalIndentation);
Copy link
Contributor

Choose a reason for hiding this comment

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

return CreateLineInfo(htmlIndentLevel: htmlIndentLevel, additionalIndentation: additionalIndentation);

Does the appending of just "//" in release bits enable some behavior?

Copy link
Member Author

Choose a reason for hiding this comment

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

Emitting a comment means Roslyn will indent the line of code as appropriate. Having extra content in the comment is helpful for debugging, but figured I'd save a little bit of memory in release by not including anything. It doesn't seem to affect the formatting behaviour

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:

@ToddGrun
Copy link
Contributor

ToddGrun commented Jan 6, 2026

Looks great, would be nice to have bugs tracking whatever follow up is needed. Awesome job!

@davidwengier
Copy link
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 FormatNestedComponents5 is a much better formatting result now.

@davidwengier davidwengier enabled auto-merge January 7, 2026 05:59
@davidwengier davidwengier disabled auto-merge January 7, 2026 06:30
#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
Fixes #6551
Fixes #8413

Adding an option is _way_ too annoying! We'll have to fix that up when
we get rid of the language server.
@davidwengier davidwengier enabled auto-merge January 7, 2026 06:43
@davidwengier davidwengier merged commit d9e43a6 into main Jan 7, 2026
10 checks passed
@davidwengier davidwengier deleted the dev/dawengie/LetsDoHtmlFormatting branch January 7, 2026 08:52
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment