diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt index 99b9ac0396..b5dea8773a 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRule.kt @@ -4,9 +4,11 @@ import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.children import org.jetbrains.kotlin.com.intellij.lang.ASTNode +import org.jetbrains.kotlin.com.intellij.psi.PsiComment import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.psi.KtAnnotationEntry +import org.jetbrains.kotlin.psi.psiUtil.nextLeaf class AnnotationRule : Rule("annotation") { @@ -52,7 +54,8 @@ class AnnotationRule : Rule("annotation") { annotations.size > 1 && !whiteSpaces.last().textContains('\n') val annotationsWithParametersAreNotOnSeparateLines = annotations.any { it.valueArgumentList != null } && - !whiteSpaces.all { it.textContains('\n') } + !whiteSpaces.all { it.textContains('\n') } && + doesNotEndWithAComment(whiteSpaces) if (multipleAnnotationsOnSameLineAsAnnotatedConstruct) { emit( @@ -84,4 +87,9 @@ class AnnotationRule : Rule("annotation") { } } } + + private fun doesNotEndWithAComment(whiteSpaces: List): Boolean { + val lastNode = whiteSpaces.lastOrNull()?.nextLeaf() + return lastNode !is PsiComment || lastNode.nextLeaf()?.textContains('\n') == false + } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt index ca7087e214..6d7206b0cd 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/AnnotationRuleTest.kt @@ -22,6 +22,40 @@ class AnnotationRuleTest { ).isEmpty() } + @Test + fun `lint single annotation with parameters ends with a comment`() { + assertThat( + AnnotationRule().lint( + """ + @Suppress("AnnotationRule") // this is a comment + class A + """.trimIndent() + ) + ).isEmpty() + } + + @Test + fun `lint single annotation with parameters on the same line with a comment`() { + assertThat( + AnnotationRule().lint( + """ + @Suppress("AnnotationRule") /* this is a comment */ class A + """.trimIndent() + ) + ).hasSize(1) + } + + @Test + fun `lint single annotation with parameters on the same line with a comment and many spaces`() { + assertThat( + AnnotationRule().lint( + """ + @Suppress("AnnotationRule") /* this is a comment */ class A + """.trimIndent() + ) + ).hasSize(1) + } + @Test fun `format single annotation may be placed on line before annotated construct`() { val code =