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

Allow ignore spaces around declaration to work when preserve single line statements is set to false #34959

Merged
merged 3 commits into from
May 2, 2019

Conversation

chborl
Copy link
Contributor

@chborl chborl commented Apr 12, 2019

Fixes #31868

Who is impacted by this bug?
Users who use Format Document and have these two settings in the .editorconfig
csharp_space_around_declaration_statements = ignore
csharp_preserve_single_line_statements = false

What is the customer scenario and impact of the bug?
If a user has these two settings in their .editorconfig:
csharp_space_around_declaration_statements = ignore
csharp_preserve_single_line_statements = false

When they Format Document the extra spaces around their declaration statements are removed when they should have been ignored.

Expected
Original spacing around the declaration statements should be left alone since user specified
csharp_space_around_declaration_statements = ignore.

Example,

    public void FixMyType()
    {
        var     myint       = 0;
        var     myint2      = 1;
    }

Actual
The original spacing is not maintained. It becomes,

    public void FixMyType()
    {
        var myint = 0;
        var myint2 = 1;
    }

What is the workaround?
After using Format Document, manually redo spacing around declaration statements.

How was the bug found?
Developer Community issue

If this fix is for a regression - what had regressed, when was the regression introduced, and why was the regression originally missed?
Not a regression. Repros in 15.9.11

@chborl chborl requested a review from a team as a code owner April 12, 2019 18:41
@chborl chborl added Area-IDE PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. labels Apr 12, 2019
@chborl chborl changed the title allow ignore spaces around declaration to work when preserve single line statements is set to false Allow ignore spaces around declaration to work when preserve single line statements is set to false Apr 12, 2019
@@ -166,7 +166,7 @@ private void RemoveSuppressOperationForBlock(List<SuppressOperation> list, Synta

for (int i = 0; i < list.Count; i++)
{
if (list[i] != null && list[i].TextSpan.Start >= span.Start && list[i].TextSpan.End <= span.End)
if (list[i] != null && list[i].TextSpan.Start >= span.Start && list[i].TextSpan.End <= span.End && list[i].Option == SuppressOption.NoWrappingIfOnSingleLine)
Copy link
Member

Choose a reason for hiding this comment

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

this is a pretty core method. Can you add comments explaining why this behavior is correct?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@CyrusNajmabadi ,

When setting these options:

changingOptions.Add(CSharpFormattingOptions.SpacesIgnoreAroundVariableDeclaration, true);
changingOptions.Add(CSharpFormattingOptions.WrappingKeepStatementsOnSingleLine, false);

and running this code:

class Program
{
    public void FixMyType()
    {
        var    myint    =    0;
    }
}

This line of code is removing all three of these entries:
image

If I leave the NoSpacing entry in the list, it keeps the spacing intact as desired.

There might be another place that is more appropriate to fix it. Any ideas where?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fix could also be this, if (list[i] != null && list[i].TextSpan.Start >= span.Start && list[i].TextSpan.End <= span.End && list[i].Option != SuppressOption.NoSpacing). I'm not familiar enough with this code to know if other features are affected by the same setting.

Copy link
Member

Choose a reason for hiding this comment

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

I would definitely ask @heejaechang about this. I think he's the only one qualified to state the best place for this fix.

Copy link
Contributor

Choose a reason for hiding this comment

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

I am not sure why code is written this way, but it looks like we are removing suppression operation based on option rather than add suppression operation based on option.

because it is blindly add all suppression rule first and then later try to remove those suppression added, we have this strange code flow.

I think we should have an issue opened saying we should do thing in opposite way where we add suppression rules based on option rather than remove rule based on options

Copy link
Contributor

Choose a reason for hiding this comment

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

that being said, in current form, what @chborl did is right.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we should have an issue opened saying we should do thing in opposite way where we add suppression rules based on option rather than remove rule based on options

I filed this follow-up issue: #35377

@chborl chborl removed the PR For Personal Review Only The PR doesn’t require anyone other than the developer to review it. label Apr 19, 2019
@chborl chborl force-pushed the wrappingformatissue branch from 2a1ae6d to 5039423 Compare April 30, 2019 22:55
@dpoeschl dpoeschl changed the base branch from dev16.1 to release/dev16.1 May 1, 2019 20:05
Copy link
Contributor

@dpoeschl dpoeschl left a comment

Choose a reason for hiding this comment

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

Thanks for the explanations.

@jinujoseph jinujoseph closed this May 2, 2019
@jinujoseph jinujoseph reopened this May 2, 2019
@jinujoseph
Copy link
Contributor

@ManishJayaswal for approval

@jinujoseph jinujoseph added this to the 16.1 milestone May 2, 2019
@chborl chborl merged commit 76e66c4 into dotnet:release/dev16.1 May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants