-
Notifications
You must be signed in to change notification settings - Fork 232
Handle Html indentation ourselves, rather than using the IDE formatter #12623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs
Outdated
Show resolved
Hide resolved
src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/HtmlFormattingPass.cs
Show resolved
Hide resolved
Some of these got a little worse, but I'd rather fix them in followups
|
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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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
ToddGrun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
|
Looks great, would be nice to have bugs tracking whatever follow up is needed. Awesome job! |
|
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
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.