facebook / ktfmt

A program that reformats Kotlin source code to comply with the common community standard for Kotlin code conventions.
https://facebook.github.io/ktfmt/
Apache License 2.0
903 stars 74 forks source link

Formatting of field annotations is inconsistent with GJF #316

Open xeals opened 2 years ago

xeals commented 2 years ago

Apologies if this is not an explicit goal, but I'm unable to find anything addressing this or not.

The Google Java Style guidelines have no preference regarding if annotations with parameters are formatted on a single or multiple lines, but Google Java Format makes the decision that if any annotation has parameters, it is formatted across multiple lines:

@Autowired @NotNull private Foo foo;
// is unchanged

@Autowired(required = false) @Nullable private Bar bar;
// becomes
@Autowired(required = false)
@Nullable
private Bar bar;

ktfmt prefers to always format annotations on a single line where they fit:

// neither are changed
@Autowired @NotNull private lateinit var foo: Foo
@Autowired(required = false) @Nullable private lateinit var bar: Bar

In mixed-language projects, this leads to inconsistency issues, particularly around declarations that often use multiple annotations with arguments (JPA, lint/warning suppressions, serialization, etc).

cgrushko commented 2 years ago

I think we went for one line annotations because of some idiosyncrasies in our code base. @strulovich did I remember correctly?

(Meaning, doing one per line causes some of our files to become super long)

strulovich commented 2 years ago

I vaguely remember why, but we used to follow the JetBrains convention of annotations with arguments on their own line. It created a bunch of awkward constructs with some short named annotations breaking lines into many lines (Your example for bar seems to be of that style).

I'm open to looking at this again, if there's a PR I can run it on the codebase and see if it comes up awkward or not. Otherwise it may take some time until I make this PR and test it.