Skip to content

Commit

Permalink
Bugfix. Comments should be indented as the code they are commenting (#…
Browse files Browse the repository at this point in the history
…566)

* bugfix/indentation-of-comments(#553)

### What's done:
  * Fixed bugs
  • Loading branch information
aktsay6 authored Nov 30, 2020
1 parent 9224019 commit 4ce391a
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import com.pinterest.ktlint.core.ast.ElementType.ARROW
import com.pinterest.ktlint.core.ast.ElementType.AS_KEYWORD
import com.pinterest.ktlint.core.ast.ElementType.AS_SAFE
import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.BLOCK_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.BODY
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.COLON
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.ELSE
import com.pinterest.ktlint.core.ast.ElementType.ELVIS
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EQ
import com.pinterest.ktlint.core.ast.ElementType.IS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.KDOC_END
Expand Down Expand Up @@ -174,13 +176,25 @@ internal class DotCallChecker(config: IndentationConfig) : CustomIndentationChec
private fun ASTNode.isDotBeforeCallOrReference() = elementType.let { it == DOT || it == SAFE_ACCESS } &&
treeNext.elementType.let { it == CALL_EXPRESSION || it == REFERENCE_EXPRESSION }

private fun ASTNode.isCommentBeforeDot() : Boolean {
if (elementType == EOL_COMMENT || elementType == BLOCK_COMMENT) {
var nextNode = treeNext
while (nextNode != null && (nextNode.elementType == WHITE_SPACE || nextNode.elementType == EOL_COMMENT)) {
nextNode = nextNode.treeNext
}
return nextNode.isDotBeforeCallOrReference()
}
return false
}

@Suppress("ComplexMethod")
override fun checkNode(whiteSpace: PsiWhiteSpace, indentError: IndentationError): CheckResult? {
whiteSpace.nextSibling.node
.takeIf { nextNode ->
nextNode.isDotBeforeCallOrReference() ||
nextNode.elementType == OPERATION_REFERENCE && nextNode.firstChildNode.elementType.let {
it == ELVIS || it == IS_EXPRESSION || it == AS_KEYWORD || it == AS_SAFE
}
} || nextNode.isCommentBeforeDot()
}
?.let {
// we need to get indent before the first expression in calls chain
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,13 @@ fun `method name incorrect, part 4`() {
}
""".trimIndent()
lintMethod(code, LintError(2, 7, ruleId, "${FUNCTION_NAME_INCORRECT_CASE.warnText()} methODTREE", true))

foo
// we are calling bar
.bar()

bar
/* This is a block comment */
.foo()
}

Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,13 @@ fun `method name incorrect, part 4`() {
}
""".trimIndent()
lintMethod(code, LintError(2, 7, ruleId, "${FUNCTION_NAME_INCORRECT_CASE.warnText()} methODTREE", true))

foo
// we are calling bar
.bar()

bar
/* This is a block comment */
.foo()
}

0 comments on commit 4ce391a

Please sign in to comment.