Handle VS Code newline behaviour#12773
Merged
davidwengier merged 3 commits intodotnet:mainfrom Feb 13, 2026
Merged
Conversation
…ng attributes around in various ways.
chsienki
approved these changes
Feb 12, 2026
| // 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]; |
Member
There was a problem hiding this comment.
Perhaps a dumb question, but what if the formatter adds two blank lines? Or do we just know that will never happen?
Member
Author
There was a problem hiding this comment.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
DifferentAttributeWrappingPoint3test, 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 😛)