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

Fix leading comments not being preserved on variable case #74278

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

DannySliekers
Copy link

@DannySliekers DannySliekers commented Jul 6, 2024

Fixes: #74208
FYI:
This does not fix the return case: return builder.ToImmutable() and the method case: Goo(builder.ToImmutable()) and potential other cases i'm not aware of.

@dotnet-issue-labeler dotnet-issue-labeler bot added Area-IDE untriaged Issues and PRs which have not yet been triaged by a lead labels Jul 6, 2024
@dotnet-policy-service dotnet-policy-service bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Jul 6, 2024
@DannySliekers
Copy link
Author

@dotnet-policy-service agree

@DannySliekers DannySliekers marked this pull request as ready for review July 6, 2024 17:52
@DannySliekers DannySliekers requested a review from a team as a code owner July 6, 2024 17:52
@@ -82,6 +83,15 @@ internal partial class CSharpUseCollectionExpressionForBuilderCodeFixProvider()
foreach (var (statement, _) in analysisResult.Matches)
subEditor.RemoveNode(statement);

//Add the leading trivia from the declaration of the builder to the invocation where we convert the builder to a collection.
var dummyObjectCreationDeclaration = dummyObjectCreation.Ancestors().Take(3);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why Take(3)?At least doc what that number means or turn it into a constant. But I suspect, that this logic is a bit too specific to a single line comment case. What if instead of

// Leading
var builder = ImmutableArray.CreateBuilder<Type>();

you have

/* Leading */ var builder = ImmutableArray.CreateBuilder<int>();

Yeah, Cyrus said in the issue that this is not a common patterns, but a) it mught crash that logic if there are less than 3 trivia elements and b) I don't think is is complicated to handle this case. Your trivia in single line comment case looks like this:

    // Comment
    <- white spaces here

What you need to do is remove that white space to preserve correct formatting after fix. I think, there is a WithoutTrailingWhiteSpace() extension method for syntax trivia or similar

//Add the leading trivia from the declaration of the builder to the invocation where we convert the builder to a collection.
var dummyObjectCreationDeclaration = dummyObjectCreation.Ancestors().Take(3);

if (dummyObjectCreationDeclaration.Last() is VariableDeclarationSyntax variableDeclaration)
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (dummyObjectCreationDeclaration.Last() is VariableDeclarationSyntax variableDeclaration)
if (dummyObjectCreationDeclaration.LastOrDefault() is VariableDeclarationSyntax variableDeclaration)

Last() crashes if there are no elements in the sequence. The desired behavior here was to use LastOrDefault()

{
//Leading
{{pattern}}
//Leading
Copy link
Contributor

Choose a reason for hiding this comment

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

Name leading trivias differently for clarity, e.g. // Leading 1 and // Leading 2

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.

IDE0304 should preserve the leading comment
2 participants