From f3e525748554680a9f66fbd1429886bdcf25ffaa Mon Sep 17 00:00:00 2001 From: paul-dingemans Date: Wed, 2 Nov 2022 16:20:34 +0100 Subject: [PATCH] Fix indent of annotation array entry (#1689) * Fix indent of annotation array entry Closes #1688 --- CHANGELOG.md | 2 +- .../ktlint/ruleset/standard/AnnotationRule.kt | 6 +++++- .../ktlint/ruleset/standard/AnnotationRuleTest.kt | 10 ++++++++++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26df1c6793..4440958d60 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,7 +44,7 @@ if (node.isRoot()) { * Update Kotlin development version to `1.7.20` and Kotlin version to `1.7.20`. * CLI options `--debug`, `--trace`, `--verbose` and `-v` are replaced with `--log-level=` or the short version `-l=, see [CLI log-level](https://pinterest.github.io/ktlint/install/cli/#logging). ([#1632](https://github.com/pinterest/ktlint/issue/1632)) * In CLI, disable logging entirely by setting `--log-level=none` or `-l=none` ([#1652](https://github.com/pinterest/ktlint/issue/1652)) -* Rewrite `indent` rule. Solving problems in the old algorithm was very difficult. With the new algorithm this becomes a lot easier. Although the new implementation of the rule has been compared against several open source projects containing over 400,000 lines of code, it is still likely that new issues will be discovered. Please report your indentation issues so that these can be fixed as well. ([#1682](https://github.com/pinterest/ktlint/pull/1682), [#1321](https://github.com/pinterest/ktlint/issues/1321), [#1200](https://github.com/pinterest/ktlint/issues/1200), [#1562](https://github.com/pinterest/ktlint/issues/1562), [#1563](https://github.com/pinterest/ktlint/issues/1563), [#1639](https://github.com/pinterest/ktlint/issues/1639)) +* Rewrite `indent` rule. Solving problems in the old algorithm was very difficult. With the new algorithm this becomes a lot easier. Although the new implementation of the rule has been compared against several open source projects containing over 400,000 lines of code, it is still likely that new issues will be discovered. Please report your indentation issues so that these can be fixed as well. ([#1682](https://github.com/pinterest/ktlint/pull/1682), [#1321](https://github.com/pinterest/ktlint/issues/1321), [#1200](https://github.com/pinterest/ktlint/issues/1200), [#1562](https://github.com/pinterest/ktlint/issues/1562), [#1563](https://github.com/pinterest/ktlint/issues/1563), [#1639](https://github.com/pinterest/ktlint/issues/1639), [#1688](https://github.com/pinterest/ktlint/issues/1688)) ## [0.47.1] - 2022-09-07 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 b2ebb72eec..4e7d8eef9e 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 @@ -2,6 +2,7 @@ package com.pinterest.ktlint.ruleset.standard import com.pinterest.ktlint.core.Rule import com.pinterest.ktlint.core.ast.ElementType.ANNOTATED_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY import com.pinterest.ktlint.core.ast.ElementType.FILE_ANNOTATION_LIST import com.pinterest.ktlint.core.ast.ElementType.TYPE_ARGUMENT_LIST @@ -80,7 +81,10 @@ public class AnnotationRule : Rule("annotation") { checkForAnnotationToBePlacedOnSeparateLine(node, emit, autoCorrect) } - if (node.isPrecededByOtherAnnotationEntry() && node.isOnSameLineAsAnnotatedConstruct()) { + if (node.treeParent.elementType != ANNOTATION && + node.isPrecededByOtherAnnotationEntry() && + node.isOnSameLineAsAnnotatedConstruct() + ) { checkForMultipleAnnotationsOnSameLineAsAnnotatedConstruct(node, emit, autoCorrect) } } 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 31c6166d85..3ebcaf478f 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 @@ -538,4 +538,14 @@ class AnnotationRuleTest { annotationRuleAssertThat(code) .hasLintViolationWithoutAutoCorrect(1, 13, "Annotation with parameter(s) should be placed on a separate line prior to the annotated construct") } + + @Test + fun `Given an annotation with multiple annotation entries then do not force wrapping of the entries`() { + val code = + """ + @[JvmStatic Provides] + fun foo() = 42 + """.trimIndent() + annotationRuleAssertThat(code).hasNoLintViolations() + } }