Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove at most 1 end of line preceding redundant nullable directives #72150

Merged

Conversation

ghost
Copy link

@ghost ghost commented Feb 16, 2024

Closes #63163

This pr changes the way the redudant directive is removed by checking the trivias preceding them. All of the whitespaces ones are removed until an end of line trivia is found. This means at most, this will delete a blank like preceding the directive which removes a potential blank line.

I added a test, but I needed to change 2 tests to fit the changes. The new algorithm first groups the nodes by their directive nodes which allows to go through each of their parent's childs and perform their removal. This is done on a per node basis so cases where there's multiple directives in a row are batched together.

I am not too sure about the complexity of this and it might be because there's some node apis I wasn't aware of that would have made this simpler, but I at least wanted to present a working solution in case it could be simplified.

@ghost ghost self-requested a review as a code owner February 16, 2024 18:45
@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Feb 16, 2024
@ghost ghost added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Feb 16, 2024
@ghost
Copy link
Author

ghost commented Feb 20, 2024

Hey, this is still ready for review, I am not sure why the vs integration tests are failing, seems to be a timeout?

var nullableDirectivesByNodes = diagnostics.GroupBy(x =>
x.Location.FindNode(findInsideTrivia: false, getInnermostNodeForTie: false, cancellationToken), x =>
x.Location.FindNode(findInsideTrivia: true, getInnermostNodeForTie: true, cancellationToken))
.ToDictionary(x => x.Key, x => x.ToList());
Copy link
Member

Choose a reason for hiding this comment

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

what is thsi code for. this needs docs.

Copy link
Author

Choose a reason for hiding this comment

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

Added comments.

var leadingTrivia = node.GetLeadingTrivia();
var indexes = nullableDirectives.Select(x => leadingTrivia.IndexOf(x.ParentTrivia))
.OrderByDescending(x => x).ToArray();
foreach (var index in indexes)
Copy link
Member

Choose a reason for hiding this comment

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

i really don't understand the logic here. including what the above indices are representing, or waht the loop below is doing. this all needs examples.

Copy link
Author

Choose a reason for hiding this comment

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

Added comments, does it help?

Co-authored-by: Sam Harwell <sam@tunnelvisionlabs.com>
@CyrusNajmabadi
Copy link
Member

@sdelarosbil do you mind if i push to your repo? if not, i can push to another branch where i show what i mean :)

@ghost
Copy link
Author

ghost commented Feb 22, 2024

@sdelarosbil do you mind if i push to your repo? if not, i can push to another branch where i show what i mean :)

you can push to this branch, sure

I understand the idea, it's more I think I might miss how to easilly get the trivias relative to the directives that I have a bit of an issue.

@CyrusNajmabadi
Copy link
Member

Ok. Pushed :)

@ghost
Copy link
Author

ghost commented Feb 22, 2024

Oh, I see, yeah now I understand more how you do this now.

Thanks!

@CyrusNajmabadi CyrusNajmabadi merged commit 82d1edc into dotnet:main Feb 22, 2024
27 checks passed
sharwell added a commit to sharwell/roslyn that referenced this pull request Feb 23, 2024
This change updates the behavior of AddElasticMarker during syntax node
removal to collapse consecutive newlines surrounding the removed node
such that the total number of blank lines appearing at the location of
the removed node does not increase.

This is a dramatic simplification based on dotnet#72150.
@jjonescz jjonescz added this to the 17.10 P2 milestone Feb 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-IDE Community The pull request was submitted by a contributor who is not a Microsoft employee. untriaged Issues and PRs which have not yet been triaged by a lead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

IDE0240 codefix leaves redundunt blank line
4 participants