From 3f069cee9e9b011258dac1bf71822023da0a86a9 Mon Sep 17 00:00:00 2001 From: Harold Martin Date: Tue, 26 Apr 2022 19:57:36 -0700 Subject: [PATCH 1/4] Wrapping rule should not prevent comments for interfaces on separate lines --- .../ktlint/ruleset/standard/WrappingRule.kt | 6 +++++- .../ruleset/standard/WrappingRuleTest.kt | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt index d4a3e6ff9b..876595f64e 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt @@ -210,7 +210,11 @@ public class WrappingRule : Rule( // put entries on separate lines // TODO: group emit()s below with the one above into one (similar to ParameterListWrappingRule) for (c in node.children()) { - if (c.elementType == COMMA && !c.treeNext.isWhiteSpaceWithNewline()) { + if ( + c.elementType == COMMA && + !c.treeNext.isWhiteSpaceWithNewline() && + !(c.nextLeaf() is PsiWhiteSpace && c.nextLeaf()?.nextLeaf()?.isPartOfComment() == true) + ) { requireNewlineAfterLeaf( nodeAfterWhichNewlineIsRequired = c, autoCorrect = autoCorrect, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt index a17fe1a81c..21f9c92adc 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt @@ -134,6 +134,24 @@ internal class WrappingRuleTest { ).isEmpty() } + @Test + fun testLintClassImplementsListWithComments() { + assertThat( + WrappingRule().lint( + """ + interface ThingOne {} + interface ThingTwo {} + class BusyClass : + ThingOne, // this comment should be legal + ThingTwo { + + } + + """.trimIndent() + ) + ).isEmpty() + } + @Test fun testLintNewlineAfterEqAllowed() { assertThat( From 9880c40da540b22d7a9a1fa8391395c1f1a0e432 Mon Sep 17 00:00:00 2001 From: Harold Martin Date: Tue, 3 May 2022 17:17:03 -0700 Subject: [PATCH 2/4] address PR comments --- .../ktlint/ruleset/standard/WrappingRule.kt | 7 +--- .../ruleset/standard/WrappingRuleTest.kt | 38 ++++++++++++------- 2 files changed, 27 insertions(+), 18 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt index 876595f64e..ef1f87b0e7 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt @@ -54,6 +54,7 @@ import com.pinterest.ktlint.core.initKtLintKLogger import mu.KotlinLogging 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.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet @@ -210,11 +211,7 @@ public class WrappingRule : Rule( // put entries on separate lines // TODO: group emit()s below with the one above into one (similar to ParameterListWrappingRule) for (c in node.children()) { - if ( - c.elementType == COMMA && - !c.treeNext.isWhiteSpaceWithNewline() && - !(c.nextLeaf() is PsiWhiteSpace && c.nextLeaf()?.nextLeaf()?.isPartOfComment() == true) - ) { + if (c.elementType == COMMA && !c.treeNext.isWhiteSpaceWithNewline() && !(c.nextLeaf() is PsiWhiteSpace && c.nextLeaf()?.nextLeaf()?.isPartOfComment() == true)) { requireNewlineAfterLeaf( nodeAfterWhichNewlineIsRequired = c, autoCorrect = autoCorrect, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt index 1a03e930f3..1a550fd11a 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt @@ -318,21 +318,33 @@ internal class WrappingRuleTest { } @Test - fun testLintClassImplementsListWithComments() { - assertThat( - WrappingRule().lint( - """ - interface ThingOne {} - interface ThingTwo {} - class BusyClass : - ThingOne, // this comment should be legal - ThingTwo { + fun `test wrapping rule allows line comment`() { + val code = + """ + interface ThingOne {} + interface ThingTwo {} + class BusyClass : + ThingOne, // this comment should be legal + ThingTwo { - } + } + """.trimIndent() + wrappingRuleAssertThat(code).hasNoLintViolations() + } - """.trimIndent() - ) - ).isEmpty() + @Test + fun `test wrapping rule allows block comment`() { + val code = + """ + interface ThingOne {} + interface ThingTwo {} + class BusyClass : + ThingOne, /* this comment should be legal */ + ThingTwo { + + } + """.trimIndent() + wrappingRuleAssertThat(code).hasNoLintViolations() } @Test From 2b406825a9434578620739f950638fceeef4b632 Mon Sep 17 00:00:00 2001 From: = <=> Date: Thu, 5 May 2022 20:51:00 +0200 Subject: [PATCH 3/4] Do not require wrapping when comment is not preceded by a space --- .../ktlint/ruleset/standard/WrappingRule.kt | 10 +++++-- .../ruleset/standard/WrappingRuleTest.kt | 26 ++++++++++--------- 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt index ef1f87b0e7..568016f4dc 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRule.kt @@ -54,7 +54,6 @@ import com.pinterest.ktlint.core.initKtLintKLogger import mu.KotlinLogging 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.com.intellij.psi.tree.IElementType import org.jetbrains.kotlin.com.intellij.psi.tree.TokenSet @@ -211,7 +210,10 @@ public class WrappingRule : Rule( // put entries on separate lines // TODO: group emit()s below with the one above into one (similar to ParameterListWrappingRule) for (c in node.children()) { - if (c.elementType == COMMA && !c.treeNext.isWhiteSpaceWithNewline() && !(c.nextLeaf() is PsiWhiteSpace && c.nextLeaf()?.nextLeaf()?.isPartOfComment() == true)) { + if (c.elementType == COMMA && + !c.treeNext.isWhiteSpaceWithNewline() && + !c.isFollowedByCommentOnSameLine() + ) { requireNewlineAfterLeaf( nodeAfterWhichNewlineIsRequired = c, autoCorrect = autoCorrect, @@ -223,6 +225,10 @@ public class WrappingRule : Rule( } } + private fun ASTNode.isFollowedByCommentOnSameLine() = + nextLeaf { !it.isWhiteSpaceWithoutNewline() } + ?.isPartOfComment() == true + private fun rearrangeValueList( node: ASTNode, autoCorrect: Boolean, diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt index 1a550fd11a..f707770346 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/WrappingRuleTest.kt @@ -321,12 +321,13 @@ internal class WrappingRuleTest { fun `test wrapping rule allows line comment`() { val code = """ - interface ThingOne {} - interface ThingTwo {} - class BusyClass : - ThingOne, // this comment should be legal - ThingTwo { - + interface Foo1 {} + interface Foo2 {} + interface Foo3 {} + class Bar : + Foo1, // this comment should be legal + Foo2,// this comment should be legal + Foo3 { } """.trimIndent() wrappingRuleAssertThat(code).hasNoLintViolations() @@ -336,12 +337,13 @@ internal class WrappingRuleTest { fun `test wrapping rule allows block comment`() { val code = """ - interface ThingOne {} - interface ThingTwo {} - class BusyClass : - ThingOne, /* this comment should be legal */ - ThingTwo { - + interface Foo1 {} + interface Foo2 {} + interface Foo3 {} + class Bar : + Foo1, /* this comment should be legal */ + Foo2,/* this comment should be legal */ + Foo3 { } """.trimIndent() wrappingRuleAssertThat(code).hasNoLintViolations() From dbed2e637a017c9b0fdf162ea8097d19a5725877 Mon Sep 17 00:00:00 2001 From: = <=> Date: Sun, 8 May 2022 15:18:01 +0200 Subject: [PATCH 4/4] Update changelog Closes #1457 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index de4f960e9b..7976844770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter ### Fixed - Fix check of spacing in the receiver type of an anonymous function ([#1440](https://github.com/pinterest/ktlint/issues/1440)) +- Allow comment on same line as super class in class declaration `wrapping` ([#1457](https://github.com/pinterest/ktlint/pull/1457)) ### Changed * Set Kotlin development version to `1.6.21` and Kotlin version to `1.6.21`.