Skip to content

Commit

Permalink
Add CommentSpacingRule for end of line comment padding (pinterest#198)
Browse files Browse the repository at this point in the history
- Should add spaces before end of line comments if they are not at the beginning of a line, and add spaces after the double slash (//) if not present
  • Loading branch information
JelloRanger committed May 1, 2018
1 parent cb91be5 commit a9d1a4b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.github.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.Rule
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.PsiWhiteSpaceImpl
import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil

class CommentSpacingRule : Rule("comment-spacing") {

override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
if (node is LeafPsiElement && node is PsiComment && node.getText().startsWith("//")) {
val prevLeaf = PsiTreeUtil.prevLeaf(node)
if (prevLeaf !is PsiWhiteSpace && prevLeaf is LeafPsiElement) {
emit(node.startOffset, "Missing space before end of line comment", true)
if (autoCorrect) {
node.rawInsertBeforeMe(PsiWhiteSpaceImpl(" "))
}
}
if (!node.getText().startsWith("// ")) {
emit(node.startOffset, "Missing space after double slash in end of line comment", true)
if (autoCorrect) {
node.rawReplaceWithText("// " + node.getText().removePrefix("//"))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class StandardRuleSetProvider : RuleSetProvider {

override fun get(): RuleSet = RuleSet("standard",
ChainWrappingRule(),
CommentSpacingRule(),
FinalNewlineRule(),
// disabled until it's clear how to reconcile difference in Intellij & Android Studio import layout
// ImportOrderingRule(),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.github.shyiko.ktlint.ruleset.standard

import com.github.shyiko.ktlint.core.LintError
import com.github.shyiko.ktlint.test.format
import com.github.shyiko.ktlint.test.lint
import org.assertj.core.api.Assertions.assertThat
import org.testng.annotations.Test

class CommentSpacingRuleTest {

@Test
fun testLintValidCommentSpacing() {
assertThat(CommentSpacingRule().lint(
"""
// comment
var debugging = false // comment
var debugging = false // comment//word
// comment
""".trimIndent()
)).isEmpty()
}

@Test
fun testLintInvalidCommentSpacing() {
assertThat(CommentSpacingRule().lint(
"""
//comment
var debugging = false// comment
var debugging = false //comment
var debugging = false//comment
//comment
""".trimIndent()
)).isEqualTo(listOf(
LintError(1, 1, "comment-spacing", "Missing space after double slash in end of line comment"),
LintError(2, 22, "comment-spacing", "Missing space before end of line comment"),
LintError(3, 23, "comment-spacing", "Missing space after double slash in end of line comment"),
LintError(4, 22, "comment-spacing", "Missing space before end of line comment"),
LintError(4, 22, "comment-spacing", "Missing space after double slash in end of line comment"),
LintError(5, 5, "comment-spacing", "Missing space after double slash in end of line comment")
))
}

@Test
fun testFormatInvalidCommentSpacing() {
assertThat(CommentSpacingRule().format(
"""
//comment
var debugging = false// comment
var debugging = false //comment
var debugging = false//comment
//comment
""".trimIndent()
)).isEqualTo(
"""
// comment
var debugging = false // comment
var debugging = false // comment
var debugging = false // comment
// comment
""".trimIndent()
)
}
}

0 comments on commit a9d1a4b

Please sign in to comment.