Skip to content

Better handling for Razor on type formatting edits around the cursor - #84717

Merged
davidwengier merged 7 commits into
dotnet:mainfrom
davidwengier:contain-razor-formatting-edits
Aug 1, 2026
Merged

Better handling for Razor on type formatting edits around the cursor#84717
davidwengier merged 7 commits into
dotnet:mainfrom
davidwengier:contain-razor-formatting-edits

Conversation

@davidwengier

@davidwengier davidwengier commented Jul 31, 2026

Copy link
Copy Markdown
Member

Fixes dotnet/razor#7962

Stop guessing about which lines to consider, and try to be smart about it.

Microsoft Reviewers: Open in CodeFlow

davidwengier and others added 2 commits July 31, 2026 14:38
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
Copilot AI review requested due to automatic review settings July 31, 2026 05:06
@davidwengier
davidwengier requested a review from a team as a code owner July 31, 2026 05:06
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 CSharpOnTypeFormattingPass by 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')

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What does the triggerCharacter being "null" signify? That there isn't a cursor?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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>
Copilot AI review requested due to automatic review settings August 1, 2026 00:28

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

…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
Copilot AI review requested due to automatic review settings August 1, 2026 01:02
@davidwengier
davidwengier requested a review from a team as a code owner August 1, 2026 01:02
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 1, 2026 01:07
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@davidwengier
davidwengier removed the request for review from a team August 1, 2026 01:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Copilot AI review requested due to automatic review settings August 1, 2026 01:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings August 1, 2026 01:20
@davidwengier

Copy link
Copy Markdown
Member Author

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 🤦‍♂️

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@davidwengier
davidwengier merged commit eaf09d6 into dotnet:main Aug 1, 2026
25 checks passed
@davidwengier
davidwengier deleted the contain-razor-formatting-edits branch August 1, 2026 04:30
@jjonescz jjonescz added this to the 18.11 milestone Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

On-type formatting can move braces after the cursor

4 participants