Skip to content

Commit

Permalink
Made indent rule skip comments when calculating expected indent size
Browse files Browse the repository at this point in the history
  • Loading branch information
shyiko committed Feb 27, 2018
1 parent 1097ef0 commit 5fda7a4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ class IndentationRule : Rule("indent") {
val previousIndentSize = node.previousIndentSize()
val expectedIndentSize = if (continuationIndentSize == indentSize || shouldUseContinuationIndent(node))
continuationIndentSize else indentSize
lines.tail().forEach { line ->
if (line.isNotEmpty() && (line.length - previousIndentSize) % expectedIndentSize != 0) {
lines.tail().forEach { indent ->
if (indent.isNotEmpty() && (indent.length - previousIndentSize) % expectedIndentSize != 0) {
if (!node.isPartOf(KtParameterList::class)) { // parameter list wrapping enforced by ParameterListWrappingRule
emit(offset,
"Unexpected indentation (${line.length - previousIndentSize}) " +
"(it should be $expectedIndentSize)",
"Unexpected indentation (${
indent.length.let { if (it < previousIndentSize) it else it - previousIndentSize}
}) (it should be ${previousIndentSize + expectedIndentSize})",
false)
}
}
offset += line.length + 1
offset += indent.length + 1
}
}
}
Expand All @@ -80,21 +81,20 @@ class IndentationRule : Rule("indent") {
)
}

// todo: calculating indent based on the previous line value is wrong (see IndentationRule.testLint)
private fun ASTNode.previousIndentSize(): Int {
val parentNode = this.treeParent?.psi
var prevSibling = parentNode
while (prevSibling != null) {
val nextNode = prevSibling.nextSibling?.node?.elementType
if (prevSibling is PsiWhiteSpace &&
var node = this.treeParent?.psi
while (node != null) {
val nextNode = node.nextSibling?.node?.elementType
if (node is PsiWhiteSpace &&
nextNode != KtStubElementTypes.TYPE_REFERENCE &&
nextNode != KtStubElementTypes.SUPER_TYPE_LIST &&
nextNode != KtNodeTypes.CONSTRUCTOR_DELEGATION_CALL) {
val prevLines = prevSibling.text.split('\n')
if (prevLines.size > 1) {
return prevLines.last().length
}
nextNode != KtNodeTypes.CONSTRUCTOR_DELEGATION_CALL &&
node.textContains('\n') &&
node.nextLeaf()?.isPartOf(PsiComment::class) != true) {
return node.text.length - node.text.lastIndexOf('\n') - 1
}
prevSibling = prevSibling.prevSibling ?: prevSibling.parent
node = node.prevSibling ?: node.parent
}
return 0
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class IndentationRuleTest {
""".trimIndent()
)).isEqualTo(listOf(
LintError(12, 1, "indent", "Unexpected indentation (3) (it should be 4)"),
LintError(13, 1, "indent", "Unexpected indentation (5) (it should be 4)")
// fixme: expected indent should not depend on the "previous" line value
LintError(13, 1, "indent", "Unexpected indentation (5) (it should be 7)")
))
}

Expand Down Expand Up @@ -355,8 +356,17 @@ class IndentationRuleTest {
// comment
// comment
call(argA)
fun main() {
addOnLayoutChangeListener(object : View.OnLayoutChangeListener {
// comment
override fun onLayoutChange(
)
})
}
""".trimIndent(),
mapOf("indent_size" to "4", "continuation_indent_size" to "6")
)).isEmpty()
)).isEqualTo(listOf(
LintError(7, 1, "indent", "Unexpected indentation (1) (it should be 8)")
))
}
}

0 comments on commit 5fda7a4

Please sign in to comment.