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 of NPE in complex binary expressions in long line rule #983

Merged
merged 8 commits into from
Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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 @@ -30,6 +30,7 @@ import com.pinterest.ktlint.core.ast.ElementType.KDOC_TEXT
import com.pinterest.ktlint.core.ast.ElementType.LITERAL_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LPAR
import com.pinterest.ktlint.core.ast.ElementType.NULL
import com.pinterest.ktlint.core.ast.ElementType.OPERATION_REFERENCE
import com.pinterest.ktlint.core.ast.ElementType.PACKAGE_DIRECTIVE
import com.pinterest.ktlint.core.ast.ElementType.PARENTHESIZED
Expand Down Expand Up @@ -177,12 +178,15 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
return LongLineFixableCases.None
}
// minus 2 here as we are inserting ` +` and we don't want it to exceed line length
val shouldAddTwoSpaces = multiLineOffset == 0 && leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2
val shouldAddTwoSpaces = (multiLineOffset == 0) && (leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2)
val correcterDelimiter = if (shouldAddTwoSpaces) {
node.text.substring(0, delimiterIndex - 2).lastIndexOf(' ')
} else {
delimiterIndex
}
if (correcterDelimiter == -1) {
return LongLineFixableCases.None
}
return LongLineFixableCases.StringTemplate(node, correcterDelimiter, multiLineOffset == 0)
}

Expand Down Expand Up @@ -330,7 +334,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
binList.forEachIndexed { index, astNode ->
binaryText += findAllText(astNode)
if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) {
val commonParent = astNode.parent({ it in binList[index - 1].parents() })!!
val commonParent = astNode.parent({ it.elementType == BINARY_EXPRESSION && it in binList[index - 1].parents() })!!
val nextNode = commonParent.findChildByType(OPERATION_REFERENCE)!!.treeNext
if (!nextNode.text.contains("\n")) {
commonParent.appendNewlineMergingWhiteSpace(nextNode, nextNode)
Expand Down Expand Up @@ -429,19 +433,33 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
}
}

/**
* Collect by Depth-first search (DFS) all children to the right side of the equal sign with specific type [propertyList],
* by which we can split expression.
* Such logic needed, because AST representation of complex conditions is quite loaded
*
* @param node target node to be processed
* @param binList where to store the corresponding results
*/
private fun dfsForProperty(node: ASTNode, binList: MutableList<ASTNode>) {
node.getChildren(null).forEach {
if (it.elementType in propertyList) {
if (it.elementType == REFERENCE_EXPRESSION && it.treeParent.elementType == CALL_EXPRESSION) {
binList.add(it.treeParent)
if (it.elementType == REFERENCE_EXPRESSION && it.treeParent?.elementType == CALL_EXPRESSION) {
binList.tryAdd(it.treeParent)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did we add the same element multiple times here? How could it be?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, thanks, I did a lot of experiments with dfsForProperty and it's remain from them, now it's not necessary, removed.

} else {
binList.add(it)
binList.tryAdd(it)
}
}
dfsForProperty(it, binList)
}
}

private fun MutableList<ASTNode>.tryAdd(node: ASTNode) {
kgevorkyan marked this conversation as resolved.
Show resolved Hide resolved
if (node !in this) {
this.add(node)
}
}

private fun createSplitProperty(wrongProperty: LongLineFixableCases.Property) {
val node = wrongProperty.node
val indexLastSpace = wrongProperty.indexLastSpace
Expand Down Expand Up @@ -479,7 +497,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(

/**
* @property node node
* @property hasNewLineBefore flag to handle type of comment: ordinary comment(long part of which should be moved to the next line)
* @property hasNewLineBefore flag to handle type of comment: ordinary comment (long part of which should be moved to the next line)
* and inline comments (which should be moved entirely to the previous line)
* @property indexLastSpace index of last space to substring comment
*/
Expand Down Expand Up @@ -525,6 +543,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
private const val STRING_PART_OFFSET = 4
private val propertyList = listOf(INTEGER_CONSTANT, LITERAL_STRING_TEMPLATE_ENTRY, FLOAT_CONSTANT,
CHARACTER_CONSTANT, REFERENCE_EXPRESSION, BOOLEAN_CONSTANT, LONG_STRING_TEMPLATE_ENTRY,
SHORT_STRING_TEMPLATE_ENTRY)
SHORT_STRING_TEMPLATE_ENTRY, NULL)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
fixAndCompare("LongLineExpressionExpected.kt", "LongLineExpressionTest.kt", rulesConfigListLineLength)
}

@Test
fun `should fix complex long binary expressions`() {
fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLineLength)
}

@Test
fun `should fix long function`() {
fixAndCompare("LongLineFunExpected.kt", "LongLineFunTest.kt", rulesConfigListLineLength)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package test.paragraph3.long_line

fun foo() {
val veryLongExpression = Methoooooooooooooooood() +
12345

val veryLongExpression = Methoooooooooooooooood() ?:
null

val veryLongExpression = a.Methooooood() +
b.field

val variable = someField.filter { it.elementType == KDOC }

// limit at the left side
val variable = a?.filter { it.elementType == KDOC } ?:
null

// limit at the right side
val variable = bar?.filter { it.b == c } ?:
null

// limit at the operation reference
val variable = field?.filter { bar == foo } ?:
null

val variable = Methoooooooooooooooooooooooooood() ?:
"some loooooong string"

val variable = Methooooood() ?: "some" +
" looong string"

var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?:
if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package test.paragraph3.long_line

fun foo() {
val veryLongExpression = Methoooooooooooooooood() + 12345

val veryLongExpression = Methoooooooooooooooood() ?: null

val veryLongExpression = a.Methooooood() + b.field

val variable = someField.filter { it.elementType == KDOC }

// limit at the left side
val variable = a?.filter { it.elementType == KDOC } ?: null

// limit at the right side
val variable = bar?.filter { it.b == c } ?: null

// limit at the operation reference
val variable = field?.filter { bar == foo } ?: null

val variable = Methoooooooooooooooooooooooooood() ?: "some loooooong string"

val variable = Methooooood() ?: "some looong string"

var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ fun foo() {
val longStringExpression = "First part" +
"second Part"

val longStringExpression = "First" + "second Part"

val longStringExpression = "First very long" +
" part" + "second Part"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ fun foo() {

val longStringExpression = "First part" + "second Part"

val longStringExpression = "First" + "second Part"

val longStringExpression = "First very long part" + "second Part"

val longStringExpression2 = "String starts at the line len limit"
Expand Down