From fb9ad74ebda174bedf56fb60d4e1115bfe4f4569 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Mon, 12 Jul 2021 17:54:09 +0300 Subject: [PATCH 1/6] Debug ### What's done: * Debug --- .../diktat/ruleset/rules/chapter3/LineLength.kt | 7 +++++++ .../diktat/ruleset/chapter3/LineLengthFixTest.kt | 14 ++++++++++++++ .../long_line/LongBinaryExpressionExpected.kt | 5 +++++ .../long_line/LongBinaryExpressionTest.kt | 5 +++++ 4 files changed, 31 insertions(+) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index 4b948826e2..7371ef79d3 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -54,6 +54,7 @@ import org.jetbrains.kotlin.psi.psiUtil.parents import java.net.MalformedURLException import java.net.URL +import kotlin.math.min /** * The rule checks for lines in the file that exceed the maximum length. @@ -86,6 +87,7 @@ class LineLength(configRules: List) : DiktatRule( node.text.lines().forEach { line -> if (line.length > configuration.lineLength) { val newNode = node.psi.findElementAt(offset + configuration.lineLength.toInt() - 1)!!.node + println("NEW_NODE ${newNode.elementType} | ${newNode.text} IN ${node.psi.text.substring(offset + configuration.lineLength.toInt() - 1, minOf(offset + configuration.lineLength.toInt() + 5, offset + line.length))}") if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || !isKdocValid(newNode)) { positionByOffset = node.treeParent.calculateLineColByOffset() @@ -223,6 +225,7 @@ class LineLength(configRules: List) : DiktatRule( if (newParent.hasChildOfType(BINARY_EXPRESSION)) { val leftOffset = positionByOffset(newParent.findChildByType(BINARY_EXPRESSION)!!.startOffset).second val binList: MutableList = mutableListOf() + println("WRONG NODE : ${wrongNode.text}") dfsForProperty(wrongNode, binList) if (binList.size == 1) { return LongLineFixableCases.None @@ -324,6 +327,8 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongLineFixableCases.Condition) { + println("BINARY EXPR") + wrongBinaryExpression.binList.forEach { print(" | ${it.text} | ") } val leftOffset = wrongBinaryExpression.leftOffset val binList = wrongBinaryExpression.binList var binaryText = "" @@ -331,6 +336,8 @@ class LineLength(configRules: List) : DiktatRule( binaryText += findAllText(astNode) if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) { val commonParent = astNode.parent({ it in binList[index - 1].parents() })!! + println("\nASTNODE ${astNode.text}") + println("COMMON PARENT ${commonParent.text}") val nextNode = commonParent.findChildByType(OPERATION_REFERENCE)!!.treeNext if (!nextNode.text.contains("\n")) { commonParent.appendNewlineMergingWhiteSpace(nextNode, nextNode) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt index 6325ed139d..510f427460 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt @@ -7,6 +7,10 @@ import org.cqfn.diktat.util.FixTestBase import org.junit.jupiter.api.Test class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) { + private val rulesConfigListLargeLineLength: List = listOf( + RulesConfig(LONG_LINE.name, true, + mapOf("lineLength" to "180")) + ) private val rulesConfigListDefaultLineLength: List = listOf( RulesConfig(LONG_LINE.name, true, mapOf("lineLength" to "120")) @@ -64,4 +68,14 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fun `should fix annotation`() { fixAndCompare("LongLineAnnotationExpected.kt", "LongLineAnnotationTest.kt", rulesConfigListLineLength) } + + @Test + fun `should fix long binary expression 2, limit 50`() { + fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLineLength) + } + + @Test + fun `should fix long binary expression 2, limit 180`() { + fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLargeLineLength) + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt new file mode 100644 index 0000000000..24611f0fc6 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt @@ -0,0 +1,5 @@ +package test.paragraph3.long_line + +fun foo() { + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null +} \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt new file mode 100644 index 0000000000..2e62e9ed9b --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt @@ -0,0 +1,5 @@ +package test.paragraph3.long_line + +fun foo() { + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null +} From 77cedb255dd8bac5265745c0bd2ce58aa7c9b958 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Tue, 13 Jul 2021 19:00:12 +0300 Subject: [PATCH 2/6] Debug ### What's done: * Debug --- .../ruleset/rules/chapter3/LineLength.kt | 26 ++++++++++++++----- .../long_line/LongBinaryExpressionTest.kt | 2 ++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index 7371ef79d3..598d949a1e 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -17,16 +17,19 @@ import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CONDITION +import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EQ import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.FLOAT_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.FUN +import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL import com.pinterest.ktlint.core.ast.ElementType.IF import com.pinterest.ktlint.core.ast.ElementType.IMPORT_LIST import com.pinterest.ktlint.core.ast.ElementType.INTEGER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.KDOC_MARKDOWN_INLINE_LINK import com.pinterest.ktlint.core.ast.ElementType.KDOC_TEXT +import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION 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 @@ -38,6 +41,7 @@ import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.RPAR +import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE @@ -54,7 +58,6 @@ import org.jetbrains.kotlin.psi.psiUtil.parents import java.net.MalformedURLException import java.net.URL -import kotlin.math.min /** * The rule checks for lines in the file that exceed the maximum length. @@ -227,6 +230,9 @@ class LineLength(configRules: List) : DiktatRule( val binList: MutableList = mutableListOf() println("WRONG NODE : ${wrongNode.text}") dfsForProperty(wrongNode, binList) + print("BINARY EXPR: [") + binList.forEach { print("${it.text} | ") } + print("]\n") if (binList.size == 1) { return LongLineFixableCases.None } @@ -327,17 +333,16 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongLineFixableCases.Condition) { - println("BINARY EXPR") - wrongBinaryExpression.binList.forEach { print(" | ${it.text} | ") } val leftOffset = wrongBinaryExpression.leftOffset val binList = wrongBinaryExpression.binList var binaryText = "" binList.forEachIndexed { index, astNode -> binaryText += findAllText(astNode) + println("binaryText [${binaryText}]") if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) { val commonParent = astNode.parent({ it in binList[index - 1].parents() })!! println("\nASTNODE ${astNode.text}") - println("COMMON PARENT ${commonParent.text}") + println("COMMON PARENT ${commonParent.elementType} | ${commonParent.text}") val nextNode = commonParent.findChildByType(OPERATION_REFERENCE)!!.treeNext if (!nextNode.text.contains("\n")) { commonParent.appendNewlineMergingWhiteSpace(nextNode, nextNode) @@ -377,7 +382,9 @@ class LineLength(configRules: List) : DiktatRule( node = astNode.parent({ newNode -> newNode.nextSibling { it.elementType == OPERATION_REFERENCE } != null }, strict = false) ?: return text + //println("NODE 1 ${node.text}") node = node.nextSibling { it.elementType == OPERATION_REFERENCE }!! + //println("NODE 2 ${node.text}") if (node.treePrev.elementType == WHITE_SPACE) { text += node.treePrev.text } @@ -436,10 +443,17 @@ class LineLength(configRules: List) : DiktatRule( } } + // Collect all children to the right of the equal sign with specific type (propertyList). private fun dfsForProperty(node: ASTNode, binList: MutableList) { node.getChildren(null).forEach { - if (it.elementType in propertyList) { + println("CHILD: ${it.elementType} | ${it.text}") + if (it.elementType in propertyList ){ + //&& it.treeParent.treeParent?.elementType == BINARY_EXPRESSION) { + //&& it.treeParent.treeParent?.elementType != DOT_QUALIFIED_EXPRESSION + //&& it.treeParent.treeParent?.elementType != SAFE_ACCESS_EXPRESSION) { + //println("CHILD: ${it.elementType} | ${it.text}") if (it.elementType == REFERENCE_EXPRESSION && it.treeParent.elementType == CALL_EXPRESSION) { + println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") binList.add(it.treeParent) } else { binList.add(it) @@ -486,7 +500,7 @@ class LineLength(configRules: List) : 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 */ diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt index 2e62e9ed9b..b58735dd78 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt @@ -1,5 +1,7 @@ package test.paragraph3.long_line fun foo() { + val veryLongExpression = Method() + 12345 + baaar() + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null } From 008949476d4514a2f67b0f0fda48067037aa39b1 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Wed, 14 Jul 2021 18:47:47 +0300 Subject: [PATCH 3/6] WIP ### What's done: * WIP --- .../ruleset/rules/chapter3/LineLength.kt | 37 ++++++++++++------- .../long_line/LongBinaryExpressionExpected.kt | 18 ++++++++- .../long_line/LongBinaryExpressionTest.kt | 11 +++++- 3 files changed, 50 insertions(+), 16 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index 598d949a1e..cf7cf2c1ca 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -17,6 +17,7 @@ import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CONDITION +import com.pinterest.ktlint.core.ast.ElementType.DOT import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EQ @@ -90,7 +91,7 @@ class LineLength(configRules: List) : DiktatRule( node.text.lines().forEach { line -> if (line.length > configuration.lineLength) { val newNode = node.psi.findElementAt(offset + configuration.lineLength.toInt() - 1)!!.node - println("NEW_NODE ${newNode.elementType} | ${newNode.text} IN ${node.psi.text.substring(offset + configuration.lineLength.toInt() - 1, minOf(offset + configuration.lineLength.toInt() + 5, offset + line.length))}") + println("\n------------------\n[isFixMode: ${isFixMode}] NEW_NODE ${newNode.elementType} | ${newNode.text} IN ${node.psi.text.substring(offset + configuration.lineLength.toInt() - 1, minOf(offset + configuration.lineLength.toInt() + 5, offset + line.length))}") if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || !isKdocValid(newNode)) { positionByOffset = node.treeParent.calculateLineColByOffset() @@ -220,6 +221,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkProperty(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { + println("Check property") var newParent = wrongNode while (newParent.hasChildOfType(PARENTHESIZED)) { newParent = wrongNode.findChildByType(PARENTHESIZED)!! @@ -329,7 +331,7 @@ class LineLength(configRules: List) : DiktatRule( * This method fix too long binary expression: split after OPERATION_REFERENCE closest to max length * * In this method we collect all binary expression in correct order and then - * we collect their if their length less then max. + * we collect their if their length less than max. */ @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongLineFixableCases.Condition) { @@ -338,7 +340,7 @@ class LineLength(configRules: List) : DiktatRule( var binaryText = "" binList.forEachIndexed { index, astNode -> binaryText += findAllText(astNode) - println("binaryText [${binaryText}]") + println("binaryText [${leftOffset + binaryText.length } vs ${wrongBinaryExpression.maximumLineLength}] [${binaryText}]") if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) { val commonParent = astNode.parent({ it in binList[index - 1].parents() })!! println("\nASTNODE ${astNode.text}") @@ -443,26 +445,33 @@ class LineLength(configRules: List) : DiktatRule( } } - // Collect all children to the right of the equal sign with specific type (propertyList). + // Depth-first search. Collect all children to the right 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 strange private fun dfsForProperty(node: ASTNode, binList: MutableList) { node.getChildren(null).forEach { - println("CHILD: ${it.elementType} | ${it.text}") - if (it.elementType in propertyList ){ - //&& it.treeParent.treeParent?.elementType == BINARY_EXPRESSION) { - //&& it.treeParent.treeParent?.elementType != DOT_QUALIFIED_EXPRESSION - //&& it.treeParent.treeParent?.elementType != SAFE_ACCESS_EXPRESSION) { - //println("CHILD: ${it.elementType} | ${it.text}") - if (it.elementType == REFERENCE_EXPRESSION && it.treeParent.elementType == CALL_EXPRESSION) { - println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") - binList.add(it.treeParent) + //println("CHILD: ${it.elementType} | ${it.text}") + if (it.elementType in propertyList) { + val parentType = it.treeParent?.elementType + if (it.elementType == REFERENCE_EXPRESSION && + (parentType == CALL_EXPRESSION || parentType == DOT_QUALIFIED_EXPRESSION || parentType == SAFE_ACCESS_EXPRESSION) + ) { + //println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") + binList.tryAdd(it.treeParent) } else { - binList.add(it) + //println("CHILD: ${it.elementType} | ${it.text}") + binList.tryAdd(it) } } dfsForProperty(it, binList) } } + private fun MutableList.tryAdd(node: ASTNode) { + if (node !in this && node.treeParent?.elementType == BINARY_EXPRESSION) { + this.add(node) + } + } + private fun createSplitProperty(wrongProperty: LongLineFixableCases.Property) { val node = wrongProperty.node val indexLastSpace = wrongProperty.indexLastSpace diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt index 24611f0fc6..455106b311 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt @@ -1,5 +1,21 @@ package test.paragraph3.long_line fun foo() { - var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null + val veryLongExpression = Methoooooooooooood() + + 12345 + + // limit at the left side + val variable = someField?.let { it.elementType == KDOC } ?: + null + + // limit at the right side + val variable = someField?.let { b == c } ?: + null + + // limit at the operation reference + val variable = someField?.let { bar == fo } ?: + null + + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: + if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null } \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt index b58735dd78..9b671996fd 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt @@ -1,7 +1,16 @@ package test.paragraph3.long_line fun foo() { - val veryLongExpression = Method() + 12345 + baaar() + val veryLongExpression = Methoooooooooooood() + 12345 + + // limit at the left side + val variable = someField?.let { it.elementType == KDOC } ?: null + + // limit at the right side + val variable = someField?.let { b == c } ?: null + + // limit at the operation reference + val variable = someField?.let { bar == fo } ?: null var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null } From 74b607a3e6a8dc1aedf111d01339f2974f56e8fb Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Thu, 15 Jul 2021 17:35:11 +0300 Subject: [PATCH 4/6] WIP ### What's done: * WIP --- .../ruleset/rules/chapter3/LineLength.kt | 33 ++++++++++++++----- .../ruleset/chapter3/LineLengthFixTest.kt | 5 +-- .../long_line/LongBinaryExpressionExpected.kt | 24 +++++++++++--- .../LongBinaryExpressionExpected2.kt | 6 ++++ .../long_line/LongBinaryExpressionTest.kt | 18 +++++++--- .../long_line/LongBinaryExpressionTest2.kt | 6 ++++ .../long_line/LongLineRValueExpected.kt | 2 ++ .../long_line/LongLineRValueTest.kt | 2 ++ 8 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index cf7cf2c1ca..0a7488590d 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -34,6 +34,7 @@ import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION 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 @@ -42,6 +43,7 @@ import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.RPAR +import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE @@ -96,6 +98,7 @@ class LineLength(configRules: List) : DiktatRule( !isKdocValid(newNode)) { positionByOffset = node.treeParent.calculateLineColByOffset() val fixableType = isFixable(newNode, configuration) + println("CAN BE FIXED? ${fixableType != LongLineFixableCases.None}") LONG_LINE.warnAndFix(configRules, emitWarn, isFixMode, "max line length ${configuration.lineLength}, but was ${line.length}", offset + node.startOffset, node, fixableType != LongLineFixableCases.None) { @@ -176,6 +179,7 @@ class LineLength(configRules: List) : DiktatRule( // can't fix this case return LongLineFixableCases.None } + println("DEL INDEX ${delimiterIndex} ${node.text.substring(0, delimiterIndex)}") // check, that space to split is a part of text - not code // If the space split is part of the code, then there is a chance of breaking the code when fixing, that why we should ignore it val isSpaceIsWhiteSpace = node.psi.findElementAt(delimiterIndex)!!.node.isWhiteSpace() @@ -183,12 +187,15 @@ class LineLength(configRules: List) : 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) } @@ -342,8 +349,9 @@ class LineLength(configRules: List) : DiktatRule( binaryText += findAllText(astNode) println("binaryText [${leftOffset + binaryText.length } vs ${wrongBinaryExpression.maximumLineLength}] [${binaryText}]") if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) { - val commonParent = astNode.parent({ it in binList[index - 1].parents() })!! - println("\nASTNODE ${astNode.text}") + println("---") + val commonParent = astNode.parent({ println("${it.elementType} ${it.text} ast ${astNode.elementType}"); it.elementType == BINARY_EXPRESSION && it in binList[index - 1].parents() })!! + println("\nASTNODE ${astNode.elementType} | ${astNode.text}") println("COMMON PARENT ${commonParent.elementType} | ${commonParent.text}") val nextNode = commonParent.findChildByType(OPERATION_REFERENCE)!!.treeNext if (!nextNode.text.contains("\n")) { @@ -384,9 +392,9 @@ class LineLength(configRules: List) : DiktatRule( node = astNode.parent({ newNode -> newNode.nextSibling { it.elementType == OPERATION_REFERENCE } != null }, strict = false) ?: return text - //println("NODE 1 ${node.text}") + node = node.nextSibling { it.elementType == OPERATION_REFERENCE }!! - //println("NODE 2 ${node.text}") + if (node.treePrev.elementType == WHITE_SPACE) { text += node.treePrev.text } @@ -453,12 +461,18 @@ class LineLength(configRules: List) : DiktatRule( if (it.elementType in propertyList) { val parentType = it.treeParent?.elementType if (it.elementType == REFERENCE_EXPRESSION && - (parentType == CALL_EXPRESSION || parentType == DOT_QUALIFIED_EXPRESSION || parentType == SAFE_ACCESS_EXPRESSION) + (parentType == CALL_EXPRESSION) + //|| parentType == DOT_QUALIFIED_EXPRESSION || parentType == SAFE_ACCESS_EXPRESSION) ) { //println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") - binList.tryAdd(it.treeParent) + //if (it.treeParent?.treeParent?.elementType != DOT_QUALIFIED_EXPRESSION && it.treeParent?.treeParent?.elementType != SAFE_ACCESS_EXPRESSION) { + //println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") + //println("GRAND PAR: ${it.treeParent.treeParent.elementType} | ${it.treeParent.treeParent.text}") + binList.tryAdd(it.treeParent) + //} } else { //println("CHILD: ${it.elementType} | ${it.text}") + //binList.add(it) binList.tryAdd(it) } } @@ -467,7 +481,8 @@ class LineLength(configRules: List) : DiktatRule( } private fun MutableList.tryAdd(node: ASTNode) { - if (node !in this && node.treeParent?.elementType == BINARY_EXPRESSION) { + //println(node.treeParent?.elementType) + if (node !in this) { this.add(node) } } @@ -555,6 +570,6 @@ class LineLength(configRules: List) : 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) } } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt index 510f427460..43b6e1f34b 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt @@ -70,12 +70,13 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) } @Test - fun `should fix long binary expression 2, limit 50`() { + fun `should fix complex long binary expressions`() { fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLineLength) } @Test fun `should fix long binary expression 2, limit 180`() { - fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLargeLineLength) + //fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLargeLineLength) + fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLineLength) } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt index 455106b311..94c5f6f895 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected.kt @@ -1,21 +1,35 @@ package test.paragraph3.long_line fun foo() { - val veryLongExpression = Methoooooooooooood() + + 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 = someField?.let { it.elementType == KDOC } ?: + val variable = a?.filter { it.elementType == KDOC } ?: null // limit at the right side - val variable = someField?.let { b == c } ?: + val variable = bar?.filter { it.b == c } ?: null // limit at the operation reference - val variable = someField?.let { bar == fo } ?: + 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 -} \ No newline at end of file +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt new file mode 100644 index 0000000000..81eb03d303 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt @@ -0,0 +1,6 @@ +package test.paragraph3.long_line + +fun foo() { + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: + if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null +} \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt index 9b671996fd..2f14386194 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest.kt @@ -1,16 +1,26 @@ package test.paragraph3.long_line fun foo() { - val veryLongExpression = Methoooooooooooood() + 12345 + 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 = someField?.let { it.elementType == KDOC } ?: null + val variable = a?.filter { it.elementType == KDOC } ?: null // limit at the right side - val variable = someField?.let { b == c } ?: null + val variable = bar?.filter { it.b == c } ?: null // limit at the operation reference - val variable = someField?.let { bar == fo } ?: null + 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 } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt new file mode 100644 index 0000000000..ffe89265fc --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt @@ -0,0 +1,6 @@ +package test.paragraph3.long_line + +fun foo() { + var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: + if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueExpected.kt index a2c047bbd7..2fd0dfcf09 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueExpected.kt @@ -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" diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueTest.kt index 346e4838a1..fa6d6c1881 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineRValueTest.kt @@ -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" From 56cf4fad36157d6edbb8b1b7a2df8147408782ea Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Fri, 16 Jul 2021 12:35:37 +0300 Subject: [PATCH 5/6] Polish ### What's done: * Polish --- .../ruleset/rules/chapter3/LineLength.kt | 51 +++++-------------- .../ruleset/chapter3/LineLengthFixTest.kt | 20 ++------ .../LongBinaryExpressionExpected2.kt | 6 --- .../long_line/LongBinaryExpressionTest2.kt | 6 --- 4 files changed, 17 insertions(+), 66 deletions(-) delete mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt delete mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index 0a7488590d..90a355d4da 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -17,20 +17,16 @@ import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CONDITION -import com.pinterest.ktlint.core.ast.ElementType.DOT -import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EQ import com.pinterest.ktlint.core.ast.ElementType.FILE import com.pinterest.ktlint.core.ast.ElementType.FLOAT_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.FUN -import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL import com.pinterest.ktlint.core.ast.ElementType.IF import com.pinterest.ktlint.core.ast.ElementType.IMPORT_LIST import com.pinterest.ktlint.core.ast.ElementType.INTEGER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.KDOC_MARKDOWN_INLINE_LINK import com.pinterest.ktlint.core.ast.ElementType.KDOC_TEXT -import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION 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 @@ -43,8 +39,6 @@ import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.RPAR -import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS -import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE @@ -93,12 +87,10 @@ class LineLength(configRules: List) : DiktatRule( node.text.lines().forEach { line -> if (line.length > configuration.lineLength) { val newNode = node.psi.findElementAt(offset + configuration.lineLength.toInt() - 1)!!.node - println("\n------------------\n[isFixMode: ${isFixMode}] NEW_NODE ${newNode.elementType} | ${newNode.text} IN ${node.psi.text.substring(offset + configuration.lineLength.toInt() - 1, minOf(offset + configuration.lineLength.toInt() + 5, offset + line.length))}") if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || !isKdocValid(newNode)) { positionByOffset = node.treeParent.calculateLineColByOffset() val fixableType = isFixable(newNode, configuration) - println("CAN BE FIXED? ${fixableType != LongLineFixableCases.None}") LONG_LINE.warnAndFix(configRules, emitWarn, isFixMode, "max line length ${configuration.lineLength}, but was ${line.length}", offset + node.startOffset, node, fixableType != LongLineFixableCases.None) { @@ -179,7 +171,6 @@ class LineLength(configRules: List) : DiktatRule( // can't fix this case return LongLineFixableCases.None } - println("DEL INDEX ${delimiterIndex} ${node.text.substring(0, delimiterIndex)}") // check, that space to split is a part of text - not code // If the space split is part of the code, then there is a chance of breaking the code when fixing, that why we should ignore it val isSpaceIsWhiteSpace = node.psi.findElementAt(delimiterIndex)!!.node.isWhiteSpace() @@ -228,7 +219,6 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION") private fun checkProperty(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { - println("Check property") var newParent = wrongNode while (newParent.hasChildOfType(PARENTHESIZED)) { newParent = wrongNode.findChildByType(PARENTHESIZED)!! @@ -237,11 +227,7 @@ class LineLength(configRules: List) : DiktatRule( if (newParent.hasChildOfType(BINARY_EXPRESSION)) { val leftOffset = positionByOffset(newParent.findChildByType(BINARY_EXPRESSION)!!.startOffset).second val binList: MutableList = mutableListOf() - println("WRONG NODE : ${wrongNode.text}") dfsForProperty(wrongNode, binList) - print("BINARY EXPR: [") - binList.forEach { print("${it.text} | ") } - print("]\n") if (binList.size == 1) { return LongLineFixableCases.None } @@ -338,7 +324,7 @@ class LineLength(configRules: List) : DiktatRule( * This method fix too long binary expression: split after OPERATION_REFERENCE closest to max length * * In this method we collect all binary expression in correct order and then - * we collect their if their length less than max. + * we collect their if their length less then max. */ @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongLineFixableCases.Condition) { @@ -347,12 +333,8 @@ class LineLength(configRules: List) : DiktatRule( var binaryText = "" binList.forEachIndexed { index, astNode -> binaryText += findAllText(astNode) - println("binaryText [${leftOffset + binaryText.length } vs ${wrongBinaryExpression.maximumLineLength}] [${binaryText}]") if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) { - println("---") - val commonParent = astNode.parent({ println("${it.elementType} ${it.text} ast ${astNode.elementType}"); it.elementType == BINARY_EXPRESSION && it in binList[index - 1].parents() })!! - println("\nASTNODE ${astNode.elementType} | ${astNode.text}") - println("COMMON PARENT ${commonParent.elementType} | ${commonParent.text}") + 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) @@ -392,9 +374,7 @@ class LineLength(configRules: List) : DiktatRule( node = astNode.parent({ newNode -> newNode.nextSibling { it.elementType == OPERATION_REFERENCE } != null }, strict = false) ?: return text - node = node.nextSibling { it.elementType == OPERATION_REFERENCE }!! - if (node.treePrev.elementType == WHITE_SPACE) { text += node.treePrev.text } @@ -453,26 +433,20 @@ class LineLength(configRules: List) : DiktatRule( } } - // Depth-first search. Collect all children to the right 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 strange + /** + * 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) { node.getChildren(null).forEach { - //println("CHILD: ${it.elementType} | ${it.text}") if (it.elementType in propertyList) { - val parentType = it.treeParent?.elementType - if (it.elementType == REFERENCE_EXPRESSION && - (parentType == CALL_EXPRESSION) - //|| parentType == DOT_QUALIFIED_EXPRESSION || parentType == SAFE_ACCESS_EXPRESSION) - ) { - //println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") - //if (it.treeParent?.treeParent?.elementType != DOT_QUALIFIED_EXPRESSION && it.treeParent?.treeParent?.elementType != SAFE_ACCESS_EXPRESSION) { - //println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}") - //println("GRAND PAR: ${it.treeParent.treeParent.elementType} | ${it.treeParent.treeParent.text}") - binList.tryAdd(it.treeParent) - //} + if (it.elementType == REFERENCE_EXPRESSION && it.treeParent?.elementType == CALL_EXPRESSION) { + binList.tryAdd(it.treeParent) } else { - //println("CHILD: ${it.elementType} | ${it.text}") - //binList.add(it) binList.tryAdd(it) } } @@ -481,7 +455,6 @@ class LineLength(configRules: List) : DiktatRule( } private fun MutableList.tryAdd(node: ASTNode) { - //println(node.treeParent?.elementType) if (node !in this) { this.add(node) } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt index 43b6e1f34b..3b987b50a5 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt @@ -7,10 +7,6 @@ import org.cqfn.diktat.util.FixTestBase import org.junit.jupiter.api.Test class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) { - private val rulesConfigListLargeLineLength: List = listOf( - RulesConfig(LONG_LINE.name, true, - mapOf("lineLength" to "180")) - ) private val rulesConfigListDefaultLineLength: List = listOf( RulesConfig(LONG_LINE.name, true, mapOf("lineLength" to "120")) @@ -44,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) @@ -68,15 +69,4 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fun `should fix annotation`() { fixAndCompare("LongLineAnnotationExpected.kt", "LongLineAnnotationTest.kt", rulesConfigListLineLength) } - - @Test - fun `should fix complex long binary expressions`() { - fixAndCompare("LongBinaryExpressionExpected.kt", "LongBinaryExpressionTest.kt", rulesConfigListLineLength) - } - - @Test - fun `should fix long binary expression 2, limit 180`() { - //fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLargeLineLength) - fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLineLength) - } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt deleted file mode 100644 index 81eb03d303..0000000000 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionExpected2.kt +++ /dev/null @@ -1,6 +0,0 @@ -package test.paragraph3.long_line - -fun foo() { - var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: - if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null -} \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt deleted file mode 100644 index ffe89265fc..0000000000 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongBinaryExpressionTest2.kt +++ /dev/null @@ -1,6 +0,0 @@ -package test.paragraph3.long_line - -fun foo() { - var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC } ?: - if (firstCodeNode == packageDirectiveNode) importsList?.prevSibling { it.elementType == KDOC } else null -} From ea4ca15d8f3782d5c49d1365df4596fec4cdbc84 Mon Sep 17 00:00:00 2001 From: Kirill Gevorkyan <26010098+kgevorkyan@users.noreply.github.com> Date: Fri, 16 Jul 2021 17:46:44 +0300 Subject: [PATCH 6/6] Review ### What's done: * Review --- .../cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt index 90a355d4da..6c7d94301f 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt @@ -445,21 +445,15 @@ class LineLength(configRules: List) : DiktatRule( node.getChildren(null).forEach { if (it.elementType in propertyList) { if (it.elementType == REFERENCE_EXPRESSION && it.treeParent?.elementType == CALL_EXPRESSION) { - binList.tryAdd(it.treeParent) + binList.add(it.treeParent) } else { - binList.tryAdd(it) + binList.add(it) } } dfsForProperty(it, binList) } } - private fun MutableList.tryAdd(node: ASTNode) { - if (node !in this) { - this.add(node) - } - } - private fun createSplitProperty(wrongProperty: LongLineFixableCases.Property) { val node = wrongProperty.node val indexLastSpace = wrongProperty.indexLastSpace