Skip to content

Commit

Permalink
Allow whitespace after lpar followed by a comment (#664)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roman Zavarnitsyn authored and Tapchicoma committed Dec 1, 2019
1 parent bcf5c07 commit 10dd8ff
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)
Expand All @@ -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()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
}

0 comments on commit 10dd8ff

Please sign in to comment.