Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix. Comments should be indented as the code they are commenting #566

Merged
merged 5 commits into from
Nov 30, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.elementType == DOT
aktsay6 marked this conversation as resolved.
Show resolved Hide resolved
}
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()
}