Skip to content

Format multiple annotations and @[annotation1 annotation2] notations #3212

@Jolanrensen

Description

@Jolanrensen

Expected Rule behavior

Some libraries have a lot of annotations. This is more common in server-side libraries that use Spring, for instance. However, this can become quite cluttered when unformatted or unstructured.

Take a look at the following example from the wild:

@[Deprecated(DEPRECATED_ACCESS_API) AccessApiOverload]
@Suppress("INAPPLICABLE_JVM_NAME")
@JvmName("frameColDataFrameKProperty")
public fun <C> AnyColumnGroupAccessor.frameCol(property: KProperty<DataFrame<C>>) = TODO()

I wonder, is KtLint familiar with the @[annotation1 annotation2] notation?

What I would recommend the rules to do:

  • enforce lexicographical order of annotations, similar to imports, putting @[] annotations at the bottom (as they're often the longest):
@JvmName("frameColDataFrameKProperty")
@Suppress("INAPPLICABLE_JVM_NAME")
@[AccessApiOverload Deprecated(DEPRECATED_ACCESS_API)]
public fun <C> AnyColumnGroupAccessor.frameCol(property: KProperty<DataFrame<C>>) = TODO()
  • if a @[] spans multiple lines, they should be indented together, with a single annotation per line (this is currently not recommended):
@[
JvmName("frameColDataFrameKProperty") Suppress("INAPPLICABLE_JVM_NAME")
AccessApiOverload
Deprecated(DEPRECATED_ACCESS_API)
]
public fun <C> AnyColumnGroupAccessor.frameCol(property: KProperty<DataFrame<C>>) = TODO()

should become something like this:

@[
    AccessApiOverload
    Deprecated(DEPRECATED_ACCESS_API)
    JvmName("frameColDataFrameKProperty")
    Suppress("INAPPLICABLE_JVM_NAME")
]
public fun <C> AnyColumnGroupAccessor.frameCol(property: KProperty<DataFrame<C>>) = TODO()
  • Using @[annotation] should be replaced by @annotation
  • Using @[annotation1 annotation2] on one line is fine if they fit. Else they should spread across multiple lines.
  • Using 3+ annotations (or a customizable other number) without @[] will be replaced with the @[annotation1 annotation2 annotation3] syntax
  • Using multiple @[] is allowed to group annotations together, but they should be ordered by width
@[AccessApiOverload Deprecated(DEPRECATED_ACCESS_API)]
@[JvmName("frameColDataFrameKProperty") Suppress("INAPPLICABLE_JVM_NAME")]
public fun <C> AnyColumnGroupAccessor.frameCol...

Some other ideas for improvements are welcome, of course :)
Unfortunately, there are not any guides regarding this Kotlin feature yet.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions