pinterest / ktlint

An anti-bikeshedding Kotlin linter with built-in formatter
https://pinterest.github.io/ktlint/
MIT License
6.23k stars 512 forks source link

AnnotationRule formatting uncertainties #556

Closed romtsn closed 4 years ago

romtsn commented 5 years ago

I have a following Retrofit interface:

interface UserService {

    fun updateMarketingConsent(
        @Path("userId") userId: String,
        @Path("context") context: String,
        @Body body: MarketingConsent
    ): Completable

    fun showUser(@Path("id") userId: String, @Query("include") include: String? = null, @QueryMap fields: Map<String, String> = emptyMap()): Single

    fun logout(@Path("userId") userId: String): Completable
}

Running ktlint in --format mode gives the following output:

interface UserService {

    fun updateMarketingConsent(
        @Path("userId")
        userId: String,
        @Path("context")
        context: String,
        @Body body: MarketingConsentStructure
    ): Completable

    fun showUser(
        @Path("id")
        userId: String,
        @Query("include") include: String? = null, @QueryMap fields: Map<String, String> = emptyMap()
    ): Single

    fun logout(
        @Path("userId")
        userId: String
    ): Completable
}

Imo, the latter is way less readable. I went through the official styleguide, but I think an annotation with arguments should be allowed for method params (even though not sure that it's applicable for all possible cases).

Also in the showUser function, the last two parameters should be placed on different lines (so the with-argument annotation followed by no-argument annotation). Cause otherwise the parameter-list-wrapping rules kicks in. This is most likely a bug in formatter, right?

shashachu commented 5 years ago

Ah, yeah. I don't think the rule was intended for parameter annotations. We can put up a diff to have it ignore those annotations completely.

francescocervone commented 5 years ago

Is there a way to disable the annotation rule?

romtsn commented 5 years ago

@francescocervone disabled_rules=experimental:annotation in your .editorconfig

arturbosch commented 5 years ago

+1 for this change

yukukotani commented 5 years ago

I think readability and following official style are both important. I suggest to create two codestyle (https://github.com/pinterest/ktlint/issues/548), official one and strong one.