Better handling for Razor on type formatting edits around the cursor - #84717
Conversation
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: bb636ed3-32f5-4b45-9536-80e7460ee18c
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: bb636ed3-32f5-4b45-9536-80e7460ee18c
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
Pull request overview
This PR adjusts Razor’s C# on-type formatting pass to more precisely choose the line range for indentation fixes, aiming to avoid formatting lines beyond the user’s cursor (while still including the typed-character line when it immediately follows the last C# edit). It also adds a regression test covering a case where a property declaration after the caret was previously being (incorrectly) re-indented.
Changes:
- Refines indentation-range selection in
CSharpOnTypeFormattingPassby tracking the caret position into the cleaned text and conditionally including only the adjacent typed-character line. - Removes the prior unconditional “look one extra line ahead” heuristic when a trigger character is present.
- Adds a new unit test asserting a property line after the caret is not indented as part of a nearby close-curly on-type format.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/Cohost/Formatting/OnTypeFormattingTest.cs | Adds a regression test for avoiding indentation changes to code after the caret when typing }. |
| src/Razor/src/Razor/src/Microsoft.CodeAnalysis.Razor.Workspaces/Formatting/Passes/CSharpOnTypeFormattingPass.cs | Updates the indentation range computation to use caret tracking and only include the adjacent typed-character line. |
| var startLine = Math.Min(firstLine, linePositionSpanAfterFormatting.Start.Line); | ||
| var endLineInclusive = Math.Max(lastLine, linePositionSpanAfterFormatting.End.Line + lineDelta); | ||
|
|
||
| if (context.TriggerCharacter != '\0') |
There was a problem hiding this comment.
What does the triggerCharacter being "null" signify? That there isn't a cursor?
There was a problem hiding this comment.
Yeah, this formatting pipeline is used for code actions and snippets too; anything where we have edits to the generated C# we need to format, so those don't have trigger characters or cursor positions.
…s/Formatting/Passes/CSharpOnTypeFormattingPass.cs Co-authored-by: Chris Sienkiewicz <chsienki@microsoft.com>
…tting-edits Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> # Conflicts: # src/Razor/src/Razor/test/Microsoft.VisualStudio.LanguageServices.Razor.UnitTests/Cohost/Formatting/OnTypeFormattingTest.cs
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Well copilot did a terrible job of resolving merge conflicts. Sorry for the unnecessary pings. @chsienki needs re-approval after accepting your feedback and resolving the merge conflict, which then needed changes to not break the other tests 🤦♂️ |
Fixes dotnet/razor#7962
Stop guessing about which lines to consider, and try to be smart about it.
Microsoft Reviewers: Open in CodeFlow