Skip to content

Commit

Permalink
Move EOL comment after left curly brace so code remains compilable (#730
Browse files Browse the repository at this point in the history
)

* Move EOL comment after left curly brace so code remains compilable

* Leave comment on same line as left curly brace

* Remove unused property
  • Loading branch information
csgulley committed May 23, 2020
1 parent 5fa5262 commit 63e8018
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY
import com.pinterest.ktlint.core.ast.ElementType.COLONCOLON
import com.pinterest.ktlint.core.ast.ElementType.COMMA
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EXCLEXCL
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
Expand All @@ -23,8 +24,10 @@ import com.pinterest.ktlint.core.ast.prevLeaf
import com.pinterest.ktlint.core.ast.upsertWhitespaceAfterMe
import com.pinterest.ktlint.core.ast.upsertWhitespaceBeforeMe
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.impl.source.tree.TreeElement
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.psi.KtLambdaExpression

Expand Down Expand Up @@ -67,6 +70,18 @@ class SpacingAroundCurlyRule : Rule("curly-spacing") {
) {
emit(node.startOffset, "Unexpected newline before \"${node.text}\"", true)
if (autoCorrect) {
val eolCommentExists = prevLeaf.prevLeaf()?.let {
it is PsiComment && it.elementType == EOL_COMMENT
} ?: false
if (eolCommentExists) {
val commentLeaf = prevLeaf.prevLeaf()!!
if (commentLeaf.prevLeaf() is PsiWhiteSpace) {
(commentLeaf.prevLeaf() as LeafPsiElement).rawRemove()
}
(node.treeParent.treeParent as TreeElement).removeChild(commentLeaf)
(node.treeParent as TreeElement).addChild(commentLeaf, node.treeNext)
node.upsertWhitespaceAfterMe(" ")
}
(prevLeaf as LeafPsiElement).rawReplaceWithText(" ")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,4 +209,44 @@ class SpacingAroundCurlyRuleTest {
{ 42 }
""".trimIndent())
}

@Test
fun `eol comment placed after curly brace`() {
assertThat(
SpacingAroundCurlyRule().format(
"""
class MyClass()// a comment
{
val x = 0
}
""".trimIndent()
)
).isEqualTo(
"""
class MyClass() { // a comment
val x = 0
}
""".trimIndent()
)
}

@Test
fun `eol comment with preceding whitespace placed after curly brace`() {
assertThat(
SpacingAroundCurlyRule().format(
"""
class MyClass() // a comment
{
val x = 0
}
""".trimIndent()
)
).isEqualTo(
"""
class MyClass() { // a comment
val x = 0
}
""".trimIndent()
)
}
}

0 comments on commit 63e8018

Please sign in to comment.