Skip to content

Switch formatting engine over to using TextChange instead of TextEdit#10855

Merged
davidwengier merged 16 commits intodotnet:mainfrom
davidwengier:FormattingTextChange
Sep 10, 2024
Merged

Switch formatting engine over to using TextChange instead of TextEdit#10855
davidwengier merged 16 commits intodotnet:mainfrom
davidwengier:FormattingTextChange

Conversation

@davidwengier
Copy link
Member

@davidwengier davidwengier commented Sep 9, 2024

Fixes #10842

The formatting self-nerd-sniping continues.

The formatting engine was written to use the LSP TextEdit class, which makes some sense, but also uses Roslyn APIs like SourceText a lot, which uses the TextChange struct instead. This meant lots of code to convert to and from the two types. Changing the whole formatting engine over to TextChange, and using more TextSpan, LinePositionSpan etc. removes a lot of this code. It also makes a lot more sense in cohosting, to boot.

I wouldn't claim that I've gone through and improved the perf of the formatting engine, but rather I've use the changes to lead me to things that need fixing. ie, I started out moving from TextEdit[] to ImmutableArray<TextChange>, and this let me to places where pooled array builders could be used, and places where Range and Position were used which didn't make much sense, and then the constructor for LinePosition threw at one point because it turns out we were only using the Line property from the Position that used to be used, and so never validated the characters, so that API moved to int, etc.

TL;DR the commits tell the story, and there could well be something I missed, if it never came across my plate for another reason.

This commit wasn't purely mechanical, but it was close. Just making things mostly compile, no optimizations or anything yet, and probably still a bunch more renames to come.
Haven't run the tests yet :)
All tests pass!
Most places already passed an ImmutableArray here. I left the method itself being an iterator, as 50% of callers need an ImmutableArray result, but the other 50% need an array, so no clear winner.
All callers did the conversion anyway
@davidwengier davidwengier requested a review from a team as a code owner September 9, 2024 04:29
@davidwengier davidwengier merged commit 2511efe into dotnet:main Sep 10, 2024
@davidwengier davidwengier deleted the FormattingTextChange branch September 10, 2024 05:08
@dotnet-policy-service dotnet-policy-service bot added this to the Next milestone Sep 10, 2024
@phil-allen-msft phil-allen-msft modified the milestones: Next, 17.12 P3 Oct 31, 2024
davidwengier added a commit that referenced this pull request Feb 3, 2025
Fixes #11117

Looks like I broke this in #10855
We were accidentally formatting everything from line 0 to the actual
line we cared about, in on type formatting.
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.

Convert formatting engine to TextChange and ImmutableArray

3 participants