Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow annotations within angle brackets to be placed on same line #654

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.pinterest.ktlint.ruleset.experimental

import com.pinterest.ktlint.core.Rule
import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST
import com.pinterest.ktlint.core.ast.ElementType.TYPE_ARGUMENT_LIST
import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST
import com.pinterest.ktlint.core.ast.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.core.ast.children
import com.pinterest.ktlint.core.ast.isPartOf
import com.pinterest.ktlint.core.ast.upsertWhitespaceBeforeMe
import org.jetbrains.kotlin.com.intellij.lang.ASTNode
import org.jetbrains.kotlin.com.intellij.psi.PsiComment
Expand Down Expand Up @@ -74,8 +76,9 @@ class AnnotationRule : Rule("annotation") {
annotations.any { it.valueArgumentList != null } &&
!whiteSpaces.all { it.textContains('\n') } &&
doesNotEndWithAComment(whiteSpaces) &&
node.treeParent.elementType != VALUE_PARAMETER_LIST &&
node.treeParent.elementType != VALUE_ARGUMENT_LIST
node.treeParent.elementType != VALUE_PARAMETER_LIST && // fun fn(@Ann("blah") a: String)
node.treeParent.elementType != VALUE_ARGUMENT_LIST && // fn(@Ann("blah") "42")
!node.isPartOf(TYPE_ARGUMENT_LIST) // val property: Map<@Ann("blah") String, Int>

if (multipleAnnotationsOnSameLineAsAnnotatedConstruct) {
emit(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -549,4 +549,40 @@ class AnnotationRuleTest {
""".trimIndent()
assertThat(AnnotationRule().format(code)).isEqualTo(code)
}

@Test
fun `lint annotations on type arguments may be placed on same line`() {
val code =
"""
val aProperty: Map<@Ann("test") Int, @JvmSuppressWildcards(true) (String) -> Int?>
val bProperty: Map<
@Ann String,
@Ann("test") Int,
@JvmSuppressWildcards(true) (String) -> Int?
>

fun doSomething() {
funWithGenericsCall<@JvmSuppressWildcards(true) Int>()
}
""".trimIndent()
assertThat(AnnotationRule().lint(code)).isEmpty()
}

@Test
fun `format annotations on type arguments may be placed on same line`() {
val code =
"""
val aProperty: Map<@Ann("test") Int, @JvmSuppressWildcards(true) (String) -> Int?>
val bProperty: Map<
@Ann String,
@Ann("test") Int,
@JvmSuppressWildcards(true) (String) -> Int?
>

fun doSomething() {
funWithGenericsCall<@JvmSuppressWildcards(true) Int>()
}
""".trimIndent()
assertThat(AnnotationRule().format(code)).isEqualTo(code)
}
}