Skip to content

Fix formatting of mixed indentation in VS Code#12418

Merged
davidwengier merged 9 commits intodotnet:mainfrom
davidwengier:HtmlFormattingMixedTabs
Oct 31, 2025
Merged

Fix formatting of mixed indentation in VS Code#12418
davidwengier merged 9 commits intodotnet:mainfrom
davidwengier:HtmlFormattingMixedTabs

Conversation

@davidwengier
Copy link
Member

Fixes #12416, reported by @Peter-Juhasz on Discord

When a file has mixed indentation, VS Code sends across edits that include changes to content, presumably for brevity, but the Html formatter doesn't know about C#, and so changing content breaks it. To fix it, we run the edits through our source text differ, doing a diff at the word level so movements of C# (which Html sees as either tildes or a comment) end up being identical, and therefore the edits can be safely discarded.

@davidwengier davidwengier requested a review from a team as a code owner October 30, 2025 12:02

var length = oldWord.Length;

// Copy the text into char arrays for comparison. Note: To avoid allocation,
Copy link
Contributor

Choose a reason for hiding this comment

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

// Copy the text into char arrays for comparison. Note: To avoid allocation,

Feels like from here down could be moved into the base class and reused by the line differ.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, this and LineDiffer are very similar other than the creation of the arrays, and the types within, but CharDiffer is quite different. Will move as much down as reasonable.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, this is a bit annoying, and ends up having stuff in the base class that makes no sense to be there. What would be nicer is if both Word and Line differs were both TextSpanDiffers, with different create methods, but I'll do that in a separate PR.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, I take that back, creating TextSpanDiffer is actually pretty simple.


if (changes.Length > 0)
{
// There is a lot of uncertainty when we're dealing with edits that come from the Html formatter
Copy link
Contributor

Choose a reason for hiding this comment

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

// There is a lot of uncertainty when we're dealing with edits that come from the Html formatter

This comment is wonderful!

// or slashes, so we can keep it simple and just capture a "word" when the classification of
// the current character changes.
var index = 1;
while (index < text.Length)
Copy link
Contributor

Choose a reason for hiding this comment

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

while (index < text.Length)

idontcarenit: looks like a for loop

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:

@davidwengier davidwengier merged commit fd9e1dc into dotnet:main Oct 31, 2025
11 checks passed
@davidwengier davidwengier deleted the HtmlFormattingMixedTabs branch October 31, 2025 02:57
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Oct 31, 2025
@davidwengier davidwengier modified the milestones: Next, 18.3 Jan 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mixed indentation in VS Code causes issues

2 participants