Skip to content

Fix formatting of attributes on lambdas#56639

Merged
jcouv merged 7 commits intodotnet:mainfrom
jcouv:format-lambdas
Oct 5, 2021
Merged

Fix formatting of attributes on lambdas#56639
jcouv merged 7 commits intodotnet:mainfrom
jcouv:format-lambdas

Conversation

@jcouv
Copy link
Copy Markdown
Member

@jcouv jcouv commented Sep 23, 2021

Fixes #56543

Relates to test plan #52192

@jcouv jcouv added the Area-IDE label Sep 23, 2021
@jcouv jcouv self-assigned this Sep 23, 2021
@jcouv jcouv requested a review from sharwell September 23, 2021 15:32
@jcouv jcouv marked this pull request as ready for review September 23, 2021 15:32
@jcouv jcouv requested a review from a team as a code owner September 23, 2021 15:32

// * [
if (currentToken.IsKind(SyntaxKind.OpenBracketToken) &&
!currentToken.Parent.IsKind(SyntaxKind.AttributeList) &&
Copy link
Copy Markdown
Contributor

@CyrusNajmabadi CyrusNajmabadi Sep 23, 2021

Choose a reason for hiding this comment

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

what happens if i have[Foo] [Bar] now? how did it format before, how does it format now? #Resolved

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.

Good question. Before and after behavior is that we leave whatever spaces you put there.
Should we make that 1 space?

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.

I went with 0 space as at least one existing test had that expectation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes. 0 spaces would be my expectation.

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Sep 27, 2021

@CyrusNajmabadi @sharwell for another look. Thanks

@jcouv jcouv changed the base branch from main to release/dev17.0 September 28, 2021 00:12
@jcouv jcouv added this to the 17.0 milestone Sep 28, 2021
@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Sep 28, 2021

@CyrusNajmabadi for another look. Thanks

Comment thread src/Workspaces/CSharpTest/Formatting/FormattingTests.cs

// [Attribute1]$$ [Attribute2]
if (currentToken.IsKind(SyntaxKind.OpenBracketToken) &&
currentToken.Parent.IsKind(SyntaxKind.AttributeList))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

this case seems weird given the if check above this. we already checked if the owner was a member, and if so we force a space. it seems like we'd want to do the multiple-attribute check prior to doing the member check, otherwise we would not hit this for [Foo] [Bar] void SomeMember(), right?

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.

For methods, we introduce newlines between attributes.
But for fields, the order here matters. If I swap the order, then FormatMultipleAttributeOnSameLineAsField1 is affected (it expects [Attr3] [Attr4] int i; with 1 space.
So I think we should keep the order as-is.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

can you comment teh code explicitly to indicate this is intentional. note if we want fields to keep thigns on a single line, i'd far prefer that we do that explicitly. Note: for fields, i would expect no space between attributes as well :(

tagging @sharwell for thoughts here.

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Sep 30, 2021

@CyrusNajmabadi @sharwell I modified existing behavior so we no longer have a space between attributes per suggestion above.

{
await AssertFormatAsync(
expected: @"
[Attribute][Attribute2]
Copy link
Copy Markdown
Contributor

@sharwell sharwell Oct 1, 2021

Choose a reason for hiding this comment

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

❔ What did this do before your change? Just wondering if the output used to split them to their own lines (like SpacingAfterAttribute_MultipleOnDeclaration shows). #Resolved

Copy link
Copy Markdown
Member Author

@jcouv jcouv Oct 1, 2021

Choose a reason for hiding this comment

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

Prior to this PR, this would leave the formatting as-is (ie. [Attribute] [Attribute2] with 2 spaces)

Four tests were affected by this file by the product code change:
image

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Oct 1, 2021

From discussion with Jinu, I'm pushing the bug and the fix from 17.0 to 17.1, so we don't have to rush to resolve this today. Thanks

@jcouv jcouv modified the milestones: 17.0, 17.1 Oct 1, 2021
@jcouv jcouv changed the base branch from release/dev17.0 to main October 1, 2021 18:05
@sharwell
Copy link
Copy Markdown
Contributor

sharwell commented Oct 1, 2021

This sounds good to me:

  1. Cases where attribute lists were placed on separate lines by the formatter do not change behavior
  2. There are no changes which cause the formatter to remove the line ending between two attribute lists originally on separate lines (i.e. merge them to be on one line)
  3. Cases where consecutive attributes lists are placed on the same line now consistently remove the space between the attribute lists

As part of (3), I would prefer to see the formatter apply to attributes on generic type parameters (previously this case was ignored by the formatter), which gives consistency to parameters and type parameters.

@jcouv
Copy link
Copy Markdown
Member Author

jcouv commented Oct 4, 2021

@CyrusNajmabadi for another look. Thanks
I've added tests for type parameters.

@jcouv jcouv merged commit 68d8156 into dotnet:main Oct 5, 2021
@jcouv jcouv deleted the format-lambdas branch October 5, 2021 16:01
@ghost ghost modified the milestones: 17.1, Next Oct 5, 2021
@RikkiGibson RikkiGibson modified the milestones: Next, 17.1.P1 Oct 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Lambda expressions with attributes are formatted incorrectly

4 participants