roslyn icon indicating copy to clipboard operation
roslyn copied to clipboard

Fix line breaks between members when a property has an initializer

Open DoctorKrolic opened this issue 2 years ago • 5 comments

Fixes: https://github.com/dotnet/roslyn/issues/42336

The issue is quite old, so the actual problem changed a bit. Before this PR the output for property with initializer was

class A
{
  public string Prop { get; } = "xyz";
  public void Foo()
  {
  }
}

This is not expected, we want to have an empty line between members.

In this PR I added this missing line break, so the behaviour is the same as in the case without property initializer. Added test cases for both. Put them in the same place, where all other declaration tests currently are

DoctorKrolic avatar Dec 10 '22 19:12 DoctorKrolic

Fixes: https://github.com/dotnet/roslyn/issues/42336

Per your description and issue description, the behavior described in the issue isn't the current behavior. Can you confirm?

So basically the complain in #42336 is already fixed, but this PR wants to handle another scenario.

Youssef1313 avatar Dec 10 '22 20:12 Youssef1313

the behavior described in the issue isn't the current behavior. Can you confirm?

Yes

So basically the complain in https://github.com/dotnet/roslyn/issues/42336 is already fixed, but this PR wants to handle another scenario.

I mean, I took code example from the issue, and it is still problematic, but in a bit other way. Nevertheless, even if to treat this case as a separate one, the issue should be closed as completed anyway)

DoctorKrolic avatar Dec 10 '22 20:12 DoctorKrolic

I mean, I took code example from the issue, and it is still problematic,

I just wanted to confirm I understand the situation correctly.

Youssef1313 avatar Dec 10 '22 20:12 Youssef1313

Can I get a review here please?

DoctorKrolic avatar Dec 13 '22 20:12 DoctorKrolic

This is not expected, we want to have an empty line between members.

it's unclear to me that we want this. this feels like this coudl change a lot of code that currently normalizes.

CyrusNajmabadi avatar Dec 14 '22 02:12 CyrusNajmabadi

@CyrusNajmabadi Added a bunch of tests to better represent the change. Prior to this PR all test cases with property initializers would be just crumpled together, but now they behave the same as those without initializers and emit a nice additional line break between themselves and the next member

DoctorKrolic avatar Jan 04 '23 19:01 DoctorKrolic

@jcouv I would also suggest to split declaration test into multiple once (1 test for usings, 1 test for methods, 1 test for properties etc.). Current single test for all declarations is getting really enormous and hard to navigate in. But this smells for me like a "style-only" change, so asking you the permission to do it (probably in a separate PR after this one is done)

DoctorKrolic avatar Jan 04 '23 19:01 DoctorKrolic

Current single test for all declarations is getting really enormous and hard to navigate in

Agreed. Adding method/test boundaries won't cause that much churn, so should be fine especially if some grouping remains (property initializers, indexers, ...).

jcouv avatar Jan 04 '23 19:01 jcouv

Can I get a review and failed CI restart here pls?

DoctorKrolic avatar Jan 06 '23 20:01 DoctorKrolic

@CyrusNajmabadi did you still have concerns about the baseline changes here?

333fred avatar Jan 06 '23 21:01 333fred

@jcouv @CyrusNajmabadi @333fred PTAL. Added small tweak to group properties and added tests to verify both properties and fields

DoctorKrolic avatar Jan 13 '23 13:01 DoctorKrolic

@Youssef1313 Well, this is what the CI is for)

DoctorKrolic avatar Jan 13 '23 13:01 DoctorKrolic

@Youssef1313 Well, this is what the CI is for)

I'm referring to external source generators that are in different codebases.

Generally, any change to syntax normalizer can affect them, but thinking this one is quite common.

Youssef1313 avatar Jan 13 '23 13:01 Youssef1313

any change to syntax normalizer can affect them

Well, the syntax normalizer output isn't a public API, so it may change between even minor versions, right? It isn't a big deal to add and remove several line breaks from your test in the end

DoctorKrolic avatar Jan 13 '23 13:01 DoctorKrolic

We generally don't treat syntax normalizer's output as a public API. This may be a common change, but a generator author isn't going to observe it until they update their roslyn dependency version in their own tests, and it shouldn't have meaningful impact at generator runtime.

333fred avatar Jan 13 '23 18:01 333fred

What impact does this PR have on non-auto properties? Will they be grouped in the same way? Consider adding some tests with expression-bodied and full-bodied properties.

333fred avatar Jan 13 '23 18:01 333fred

Is this PR forgotten? @jcouv Can you please take a look, since you've already signed this off before. Also probably want a look from @CyrusNajmabadi to verify that all behaviour expectations match

DoctorKrolic avatar Jan 27 '23 19:01 DoctorKrolic

@DoctorKrolic Indeed, that had fallen off my radar (we have many open PRs at the moment). Thanks for the ping. Looks good, thanks!

jcouv avatar Jan 27 '23 19:01 jcouv

/azp run

jcouv avatar Jan 27 '23 19:01 jcouv

Azure Pipelines successfully started running 2 pipeline(s).

azure-pipelines[bot] avatar Jan 27 '23 19:01 azure-pipelines[bot]

@DoctorKrolic We've had some CI issues. Could you merge latest bits from the main branch into this PR? (I do git fetch origin main then git merge origin/main then push the commit here)

jcouv avatar Jan 27 '23 20:01 jcouv

I preffer to use GitHub UI since I am already in the browser and it doesn't disable auto-merge

DoctorKrolic avatar Jan 27 '23 20:01 DoctorKrolic

Thanks for the contribution @DoctorKrolic!

jcouv avatar Jan 28 '23 01:01 jcouv