diff --git a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRule.kt b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRule.kt index 7d95589146..18b4479661 100644 --- a/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRule.kt +++ b/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRule.kt @@ -1,7 +1,10 @@ package com.pinterest.ktlint.ruleset.standard import com.pinterest.ktlint.core.Rule +import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT +import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER +import com.pinterest.ktlint.core.ast.ElementType.KDOC_START import com.pinterest.ktlint.core.ast.ElementType.LPAR import com.pinterest.ktlint.core.ast.ElementType.RPAR import com.pinterest.ktlint.core.ast.ElementType.SUPER_KEYWORD @@ -29,23 +32,19 @@ class SpacingAroundParensRule : Rule("paren-spacing") { val nextLeaf = node.nextLeaf() val spacingBefore = if (node.elementType == LPAR) { prevLeaf is PsiWhiteSpace && !prevLeaf.textContains('\n') && - ( - prevLeaf.prevLeaf()?.elementType == IDENTIFIER || - // Super keyword needs special-casing - prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD - ) && ( - node.treeParent?.elementType == VALUE_PARAMETER_LIST || - node.treeParent?.elementType == VALUE_ARGUMENT_LIST - ) + (prevLeaf.prevLeaf()?.elementType == IDENTIFIER || + // Super keyword needs special-casing + prevLeaf.prevLeaf()?.elementType == SUPER_KEYWORD) && + (node.treeParent?.elementType == VALUE_PARAMETER_LIST || + node.treeParent?.elementType == VALUE_ARGUMENT_LIST) } else { prevLeaf is PsiWhiteSpace && !prevLeaf.textContains('\n') && prevLeaf.prevLeaf()?.elementType != LPAR } val spacingAfter = if (node.elementType == LPAR) { - nextLeaf is PsiWhiteSpace && ( - !nextLeaf.textContains('\n') || - nextLeaf.nextLeaf()?.elementType == RPAR - ) + nextLeaf is PsiWhiteSpace && + (!nextLeaf.textContains('\n') || nextLeaf.nextLeaf()?.elementType == RPAR) && + !nextLeaf.isNextLeafAComment() } else { nextLeaf is PsiWhiteSpace && !nextLeaf.textContains('\n') && nextLeaf.nextLeaf()?.elementType == RPAR @@ -73,4 +72,9 @@ class SpacingAroundParensRule : Rule("paren-spacing") { } } } + + private fun ASTNode.isNextLeafAComment(): Boolean { + val commentTypes = setOf(EOL_COMMENT, BLOCK_COMMENT, KDOC_START) + return nextLeaf()?.elementType in commentTypes + } } diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/CommentSpacingRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/CommentSpacingRuleTest.kt index dd14cfb629..68f96a065b 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/CommentSpacingRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/CommentSpacingRuleTest.kt @@ -60,6 +60,11 @@ class CommentSpacingRuleTest { var debugging = false// comment var debugging = false //comment var debugging = false//comment + fun main() { + System.out.println(//123 + "test" + ) + } //comment """.trimIndent() ) @@ -69,6 +74,11 @@ class CommentSpacingRuleTest { var debugging = false // comment var debugging = false // comment var debugging = false // comment + fun main() { + System.out.println( // 123 + "test" + ) + } // comment """.trimIndent() ) diff --git a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRuleTest.kt b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRuleTest.kt index 968447322a..768e5ec9e3 100644 --- a/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRuleTest.kt +++ b/ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/SpacingAroundParensRuleTest.kt @@ -2,6 +2,8 @@ package com.pinterest.ktlint.ruleset.standard import com.pinterest.ktlint.test.diffFileFormat import com.pinterest.ktlint.test.diffFileLint +import com.pinterest.ktlint.test.format +import com.pinterest.ktlint.test.lint import org.assertj.core.api.Assertions.assertThat import org.junit.Test @@ -21,4 +23,39 @@ class SpacingAroundParensRuleTest { ) ).isEmpty() } + + @Test + fun `lint spacing after lpar followed by a comment is allowed`() { + assertThat(SpacingAroundParensRule().lint(""" + fun main() { + System.out.println( /** 123 */ + "test kdoc" + ) + System.out.println( /* 123 */ + "test comment block" + ) + System.out.println( // 123 + "test single comment" + ) + } + """.trimIndent())).isEmpty() + } + + @Test + fun `format spacing after lpar followed by a comment is allowed`() { + val code = """ + fun main() { + System.out.println( /** 123 */ + "test kdoc" + ) + System.out.println( /* 123 */ + "test comment block" + ) + System.out.println( // 123 + "test single comment" + ) + } + """.trimIndent() + assertThat(SpacingAroundParensRule().format(code)).isEqualTo(code) + } }