google-java-format icon indicating copy to clipboard operation
google-java-format copied to clipboard

Leave annotations on their own line for properties.

Open charlesoconor opened this issue 2 years ago • 8 comments

Code written

@Resource 
private OrgInfoRepository orgInfoRepository;

is changed to

@Resource private OrgInfoRepository orgInfoRepository;

My assumption is that this happens since the line isn't long enough to be split but it would be easier to read if we kept the spring annotations on a separate line.

I'm using version1.11.0

charlesoconor avatar Aug 24 '21 17:08 charlesoconor

This is a case where the formatter is being stricter than the documented style rules. Essentially changing may to must in this exception:

Exception: A single parameterless annotation may instead appear together with the first line of the signature, for example:

Given that the style guide documents this as an exception to the rule, I would agree with @charlesoconor that if there is going to be one and only one format supported by this project it should be the primary rule that annotations appear on their own line.

dvogel avatar Oct 07 '21 18:10 dvogel

+1, so how to fix this?

Alice52 avatar Dec 20 '21 01:12 Alice52

+1, VERY annoying!

@SomeTag
private IsGood shouldNotChangeIt;  // Good and clearly.

@SomeTag private IsGood2 alsoAcceptable; // Just acceptable.

kenchou avatar Dec 28 '21 06:12 kenchou

+1

and how t o change it?

yuzd avatar Jan 14 '22 02:01 yuzd

@cushon or someone on the team I can take a crack at this but would like a rough code pointer or know if it's impossible.

charlesoconor avatar Feb 17 '22 08:02 charlesoconor

Sigh -- I'm sorry to say that the annotation-on-same-line policies were the most bitterly contended negotations and we're too scarred to go back into it.

At the bare minimum, we would need an absolutely textual heuristic for when to do it and when not to, and I don't think that's possible.

kevinb9n avatar Mar 09 '22 16:03 kevinb9n

At the bare minimum, we would need an absolutely textual heuristic for when to do it and when not to, and I don't think that's possible.

In that case, would it be better go with a more lenient approach, given that it does enhance readability at the very least?

The style guide says in 4.8.5.4 Field annotations:

in this case, multiple annotations (possibly parameterized) may be listed on the same line

Should this not be interpreted as "we make an exception to allow multiple annotations on the same line if the developer so wishes"? IMO this implies the plugin should find it acceptable to have field annotations both on separate lines and on the same line.

aakash-fk avatar Mar 18 '22 06:03 aakash-fk

I think reading questions 3-5 here: https://github.com/google/google-java-format/wiki/FAQ would help frame the discussion a little better here, if you can.

kevinb9n avatar Mar 18 '22 12:03 kevinb9n

Because:

  • I don't see an argument that this issue meets the three bullet-point criteria listed at that link
  • We made the decision for how to format these cases as diligently as anyone could
  • And no new information has emerged since then about why the other way is objectively better

... It means the formatter is working as intended. Remember that every single style choice the formatter makes is disliked by some fraction of users; I have my share of things I don't personally like too.

kevinb9n avatar Jan 26 '23 00:01 kevinb9n