ktfmt icon indicating copy to clipboard operation
ktfmt copied to clipboard

Annotations get collapsed to the same line as code it affects

Open ukarlsson opened this issue 1 year ago • 5 comments

We are using ktfmt with the following gradle config

ktfmt {
    kotlinLangStyle()
}

The issue that annotations get formatted on the same line, e.g. we get lines like

        @Serializable @SerialName("native:live") object NativeLive : SpecificSettings()

        @Serializable @SerialName("native:self-paced") object NativeSelfPaced : SpecificSettings()

The Kotlinx documentation all have the annotations on different lines and I noticed that in ktfmt you also have annotations on different lines.

We would very much like ktfmt to format this like

        @Serializable
        @SerialName("native:live")
        object NativeLive : SpecificSettings()

        @Serializable
        @SerialName("native:self-paced")
        object NativeSelfPaced : SpecificSettings()

Using ktfmt version 0.43.0

Many thanks.

ukarlsson avatar Mar 27 '23 15:03 ukarlsson

@strulovich , I remember you have thoughts on this?

cgrushko avatar Mar 28 '23 08:03 cgrushko

If I recall correctly, the main reason we are at this state is since similar multiline formatting for annotated parameters look really bad.

  • We went back and forth whether to handle annotations with params differently (as the Kotlin guidelines suggests). In the end I think we do not, since it created many awkward instances.
  • Breaking annotations can make a list of annotated items look very awkward if the items are short. (It take a nicely formatted 5 lines turning them to 20 ones).
  • We can consider treating formatting differently according to the annotated target (i.e. treat properties differently than top level objects), but I think that will be a time sink with very little value.

Overall, this is basically working as intended right now. Without really strong support and argument to do it the opposite way I think we should keep it as is.

strulovich avatar Mar 28 '23 17:03 strulovich

Ok thanks for your comments

ukarlsson avatar Mar 29 '23 07:03 ukarlsson

Is there any progress?

savl-2021 avatar Jun 29 '23 11:06 savl-2021

There's no progress here. Also see https://github.com/facebook/ktfmt/issues/226

strulovich avatar Jun 29 '23 13:06 strulovich

See this comment here https://github.com/facebook/ktfmt/pull/435#issuecomment-2000099557

Also this would violate this principle here that we try to follow for ktfmt

hick209 avatar Jun 05 '24 18:06 hick209