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 37c2cb0c53..bf8137f944 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 @@ -79,8 +79,9 @@ import java.net.URL /** * The rule checks for lines in the file that exceed the maximum length. - * Rule ignores URL in KDoc. Rule also can fix some cases. - * Rule can fix long binary expressions in condition inside `if` and in property declarations and one line functions + * Rule ignores URL in KDoc. This rule can also fix some particular corner cases. + * This inspection can fix long binary expressions in condition inside `if`, + * in property declarations and in single line functions. */ @Suppress("ForbiddenComment") class LineLength(configRules: List) : DiktatRule( @@ -541,7 +542,12 @@ class LineLength(configRules: List) : DiktatRule( */ private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { - val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE) + val binNode = if (node.elementType == PARENTHESIZED) { + node.findChildByType(BINARY_EXPRESSION) + } else { + node + } + val nodeOperationReference = binNode?.findChildByType(OPERATION_REFERENCE) val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) { nodeOperationReference?.treeNext } else { @@ -551,7 +557,7 @@ class LineLength(configRules: List) : DiktatRule( nodeOperationReference } } - node.appendNewlineMergingWhiteSpace(nextNode, nextNode) + binNode?.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 2322b7910a..ea9e61916e 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 @@ -107,4 +107,9 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fun `fix bin expression first symbol last word`() { fixAndCompare("LongBinaryExpressionLastWordExpected.kt", "LongBinaryExpressionLastWordTest.kt", rulesConfigListErrorLineLength1) } + + @Test + fun `fix bin 1expression first symbol last word`() { + fixAndCompare("LongComplexExpressionExpected.kt", "LongComplexExpressionTest.kt", rulesConfigListErrorLineLength1) + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionExpected.kt new file mode 100644 index 0000000000..f0cd888f35 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionExpected.kt @@ -0,0 +1,9 @@ +package test.paragraph3.long_line + +fun foo(){ + val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || + userRole.isLowerThan(selfRole)) +} + +val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || + userRole.isLowerThan(selfRole)) diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionTest.kt new file mode 100644 index 0000000000..9171fd2998 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongComplexExpressionTest.kt @@ -0,0 +1,7 @@ +package test.paragraph3.long_line + +fun foo(){ + val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || userRole.isLowerThan(selfRole)) +} + +val attrs.disabled = (selfRole == Role.OWNER && isSelfRecord(props.selfUserInfo, user)) || !(selfRole.isHigherOrEqualThan(Role.OWNER) || userRole.isLowerThan(selfRole))