Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow whitespace after lpar followed by a comment #664

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}