Skip to content

Handle VS Code newline behaviour#12773

Merged
davidwengier merged 3 commits intodotnet:mainfrom
davidwengier:FixVSCodeFormattingQuirks
Feb 13, 2026
Merged

Handle VS Code newline behaviour#12773
davidwengier merged 3 commits intodotnet:mainfrom
davidwengier:FixVSCodeFormattingQuirks

Conversation

@davidwengier
Copy link
Copy Markdown
Member

Fixes microsoft/vscode-dotnettools#2766 and I think some others that I can't find right now.

VS Code does two things we didn't handle well, inserting blank lines between some elements, and generally moving newlines around in long Html tags. Unfortunately we didn't have great test coverage for this, since we had almost zero coverage for VS Code formatting behaviour outside of the FormattingLogTests, but fortunately we now can trivially add some, so thats what I did.

The first commit handles when new blank lines are inserted.
The second commit handles when new lines are inserted, or removed, in the middle of other lines. We had the basics of support for this, but turns out the logic was backwards and only worked for the simple wrapping that Roslyn does (since thats what the code was written for). It used to loop consuming lines, and inside the loop it looked for whether it should consume formatted or original lines. Now the logic is flipped, and we work out which to consume, then loop. Annoyingly we also have to introduce another loop, as a look-ahead. This is best seen in the DifferentAttributeWrappingPoint3 test, where 6 lines are compressed to 2 by the formatter, so we have to keep looking through the original text to find the end.
The third commit refactors the code from the second, because to say it got unwieldy is a bit of an understatement. I see now why regexes are popular (but I still don't want to write one 😛)

@davidwengier davidwengier requested a review from a team as a code owner February 12, 2026 22:38
// Next line is blank, and next original line isn't. Now we check the line after next
if (iFormatted + 2 < formattedText.Lines.Count)
{
var lineAfterNext = formattedText.Lines[iFormatted + 2];
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.

Perhaps a dumb question, but what if the formatter adds two blank lines? Or do we just know that will never happen?

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.

Not a dumb question, but I didn't want to make everything into a loop that does a look-ahead just in case, I'd rather wait for evidence that it happens in the real world.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants