forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add CommentSpacingRule for end of line comment padding (pinterest#198)
- 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
1 parent
cb91be5
commit a9d1a4b
Showing
3 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...-standard/src/main/kotlin/com/github/shyiko/ktlint/ruleset/standard/CommentSpacingRule.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("//")) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...ndard/src/test/kotlin/com/github/shyiko/ktlint/ruleset/standard/CommentSpacingRuleTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) | ||
} | ||
} |