From 0d62a5fe372d5fee69d5155cce23960cc6e2d864 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Mon, 16 May 2022 16:36:45 +0300 Subject: [PATCH 01/52] ### Whats added: * Start logic to fix and warn String Template in Linelength rule --- .../ruleset/rules/chapter3/LineLength.kt | 38 +++++++++++++++---- 1 file changed, 31 insertions(+), 7 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 5bfd51aaa5..5573a6402c 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 @@ -5,13 +5,6 @@ import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE import org.cqfn.diktat.ruleset.rules.DiktatRule -import org.cqfn.diktat.ruleset.utils.KotlinParser -import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace -import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset -import org.cqfn.diktat.ruleset.utils.findAllNodesWithConditionOnLine -import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType -import org.cqfn.diktat.ruleset.utils.getLineNumber -import org.cqfn.diktat.ruleset.utils.hasChildOfType import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION @@ -49,6 +42,7 @@ import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextSibling import com.pinterest.ktlint.core.ast.parent import com.pinterest.ktlint.core.ast.prevSibling +import org.cqfn.diktat.ruleset.utils.* import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement @@ -135,12 +129,42 @@ class LineLength(configRules: List) : DiktatRule( } } ?: return checkStringTemplate(parent, configuration) } + STRING_TEMPLATE -> { + parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { + if (searchRightSplitInBinaryExpression(parent, configuration)?.second) + parent = it + } ?: run { + val parentFun = parent.findParentNodeWithSpecificType(FUN) + val parentProperty = parent.findParentNodeWithSpecificType(PROPERTY) + return checkStringTemplate1(parent, configuration, parentProperty ?: parentFun ) + } + } else -> parent = parent.treeParent } } while (parent.treeParent != null) return LongLineFixableCases.None } + private fun createClassForStringTeplate(node:ASTNode, configuration: LineLengthConfiguration) { + + } + + private fun checkStringTemplate1(node: ASTNode, configuration: LineLengthConfiguration, FunOrPropertyNode : ASTNode?) : LongLineFixableCases { + FunOrPropertyNode ?.let { + if (it.hasChildOfType(EQ)) { + val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)!!.startOffset).second + if (positionByOffset > configuration.lineLength / 2) + return FunAndProperty(it) + } else + return createClassForStringTeplate(node, configuration) + } ?: return createClassForStringTeplate(node, configuration) + } + + воавопщшю.iojgfjiogj + jfdgnfdkjg.fdioj.fdg..dfg.fd.gdf.df.gdf. + + fjhghihj.fmkghjiogf + fkjgfogh + .fkpgfhkpogfh.dfhggh.fdh.gfh..fg.hgf.h.fg. + /** * This class finds where the string can be split * From c00df8bb8be3e557f258e3e4517c0a23ee0b3ed0 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Mon, 16 May 2022 19:03:22 +0300 Subject: [PATCH 02/52] ### Whats added: * Start logic to fix and warn String Template in Linelength rule --- .../ruleset/rules/chapter3/LineLength.kt | 140 ++++++++++++------ 1 file changed, 95 insertions(+), 45 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 56cfa8a734..80b8debde2 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 @@ -8,11 +8,13 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.* import com.pinterest.ktlint.core.ast.ElementType.ANDAND -import com.pinterest.ktlint.core.ast.ElementType.ANNOTATION_ENTRY +import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT 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.ELVIS import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.EQ @@ -25,7 +27,6 @@ import com.pinterest.ktlint.core.ast.ElementType.FUN import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL import com.pinterest.ktlint.core.ast.ElementType.GT import com.pinterest.ktlint.core.ast.ElementType.GTEQ -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 @@ -33,6 +34,7 @@ import com.pinterest.ktlint.core.ast.ElementType.KDOC_TEXT import com.pinterest.ktlint.core.ast.ElementType.LBRACE 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.LT import com.pinterest.ktlint.core.ast.ElementType.LTEQ import com.pinterest.ktlint.core.ast.ElementType.NULL @@ -45,8 +47,10 @@ import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RBRACE import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.RPAR 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.VALUE_ARGUMENT_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline @@ -58,6 +62,7 @@ import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType +import org.jetbrains.kotlin.psi.parameterRecursiveVisitor import org.jetbrains.kotlin.psi.psiUtil.parents import java.net.MalformedURLException @@ -88,6 +93,7 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } + println(node.text) } } @@ -128,7 +134,7 @@ class LineLength(configRules: List) : DiktatRule( do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - val splitOffset = searchRightSplitInBinaryExpression(parent, configuration)?.second + val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second splitOffset?.let { if (isConditionToUpAnalysisBinExpression(parent, splitOffset)) { parent = parent.treeParent @@ -140,33 +146,27 @@ class LineLength(configRules: List) : DiktatRule( } FUN, PROPERTY -> return checkFunAndProperty(parent) CONDITION -> return checkCondition(parent, configuration) + VALUE_ARGUMENT_LIST -> { + parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { + parent = it + } ?: return checkArgumentList(parent, configuration) + } EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) - STRING_TEMPLATE -> { - // as we are going from bottom to top we are excluding - // 1. IF, because it seems that string template is in condition - // 2. FUN with EQ, it seems that new line should be inserted after `=` - parent.findParentNodeWithSpecificType(IF)?.let { - parent = parent.treeParent - } ?: parent.findParentNodeWithSpecificType(FUN)?.let { node -> - // checking that string template is not in annotation - if (node.hasChildOfType(EQ) && !wrongNode.parents().any { it.elementType == ANNOTATION_ENTRY }) { - parent = node - } else { - return checkStringTemplate(parent, configuration) - } - } ?: return checkStringTemplate(parent, configuration) - } - STRING_TEMPLATE -> { + STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { - if (searchRightSplitInBinaryExpression(parent, configuration)?.second != null) - parent = it - } ?: run { - val parentFun = parent.findParentNodeWithSpecificType(FUN) - val parentProperty = parent.findParentNodeWithSpecificType(PROPERTY) - return checkStringTemplate1(parent, configuration, parentProperty ?: parentFun) - } + parent = it + } ?: parent.findParentNodeWithSpecificType(VALUE_ARGUMENT_LIST) ?. let { + parent = it + } ?: run { + val parentFun = parent.findParentNodeWithSpecificType(FUN) + val parentProperty = parent.findParentNodeWithSpecificType(PROPERTY) + val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration, parentProperty ?: parentFun) + if(returnElem !is None) + return returnElem + parent = parent.treeParent } + } else -> parent = parent.treeParent } } while (parent.treeParent != null) @@ -196,21 +196,28 @@ class LineLength(configRules: List) : DiktatRule( return LongBinaryExpression(node, configuration, leftOffset, binList) } - private fun createClassForStringTemplate(node:ASTNode, configuration: LineLengthConfiguration) { - - } - - private fun checkStringTemplate1(node: ASTNode, configuration: LineLengthConfiguration, FunOrPropertyNode : ASTNode?) : LongLineFixableCases { - FunOrPropertyNode ?.let { + private fun checkStringTemplateAndDotQualifiedExpression(node: ASTNode, configuration: LineLengthConfiguration, funOrPropertyNode : ASTNode?) : LongLineFixableCases { + funOrPropertyNode ?.let { if (it.hasChildOfType(EQ)) { val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)!!.startOffset).second - if (positionByOffset > configuration.lineLength / 2) - return FunAndProperty(it) - } else - return createClassForStringTemplate(node, configuration) - } ?: return createClassForStringTemplate(node, configuration) + if (positionByOffset < configuration.lineLength / 2) { + val returnedClass = parserStringAndDot(node, configuration) + if (returnedClass !is None) + return returnedClass + } + return FunAndProperty(it) + } + return parserStringAndDot(node, configuration) + } ?: + return parserStringAndDot(node, configuration) } + private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -220,7 +227,7 @@ class LineLength(configRules: List) : DiktatRule( * None - if the string can't be split */ @Suppress("TOO_LONG_FUNCTION", "UnsafeCallOnNullableType") - private fun checkStringTemplate(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { + private fun parserStringTemplate(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { var multiLineOffset = 0 val leftOffset = if (node.text.lines().size > 1) { node @@ -266,6 +273,22 @@ class LineLength(configRules: List) : DiktatRule( return StringTemplate(node, correcterDelimiter, multiLineOffset == 0) } + private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration) : LongLineFixableCases { + val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT)?.first + nodeDot ?. let { + return DotQualifiedExpression(wrongNode, it) + } ?: + return None() + } + + private fun checkArgumentList(wrongNode: ASTNode, configuration: LineLengthConfiguration) : LongLineFixableCases { + val node = searchRightSplitAfterType(wrongNode, configuration, COMMA)?.first + node ?. let { + return ValueArgumentList(wrongNode, node) + } + return ValueArgumentList(wrongNode) + } + private fun checkFunAndProperty(wrongNode: ASTNode) = if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() @@ -313,11 +336,33 @@ class LineLength(configRules: List) : DiktatRule( is LongBinaryExpression -> fixLongBinaryExpression(fixableType) is BinaryExpression -> fixBinaryExpression(fixableType.node) is StringTemplate -> fixStringTemplate(fixableType) + is DotQualifiedExpression -> fixDotQualifiedExpression(fixableType) + is ValueArgumentList -> fixArgumentList(fixableType) is Lambda -> fixLambda(fixableType.node) is None -> return } } + private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { + val node = wrongDotQualifiedExpression.node + val dot = wrongDotQualifiedExpression.nodeForSplit + node.appendNewlineMergingWhiteSpace(dot,dot) + } + private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { + val node = wrongArgumentList.node + val comma = wrongArgumentList.nodeForSplit + comma ?. let { + node.appendNewlineMergingWhiteSpace(comma, comma) + } ?: run { + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR)!!.treePrev, node.findChildByType(RPAR)!!.treePrev) + node.getChildren(null).forEach { elem-> + if (elem.elementType == COMMA && elem.treeNext.elementType != RPAR) + node.appendNewlineMergingWhiteSpace(elem.treeNext, elem.treeNext) + } + } + } + private fun fixComment(wrongComment: Comment) { val wrongNode = wrongComment.node if (wrongComment.hasNewLineBefore) { @@ -492,16 +537,16 @@ class LineLength(configRules: List) : DiktatRule( * Finds the first binary expression closer to the separator */ @Suppress("UnsafeCallOnNullableType") - private fun searchRightSplitInBinaryExpression(parent: ASTNode, configuration: LineLengthConfiguration): Pair? { + private fun searchRightSplitAfterType(parent: ASTNode, configuration: LineLengthConfiguration, type: IElementType): Pair? { val binList: MutableList = mutableListOf() searchBinaryExpression(parent, binList) return binList.map { - it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + it to positionByOffset(it.getFirstChildWithType(type)!!.startOffset).second } .sortedBy { it.second } .reversed() .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 + offset + (it.getFirstChildWithType(type)?.text!!.length ?: 0) <= configuration.lineLength + 1 } } @@ -582,7 +627,7 @@ class LineLength(configRules: List) : DiktatRule( ) : LongLineFixableCases(node) /** - * Class Fun show that long line should be split in Fun: after EQ (between head and body this function) + * Class FunAndProperty show that long line should be split in Fun Or Property: after EQ (between head and body this function) */ private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) @@ -592,11 +637,16 @@ class LineLength(configRules: List) : DiktatRule( private class Lambda(node: ASTNode) : LongLineFixableCases(node) /** - * Class Property show that long line should be split in property: after a EQ - * @property indexLastSpace - * @property text + * Class DotQualifiedExpression show that line should be split in DotQualifiedExpression after node [nodeForSplit] */ + private class DotQualifiedExpression(node: ASTNode, val nodeForSplit: ASTNode) : LongLineFixableCases(node) + /** + * Class ValueArgumentList show that line should be split in ValueArgumentList: + * If [nodeForSplit] !is null that node should be split after this COMMA + * Else ([nodeForSplit] is null) - node should be split after LPAR, before RPAR and after all COMMA + */ + private class ValueArgumentList(node: ASTNode, val nodeForSplit: ASTNode? = null) : LongLineFixableCases(node) /** * val text = "first part" + * "second part" + From 5e521176d40d8853f14c816ba62361fedb588421 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Fri, 20 May 2022 18:56:15 +0300 Subject: [PATCH 03/52] ### Whats added: * Start logic to fix and warn String Template in Linelength rule --- .../ruleset/rules/chapter3/LineLength.kt | 101 ++++++++++++------ .../cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 4 +- .../ruleset/chapter3/LineLengthFixTest.kt | 16 ++- .../ruleset/chapter3/LineLengthWarnTest.kt | 9 +- .../long_line/LongBinaryExpressionExpected.kt | 4 +- .../LongDotQualifiedExpressionExpected.kt | 4 + .../long_line/LongLineAnnotationExpected.kt | 8 +- .../long_line/LongLineFunExpected.kt | 9 +- .../long_line/LongLineRValueExpected.kt | 11 +- .../long_line/LongShortRValueExpected.kt | 3 +- 10 files changed, 117 insertions(+), 52 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.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 80b8debde2..7b3679db9d 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,16 +54,10 @@ import com.pinterest.ktlint.core.ast.ElementType.VALUE_ARGUMENT_LIST import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline -import com.pinterest.ktlint.core.ast.nextSibling -import com.pinterest.ktlint.core.ast.parent -import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode -import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType -import org.jetbrains.kotlin.psi.parameterRecursiveVisitor -import org.jetbrains.kotlin.psi.psiUtil.parents import java.net.MalformedURLException import java.net.URL @@ -131,18 +125,34 @@ class LineLength(configRules: List) : DiktatRule( ) private fun isFixable(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { var parent = wrongNode + var stringOrDot : ASTNode? = null do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second - splitOffset?.let { - if (isConditionToUpAnalysisBinExpression(parent, splitOffset)) { - parent = parent.treeParent - } else { - return checkBinaryExpression(parent, configuration) + parent.findParentNodeWithSpecificType(VALUE_ARGUMENT_LIST) ?. let { + parent = it + } ?: parent.findParentNodeWithSpecificType(FUNCTION_LITERAL) ?. let { + parent = it + } ?: run { + val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second + splitOffset?.let { + val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) + val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) + if (parentIsBiExprOrParenthesized) { + parent = parent.treeParent + } else if (parentIsFunOrProperty && splitOffset >= configuration.lineLength) { + if (stringOrDot != null) { + val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration) + if (returnElem !is None) + return returnElem + } + parent = parent.treeParent + } else { + return checkBinaryExpression(parent, configuration) + } } + ?: run { parent = parent.treeParent } } - ?: run { parent = parent.treeParent } } FUN, PROPERTY -> return checkFunAndProperty(parent) CONDITION -> return checkCondition(parent, configuration) @@ -154,6 +164,7 @@ class LineLength(configRules: List) : DiktatRule( EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { + stringOrDot = parent parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { parent = it } ?: parent.findParentNodeWithSpecificType(VALUE_ARGUMENT_LIST) ?. let { @@ -178,9 +189,8 @@ class LineLength(configRules: List) : DiktatRule( */ private fun isConditionToUpAnalysisBinExpression(parent: ASTNode, offset: Int): Boolean { val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) - val parentIsFunctionLiteral = parent.treeParent.treeParent.elementType == FUNCTION_LITERAL val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) - return (parentIsBiExprOrParenthesized || parentIsFunctionLiteral || (parentIsFunOrProperty && offset >= configuration.lineLength)) + return (parentIsBiExprOrParenthesized || (parentIsFunOrProperty && offset >= configuration.lineLength)) } /** @@ -196,7 +206,7 @@ class LineLength(configRules: List) : DiktatRule( return LongBinaryExpression(node, configuration, leftOffset, binList) } - private fun checkStringTemplateAndDotQualifiedExpression(node: ASTNode, configuration: LineLengthConfiguration, funOrPropertyNode : ASTNode?) : LongLineFixableCases { + private fun checkStringTemplateAndDotQualifiedExpression(node: ASTNode, configuration: LineLengthConfiguration, funOrPropertyNode : ASTNode? = null) : LongLineFixableCases { funOrPropertyNode ?.let { if (it.hasChildOfType(EQ)) { val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)!!.startOffset).second @@ -276,7 +286,7 @@ class LineLength(configRules: List) : DiktatRule( private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration) : LongLineFixableCases { val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT)?.first nodeDot ?. let { - return DotQualifiedExpression(wrongNode, it) + return DotQualifiedExpression(wrongNode) } ?: return None() } @@ -345,17 +355,18 @@ class LineLength(configRules: List) : DiktatRule( private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node - val dot = wrongDotQualifiedExpression.nodeForSplit + val dot = node.getFirstChildWithType(DOT) node.appendNewlineMergingWhiteSpace(dot,dot) } private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { val node = wrongArgumentList.node - val comma = wrongArgumentList.nodeForSplit + val nodeWithComma = wrongArgumentList.nodeForSplit + val comma = nodeWithComma?.getFirstChildWithType(COMMA) comma ?. let { - node.appendNewlineMergingWhiteSpace(comma, comma) + nodeWithComma.appendNewlineMergingWhiteSpace(comma, comma) } ?: run { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR)!!.treePrev, node.findChildByType(RPAR)!!.treePrev) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) node.getChildren(null).forEach { elem-> if (elem.elementType == COMMA && elem.treeNext.elementType != RPAR) node.appendNewlineMergingWhiteSpace(elem.treeNext, elem.treeNext) @@ -426,13 +437,13 @@ class LineLength(configRules: List) : DiktatRule( val incorrectText = wrongStringTemplate.node.text val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex) val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length) - val textBetwenParts = + val textBetweenParts = if (wrongStringTemplate.isOneLineString) { "\" +\n\"" } else { "\n" } - val correctNode = KotlinParser().createNode("$firstPart$textBetwenParts$secondPart") + val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, correctNode) } @@ -485,6 +496,32 @@ class LineLength(configRules: List) : DiktatRule( } } + private fun searchComma(node: ASTNode, commaList: MutableList) { + if (node.elementType == VALUE_ARGUMENT_LIST){ + node.getChildren(null) + .filter { + it.elementType == VALUE_ARGUMENT_LIST + } + .forEach { + searchComma(it, commaList) + } + commaList.add(node) + } + } + + private fun searchDot(node: ASTNode, DotList: MutableList) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION){ + node.getChildren(null) + .filter { + it.elementType == DOT_QUALIFIED_EXPRESSION + } + .forEach { + searchDot(it, DotList) + } + DotList.add(node) + } + } + /** * This method stored all the nodes that have BINARY_EXPRESSION or PREFIX_EXPRESSION element type. * Return List of the Pair @@ -538,15 +575,19 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun searchRightSplitAfterType(parent: ASTNode, configuration: LineLengthConfiguration, type: IElementType): Pair? { - val binList: MutableList = mutableListOf() - searchBinaryExpression(parent, binList) - return binList.map { - it to positionByOffset(it.getFirstChildWithType(type)!!.startOffset).second + val list: MutableList = mutableListOf() + when (type) { + OPERATION_REFERENCE -> searchBinaryExpression(parent, list) + COMMA -> searchComma(parent, list) + DOT -> searchDot(parent, list) + } + return list.map { + it to positionByOffset(it.getFirstChildWithType(type)?.startOffset ?: configuration.lineLength.toInt()).second } .sortedBy { it.second } .reversed() .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(type)?.text!!.length ?: 0) <= configuration.lineLength + 1 + offset + (it.getFirstChildWithType(type)?.text?.length ?: 0) <= configuration.lineLength + 1 } } @@ -637,9 +678,9 @@ class LineLength(configRules: List) : DiktatRule( private class Lambda(node: ASTNode) : LongLineFixableCases(node) /** - * Class DotQualifiedExpression show that line should be split in DotQualifiedExpression after node [nodeForSplit] + * Class DotQualifiedExpression show that line should be split in DotQualifiedExpression */ - private class DotQualifiedExpression(node: ASTNode, val nodeForSplit: ASTNode) : LongLineFixableCases(node) + private class DotQualifiedExpression(node: ASTNode) : LongLineFixableCases(node) /** * Class ValueArgumentList show that line should be split in ValueArgumentList: diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt index 8a5ef64524..e1205cd49b 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt @@ -552,7 +552,9 @@ fun ASTNode.leaveExactlyNumNewLines(num: Int) { */ fun ASTNode.appendNewlineMergingWhiteSpace(whiteSpaceNode: ASTNode?, beforeNode: ASTNode?) { if (whiteSpaceNode != null && whiteSpaceNode.elementType == WHITE_SPACE) { - (whiteSpaceNode as LeafPsiElement).rawReplaceWithText("\n${whiteSpaceNode.text}") + if (whiteSpaceNode.text.lines().size == 1) { + (whiteSpaceNode as LeafPsiElement).rawReplaceWithText("\n${whiteSpaceNode.text}") + } } else { addChild(PsiWhiteSpaceImpl("\n"), beforeNode) } 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 8dfc008cf4..e1a79d7a85 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,8 +70,13 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) } @Test - fun `shouldn't fix`() { - fixAndCompare("LongExpressionNoFixExpected.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) + fun `shouldn't fix1`() { + fixAndCompare("LongExpressionNoFixExpected1.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) + } + + @Test + fun `shouldn't fix2`() { + fixAndCompare("LongExpressionNoFixExpected2.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) } @Test @@ -88,4 +93,11 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fun `fix expression in condition`() { fixAndCompare("LongExpressionInConditionExpected.kt", "LongExpressionInConditionTest.kt", rulesConfigListLineLength) } + + @Test + fun `fix long Dot Qualified Expression`() { + fixAndCompare("LongDotQualifiedExpressionExpected.kt", "LongDotQualifiedExpressionTest.kt", rulesConfigListLineLength) + } + + } diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthWarnTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthWarnTest.kt index 7eec5ab34b..28311e8b87 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthWarnTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthWarnTest.kt @@ -141,8 +141,8 @@ class LineLengthWarnTest : LintTestBase(::LineLength) { | } |} """.trimMargin(), - LintError(14, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 143", false), - LintError(18, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 142", false) + LintError(14, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 143", true), + LintError(18, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 142", true) ) } @@ -223,7 +223,8 @@ class LineLengthWarnTest : LintTestBase(::LineLength) { |} """.trimMargin(), LintError(8, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 130", false), - LintError(9, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 123", false) + LintError(9, 1, ruleId, "${LONG_LINE.warnText()} max line length 120, but was 123", true) + ) } @@ -235,7 +236,7 @@ class LineLengthWarnTest : LintTestBase(::LineLength) { |@Query(value = "ASDAASDASDASDASDASDASDASDAASDASDASDASDASDASDASDAASDASDASDASDASDASD") |fun foo() = println("ASDAASDASDASDASDASDASDASDAASDASDASDASDASDASDASDAASDASDASDASDASDASD") """.trimMargin(), - LintError(1, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 84", false), + LintError(1, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 84", true), LintError(2, 1, ruleId, "${LONG_LINE.warnText()} max line length 40, but was 89", true), rulesConfigList = shortLineLength ) 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 432b811517..abc66c3d7c 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 @@ -39,8 +39,8 @@ fun foo() { val variable = Methoooooooooooooooooooooooooood() ?: "some loooooong string" - val variable = Methooooood() ?: "some" + -" looong string" + val variable = Methooooood() + ?: "some looong string" var headerKdoc = firstCodeNode.prevSibling { it.elementType == KDOC diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt new file mode 100644 index 0000000000..b25f02b974 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -0,0 +1,4 @@ +package test.paragraph3.long_line + +val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot +.Qualified.Expression \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt index e32daa3b6c..ca87b3f0d4 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt @@ -1,11 +1,11 @@ package test.paragraph3.long_line -@Query(value = "select * from test inner join" + -" test_execution on test.id = test_execution.test_id and test_execution.st", nativeQuery = true) +@Query( +value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.st", + nativeQuery = true) fun retrieveBatches(limit: Int, offset: Int, executionId: Long): Some -@Query(value = "select * from test inner join" + -" test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true) +@Query(value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true) fun some(limit: Int, offset: Int, executionId: Long): List @Query(value = "select * from test inner joi", nativeQuery = true) diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineFunExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineFunExpected.kt index 476a99c1d6..937c5ea153 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineFunExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineFunExpected.kt @@ -1,6 +1,9 @@ package test.paragraph3.long_line -fun foo() = - println("fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd") +fun foo() = println( +"fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd" +) -fun foo () { println("fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd") } \ No newline at end of file +fun foo () { println( +"fhdbsfkhfbvsjfkvbhjdksfvhbhhjhjhjnaljfbkshvjdsjdnlvbdkhkjncdkljskbfvhdsjndlvfkbdhfjdncjsdcscsdcsdcsdcdd" +) } \ No newline at end of file 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 06fc6dc392..4e48223507 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 @@ -7,14 +7,14 @@ fun foo() { fun foo() { val veryLoooooooooooooooooongNamesList = listOf("Jack", "Nick") - veryLoooooooooooooooooongNamesList.forEach { + veryLoooooooooooooooooongNamesList +.forEach { name -> if (name == "Nick") { veryLoooooooooooooooooongNamesList.map { val str = "This string shouldn't be split"} name.map { val str = "This string should be split" } } - } } @@ -33,10 +33,11 @@ fun foo() { val longStringExpression = "First part" + "second Part" - val longStringExpression = "First" + "second Part" + val longStringExpression = "First" + + "second Part" - val longStringExpression = "First very long" + -" part" + "second Part" + val longStringExpression = + "First very long part" + "second Part" val longStringExpression2 = "String starts at the line len limit" diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongShortRValueExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongShortRValueExpected.kt index 5b9c951977..413e655a03 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongShortRValueExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongShortRValueExpected.kt @@ -1,3 +1,4 @@ package test.paragraph3.long_line -val LongWithVar2 = "${s + "a"} is a string" \ No newline at end of file +val LongWithVar2 = + "${s + "a"} is a string" \ No newline at end of file From 949f78a40ff16c9ff7f3efb124156c2816683bf0 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 17:21:03 +0300 Subject: [PATCH 04/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 119 ++++++++++++------ .../ruleset/chapter3/LineLengthFixTest.kt | 5 +- .../LongDotQualifiedExpressionTest.kt | 3 + ...ted.kt => LongExpressionNoFixExpected1.kt} | 4 +- .../long_line/LongExpressionNoFixExpected2.kt | 8 ++ .../long_line/LongInlineCommentsExpected.kt | 4 +- .../long_line/LongLineAnnotationExpected.kt | 20 ++- .../long_line/LongStringTemplateExpected.kt | 19 +-- .../LongValueArgumentsListExpected.kt | 32 +++++ .../long_line/LongValueArgumentsListTest.kt | 22 ++++ .../src/main/kotlin/CheckLongLineExpected.kt | 3 + .../src/main/kotlin/CheckLongLineTest.kt | 6 + 12 files changed, 192 insertions(+), 53 deletions(-) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt rename diktat-rules/src/test/resources/test/paragraph3/long_line/{LongExpressionNoFixExpected.kt => LongExpressionNoFixExpected1.kt} (76%) create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListExpected.kt create mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListTest.kt create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineExpected.kt create mode 100644 diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineTest.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 7b3679db9d..af44c45705 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 @@ -8,6 +8,7 @@ import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.* import com.pinterest.ktlint.core.ast.ElementType.ANDAND +import com.pinterest.ktlint.core.ast.ElementType.ARROW import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT @@ -51,6 +52,8 @@ import com.pinterest.ktlint.core.ast.ElementType.RPAR 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.VALUE_ARGUMENT_LIST +import com.pinterest.ktlint.core.ast.ElementType.WHEN_CONDITION_WITH_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.WHEN_ENTRY import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline @@ -58,6 +61,7 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType +import org.jetbrains.kotlin.psi.typeReferenceRecursiveVisitor import java.net.MalformedURLException import java.net.URL @@ -87,6 +91,7 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } + //println(node.prettyPrint()) println(node.text) } } @@ -129,9 +134,8 @@ class LineLength(configRules: List) : DiktatRule( do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - parent.findParentNodeWithSpecificType(VALUE_ARGUMENT_LIST) ?. let { - parent = it - } ?: parent.findParentNodeWithSpecificType(FUNCTION_LITERAL) ?. let { + val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) + findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry) ?. let { parent = it } ?: run { val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second @@ -159,20 +163,25 @@ class LineLength(configRules: List) : DiktatRule( VALUE_ARGUMENT_LIST -> { parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { parent = it - } ?: return checkArgumentList(parent, configuration) + } ?: run { + if (parent.treeParent.treeParent.elementType == WHEN_ENTRY ) { + return WhenEntry(parent.treeParent.treeParent) + } + return ValueArgumentList(parent, configuration) + } } + WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { stringOrDot = parent - parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { - parent = it - } ?: parent.findParentNodeWithSpecificType(VALUE_ARGUMENT_LIST) ?. let { + val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) + findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry) ?. let { parent = it } ?: run { - val parentFun = parent.findParentNodeWithSpecificType(FUN) - val parentProperty = parent.findParentNodeWithSpecificType(PROPERTY) - val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration, parentProperty ?: parentFun) + val parentIsPropertyOrFun = listOf(PROPERTY, FUN) + val parenPropertyOrFunNode = findParentNodeWithSpecificTypeMany(parent, parentIsPropertyOrFun) + val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration, parenPropertyOrFunNode) if(returnElem !is None) return returnElem parent = parent.treeParent @@ -184,13 +193,13 @@ class LineLength(configRules: List) : DiktatRule( return None() } - /** - * Analyzes the Binary expression and decides to go higher level with the analysis or analyze at this level - */ - private fun isConditionToUpAnalysisBinExpression(parent: ASTNode, offset: Int): Boolean { - val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) - val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) - return (parentIsBiExprOrParenthesized || (parentIsFunOrProperty && offset >= configuration.lineLength)) + private fun findParentNodeWithSpecificTypeMany(node: ASTNode, listType : List): ASTNode? { + listType.forEach { type -> + node.findParentNodeWithSpecificType(type) ?. let { + return it + } + } + return null } /** @@ -291,14 +300,6 @@ class LineLength(configRules: List) : DiktatRule( return None() } - private fun checkArgumentList(wrongNode: ASTNode, configuration: LineLengthConfiguration) : LongLineFixableCases { - val node = searchRightSplitAfterType(wrongNode, configuration, COMMA)?.first - node ?. let { - return ValueArgumentList(wrongNode, node) - } - return ValueArgumentList(wrongNode) - } - private fun checkFunAndProperty(wrongNode: ASTNode) = if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() @@ -349,27 +350,70 @@ class LineLength(configRules: List) : DiktatRule( is DotQualifiedExpression -> fixDotQualifiedExpression(fixableType) is ValueArgumentList -> fixArgumentList(fixableType) is Lambda -> fixLambda(fixableType.node) + is WhenEntry -> fixWhenEntry(fixableType) is None -> return } } + private fun fixWhenEntry(wrongWhenEntry : WhenEntry) { + val node = wrongWhenEntry.node + node.getFirstChildWithType(ARROW) ?. let { + node.addChild(PsiWhiteSpaceImpl(" "), it.treeNext) + node.addChild(PsiWhiteSpaceImpl("{"), it.treeNext.treeNext) + node.appendNewlineMergingWhiteSpace(it.treeNext.treeNext.treeNext, it.treeNext.treeNext.treeNext) + } + node.appendNewlineMergingWhiteSpace(node.lastChildNode.treeNext, node.lastChildNode.treeNext) + node.addChild(PsiWhiteSpaceImpl("}"), node.lastChildNode.treeNext) + } + private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node val dot = node.getFirstChildWithType(DOT) node.appendNewlineMergingWhiteSpace(dot,dot) } - private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { + + private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList) : Int{ + val lineLength = wrongArgumentList.maximumLineLength.lineLength val node = wrongArgumentList.node - val nodeWithComma = wrongArgumentList.nodeForSplit - val comma = nodeWithComma?.getFirstChildWithType(COMMA) - comma ?. let { - nodeWithComma.appendNewlineMergingWhiteSpace(comma, comma) - } ?: run { + var offset = 0 + node.getFirstChildWithType(COMMA) ?. let { + if (positionByOffset(it.startOffset).second > lineLength) { + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) + offset = 50 + } + } ?: node.getFirstChildWithType(RPAR) ?. let { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - node.getChildren(null).forEach { elem-> - if (elem.elementType == COMMA && elem.treeNext.elementType != RPAR) - node.appendNewlineMergingWhiteSpace(elem.treeNext, elem.treeNext) + offset = 50 + } + return offset + } + + private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { + val lineLength = wrongArgumentList.maximumLineLength.lineLength + val node = wrongArgumentList.node + val offset = fixArgumentsListFirstArgument(wrongArgumentList) + val listComma = node.getAllChildrenWithType(COMMA).map { + it to positionByOffset(it.startOffset - offset).second + }.sortedBy { it.second } //.filter { } + var n = 1 + listComma.forEachIndexed { index, pair -> + if (pair.second >= n * lineLength){ + n++ + val commaSplit = if (index > 0){ + listComma[index - 1].first + } else { + pair.first + } + node.appendNewlineMergingWhiteSpace(commaSplit.treeNext, commaSplit.treeNext) + } + } + node.getFirstChildWithType(RPAR) ?. let { + if (positionByOffset(it.treePrev.startOffset).second + it.treePrev.text.length - offset > lineLength * n && listComma.isNotEmpty()) { + listComma.last().first.run { + node.appendNewlineMergingWhiteSpace(this.treeNext, this.treeNext) + } } } } @@ -673,7 +717,7 @@ class LineLength(configRules: List) : DiktatRule( private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) /** - * Class Lambda show that long line should be split in Comment: in space between two words + * Class Lambda show that long line should be split in Lambda: in space after LBRACE node and before RBRACE node */ private class Lambda(node: ASTNode) : LongLineFixableCases(node) @@ -687,7 +731,10 @@ class LineLength(configRules: List) : DiktatRule( * If [nodeForSplit] !is null that node should be split after this COMMA * Else ([nodeForSplit] is null) - node should be split after LPAR, before RPAR and after all COMMA */ - private class ValueArgumentList(node: ASTNode, val nodeForSplit: ASTNode? = null) : LongLineFixableCases(node) + private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration) : LongLineFixableCases(node) + + private class WhenEntry(node: ASTNode) : LongLineFixableCases(node) + /** * val text = "first part" + * "second part" + 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 e1a79d7a85..d452f27cd0 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 @@ -99,5 +99,8 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fixAndCompare("LongDotQualifiedExpressionExpected.kt", "LongDotQualifiedExpressionTest.kt", rulesConfigListLineLength) } - + @Test + fun `fix long value arguments list`() { + fixAndCompare("LongValueArgumentsListExpected.kt", "LongValueArgumentsListTest.kt", rulesConfigListLineLength) + } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt new file mode 100644 index 0000000000..be356364b9 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -0,0 +1,3 @@ +package test.paragraph3.long_line + +val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected1.kt similarity index 76% rename from diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt rename to diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected1.kt index ddc30f0694..22e030d248 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected1.kt @@ -3,6 +3,6 @@ package org.cqfn.diktat.resources.paragraph3.longline class veryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong { // looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg //looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg - val s = "d" + -" s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" + val s = + "d s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt new file mode 100644 index 0000000000..22e030d248 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt @@ -0,0 +1,8 @@ +package org.cqfn.diktat.resources.paragraph3.longline + +class veryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong { + // looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg + //looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg + val s = + "d s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt index 4e9bf7519b..0f2bc91abe 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt @@ -45,7 +45,9 @@ fun foo() { // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ElementType.IF -> handleIfElse(node) ElementType.EOL_COMMENT, ElementType.BLOCK_COMMENT -> handleEolAndBlockComments(node, configuration) - ElementType.KDOC -> handleKdocComments(node, configuration) + ElementType.KDOC -> { + handleKdocComments(node, configuration) +} // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff else -> { // this is a generated else block diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt index ca87b3f0d4..3170f8c26a 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongLineAnnotationExpected.kt @@ -2,19 +2,27 @@ package test.paragraph3.long_line @Query( value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.st", - nativeQuery = true) + nativeQuery = true +) fun retrieveBatches(limit: Int, offset: Int, executionId: Long): Some -@Query(value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true) +@Query( +value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", + nativeQuery = true +) fun some(limit: Int, offset: Int, executionId: Long): List -@Query(value = "select * from test inner joi", nativeQuery = true) +@Query(value = "select * from test inner joi", + nativeQuery = true) fun test(limit: Int, offset: Int, executionId: Long): List -@Query(value = "select * from test inner joibbb", nativeQuery = true) +@Query(value = "select * from test inner joibbb", + nativeQuery = true) fun cornerCase(limit: Int, offset: Int, executionId: Long): List -@Query(value = "select * from test inner join" + -" test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", nativeQuery = true) +@Query( +value = "select * from test inner join test_execution on test.id = test_execution.test_id and test_execution.status = 'READY' and test_execution.test_suite_execution_id = ?3 limit ?1 offset ?2", + nativeQuery = true +) fun some(limit: Int, offset: Int, executionId: Long) = println("testtesttesttesttesttesttesttesttesttesttesttest") diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt index b386956da8..0c18663665 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt @@ -3,10 +3,12 @@ object Observables { val someCode = 15 // Some // looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong line - @Deprecated("New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in" + -" future RxKotlin release.", - replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, combineFunction)", "io.reactivex.Observable"), - level = DeprecationLevel.WARNING) + @Deprecated( +"New type inference algorithm in Kotlin 1.4 makes this method obsolete. Method will be removed in future RxKotlin release.", + replaceWith = ReplaceWith("Observable.combineLatest(source1, source2, source3, source4, combineFunction)", + "io.reactivex.Observable"), + level = DeprecationLevel.WARNING +) @CheckReturnValue @SchedulerSupport(SchedulerSupport.NONE) inline fun combineLatest() {} @@ -15,9 +17,12 @@ val someCode = 15 class Foo() { fun Fuu() { - logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" + - response.message}") - logger.log("<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvsdcsdcscskdjdfvd" else "dfjvndsdcsdcsdcskjnbvif" + response.message}") + logger.log( +"<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvkdjdfvd" else "dfjvndkjnbvif" + response.message}" +) + logger.log( +"<-- ${response.code} ${ if (response.message.isEmpty()) "skfnvsdcsdcscskdjdfvd" else "dfjvndsdcsdcsdcskjnbvif" + response.message}" +) } val q = """ diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListExpected.kt new file mode 100644 index 0000000000..bcb4040527 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListExpected.kt @@ -0,0 +1,32 @@ +package test.paragraph3.long_line + +val firstArgument = 1 +val secondArgument = 2 +val thirdArgument = 3 +val fourthArgument = 4 +val fifthArguments = 5 +val sixthArguments = 6 +val seventhArguments = 7 +val eighthArguments = 8 + +// Many arguments in function +val result1 = ManyParamInFunction(firstArgument, + secondArgument, thirdArgument, fourthArgument, + fifthArguments, sixthArguments, seventhArguments, + eighthArguments) + +// +val result2 = veryLongNameFun(firstArgument, + secondArgument) + +// first argument cannot to be able to stay in +// the first line +val result3 = veryLongNameInFirstParam( +firstArgument, secondArgument, thirdArgument +) + +// first argument cannot to be able to stay in +// the first line +val result4 = veryLongNameInFirstParam( +firstArgument +) \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListTest.kt new file mode 100644 index 0000000000..d516f45726 --- /dev/null +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongValueArgumentsListTest.kt @@ -0,0 +1,22 @@ +package test.paragraph3.long_line + +val firstArgument = 1 +val secondArgument = 2 +val thirdArgument = 3 +val fourthArgument = 4 +val fifthArguments = 5 +val sixthArguments = 6 +val seventhArguments = 7 +val eighthArguments = 8 + +// Many arguments in function +val result1 = ManyParamInFunction(firstArgument, secondArgument, thirdArgument, fourthArgument, fifthArguments, sixthArguments, seventhArguments, eighthArguments) + +// +val result2 = veryLongNameFun(firstArgument, secondArgument) + +// first argument cannot to be able to stay in the first line +val result3 = veryLongNameInFirstParam(firstArgument, secondArgument, thirdArgument) + +// first argument cannot to be able to stay in the first line +val result4 = veryLongNameInFirstParam(firstArgument) \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineExpected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineExpected.kt new file mode 100644 index 0000000000..c7f575b940 --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineExpected.kt @@ -0,0 +1,3 @@ +package org.cqfn.diktat + +val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineTest.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineTest.kt new file mode 100644 index 0000000000..92665f2891 --- /dev/null +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/CheckLongLineTest.kt @@ -0,0 +1,6 @@ +package org.cqfn.diktat + +/** + * @return + */ +val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression \ No newline at end of file From abba36fb65b027b68c37b81f686e154a1bca4d86 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 17:47:53 +0300 Subject: [PATCH 05/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 113 ++++++++++-------- 1 file changed, 61 insertions(+), 52 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 af44c45705..fd2bf23e41 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 @@ -9,10 +9,10 @@ import org.cqfn.diktat.ruleset.utils.* import com.pinterest.ktlint.core.ast.ElementType.ANDAND import com.pinterest.ktlint.core.ast.ElementType.ARROW -import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT +import com.pinterest.ktlint.core.ast.ElementType.COMMA 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 @@ -61,7 +61,6 @@ import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.com.intellij.psi.tree.IElementType -import org.jetbrains.kotlin.psi.typeReferenceRecursiveVisitor import java.net.MalformedURLException import java.net.URL @@ -91,8 +90,8 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } - //println(node.prettyPrint()) - println(node.text) + // println(node.prettyPrint()) + // println(node.text) } } @@ -127,15 +126,16 @@ class LineLength(configRules: List) : DiktatRule( @Suppress( "TOO_LONG_FUNCTION", "ComplexMethod", + "GENERIC_VARIABLE_WRONG_DECLARATION", ) private fun isFixable(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { var parent = wrongNode - var stringOrDot : ASTNode? = null + var stringOrDot: ASTNode? = null do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) - findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry) ?. let { + findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second @@ -144,11 +144,12 @@ class LineLength(configRules: List) : DiktatRule( val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) if (parentIsBiExprOrParenthesized) { parent = parent.treeParent - } else if (parentIsFunOrProperty && splitOffset >= configuration.lineLength) { - if (stringOrDot != null) { + } else if (parentIsFunOrProperty && splitOffset >= configuration.lineLength) { + stringOrDot?.let { val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration) - if (returnElem !is None) + if (returnElem !is None) { return returnElem + } } parent = parent.treeParent } else { @@ -160,15 +161,13 @@ class LineLength(configRules: List) : DiktatRule( } FUN, PROPERTY -> return checkFunAndProperty(parent) CONDITION -> return checkCondition(parent, configuration) - VALUE_ARGUMENT_LIST -> { - parent.findParentNodeWithSpecificType(BINARY_EXPRESSION) ?. let { - parent = it - } ?: run { - if (parent.treeParent.treeParent.elementType == WHEN_ENTRY ) { - return WhenEntry(parent.treeParent.treeParent) - } - return ValueArgumentList(parent, configuration) + VALUE_ARGUMENT_LIST -> parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { + parent = it + } ?: run { + if (parent.treeParent.treeParent.elementType == WHEN_ENTRY) { + return WhenEntry(parent.treeParent.treeParent) } + return ValueArgumentList(parent, configuration) } WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) @@ -176,14 +175,15 @@ class LineLength(configRules: List) : DiktatRule( STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { stringOrDot = parent val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) - findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry) ?. let { + findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { parent = it } ?: run { val parentIsPropertyOrFun = listOf(PROPERTY, FUN) val parenPropertyOrFunNode = findParentNodeWithSpecificTypeMany(parent, parentIsPropertyOrFun) val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration, parenPropertyOrFunNode) - if(returnElem !is None) + if (returnElem !is None) { return returnElem + } parent = parent.treeParent } } @@ -193,9 +193,9 @@ class LineLength(configRules: List) : DiktatRule( return None() } - private fun findParentNodeWithSpecificTypeMany(node: ASTNode, listType : List): ASTNode? { + private fun findParentNodeWithSpecificTypeMany(node: ASTNode, listType: List): ASTNode? { listType.forEach { type -> - node.findParentNodeWithSpecificType(type) ?. let { + node.findParentNodeWithSpecificType(type)?.let { return it } } @@ -215,28 +215,33 @@ class LineLength(configRules: List) : DiktatRule( return LongBinaryExpression(node, configuration, leftOffset, binList) } - private fun checkStringTemplateAndDotQualifiedExpression(node: ASTNode, configuration: LineLengthConfiguration, funOrPropertyNode : ASTNode? = null) : LongLineFixableCases { - funOrPropertyNode ?.let { + @Suppress("TOO_MANY_LINES_IN_LAMBDA") + private fun checkStringTemplateAndDotQualifiedExpression( + node: ASTNode, + configuration: LineLengthConfiguration, + funOrPropertyNode: ASTNode? = null + ): LongLineFixableCases { + funOrPropertyNode?.let { if (it.hasChildOfType(EQ)) { val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)!!.startOffset).second if (positionByOffset < configuration.lineLength / 2) { val returnedClass = parserStringAndDot(node, configuration) - if (returnedClass !is None) + if (returnedClass !is None) { return returnedClass + } } return FunAndProperty(it) } return parserStringAndDot(node, configuration) - } ?: - return parserStringAndDot(node, configuration) + } ?: return parserStringAndDot(node, configuration) } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -292,12 +297,11 @@ class LineLength(configRules: List) : DiktatRule( return StringTemplate(node, correcterDelimiter, multiLineOffset == 0) } - private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration) : LongLineFixableCases { + private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT)?.first - nodeDot ?. let { + nodeDot?.let { return DotQualifiedExpression(wrongNode) - } ?: - return None() + } ?: return None() } private fun checkFunAndProperty(wrongNode: ASTNode) = @@ -355,9 +359,9 @@ class LineLength(configRules: List) : DiktatRule( } } - private fun fixWhenEntry(wrongWhenEntry : WhenEntry) { + private fun fixWhenEntry(wrongWhenEntry: WhenEntry) { val node = wrongWhenEntry.node - node.getFirstChildWithType(ARROW) ?. let { + node.getFirstChildWithType(ARROW)?.let { node.addChild(PsiWhiteSpaceImpl(" "), it.treeNext) node.addChild(PsiWhiteSpaceImpl("{"), it.treeNext.treeNext) node.appendNewlineMergingWhiteSpace(it.treeNext.treeNext.treeNext, it.treeNext.treeNext.treeNext) @@ -369,20 +373,20 @@ class LineLength(configRules: List) : DiktatRule( private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node val dot = node.getFirstChildWithType(DOT) - node.appendNewlineMergingWhiteSpace(dot,dot) + node.appendNewlineMergingWhiteSpace(dot, dot) } - private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList) : Int{ + private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList): Int { val lineLength = wrongArgumentList.maximumLineLength.lineLength val node = wrongArgumentList.node var offset = 0 - node.getFirstChildWithType(COMMA) ?. let { + node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) offset = 50 } - } ?: node.getFirstChildWithType(RPAR) ?. let { + } ?: node.getFirstChildWithType(RPAR)?.let { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) offset = 50 @@ -396,12 +400,12 @@ class LineLength(configRules: List) : DiktatRule( val offset = fixArgumentsListFirstArgument(wrongArgumentList) val listComma = node.getAllChildrenWithType(COMMA).map { it to positionByOffset(it.startOffset - offset).second - }.sortedBy { it.second } //.filter { } - var n = 1 + }.sortedBy { it.second } + var lineNumber = 1 listComma.forEachIndexed { index, pair -> - if (pair.second >= n * lineLength){ - n++ - val commaSplit = if (index > 0){ + if (pair.second >= lineNumber * lineLength) { + lineNumber++ + val commaSplit = if (index > 0) { listComma[index - 1].first } else { pair.first @@ -409,8 +413,8 @@ class LineLength(configRules: List) : DiktatRule( node.appendNewlineMergingWhiteSpace(commaSplit.treeNext, commaSplit.treeNext) } } - node.getFirstChildWithType(RPAR) ?. let { - if (positionByOffset(it.treePrev.startOffset).second + it.treePrev.text.length - offset > lineLength * n && listComma.isNotEmpty()) { + node.getFirstChildWithType(RPAR)?.let { + if (positionByOffset(it.treePrev.startOffset).second + it.treePrev.text.length - offset > lineLength * lineNumber && listComma.isNotEmpty()) { listComma.last().first.run { node.appendNewlineMergingWhiteSpace(this.treeNext, this.treeNext) } @@ -541,7 +545,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun searchComma(node: ASTNode, commaList: MutableList) { - if (node.elementType == VALUE_ARGUMENT_LIST){ + if (node.elementType == VALUE_ARGUMENT_LIST) { node.getChildren(null) .filter { it.elementType == VALUE_ARGUMENT_LIST @@ -553,8 +557,8 @@ class LineLength(configRules: List) : DiktatRule( } } - private fun searchDot(node: ASTNode, DotList: MutableList) { - if (node.elementType == DOT_QUALIFIED_EXPRESSION){ + private fun searchDot(node: ASTNode, dotList: MutableList) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION) { node.getChildren(null) .filter { it.elementType == DOT_QUALIFIED_EXPRESSION @@ -618,7 +622,11 @@ class LineLength(configRules: List) : DiktatRule( * Finds the first binary expression closer to the separator */ @Suppress("UnsafeCallOnNullableType") - private fun searchRightSplitAfterType(parent: ASTNode, configuration: LineLengthConfiguration, type: IElementType): Pair? { + private fun searchRightSplitAfterType( + parent: ASTNode, + configuration: LineLengthConfiguration, + type: IElementType + ): Pair? { val list: MutableList = mutableListOf() when (type) { OPERATION_REFERENCE -> searchBinaryExpression(parent, list) @@ -730,6 +738,7 @@ class LineLength(configRules: List) : DiktatRule( * Class ValueArgumentList show that line should be split in ValueArgumentList: * If [nodeForSplit] !is null that node should be split after this COMMA * Else ([nodeForSplit] is null) - node should be split after LPAR, before RPAR and after all COMMA + * @property maximumLineLength */ private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration) : LongLineFixableCases(node) From 6854102cc268f24eee82e43b37ee74f9a90db6d6 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:06:36 +0300 Subject: [PATCH 06/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 41 +++++++++---------- 1 file changed, 19 insertions(+), 22 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 fd2bf23e41..304c0c0d6b 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 @@ -90,8 +90,6 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } - // println(node.prettyPrint()) - // println(node.text) } } @@ -394,6 +392,9 @@ class LineLength(configRules: List) : DiktatRule( return offset } + /** + * Fix arguments in arguments list + */ private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { val lineLength = wrongArgumentList.maximumLineLength.lineLength val node = wrongArgumentList.node @@ -544,19 +545,13 @@ class LineLength(configRules: List) : DiktatRule( } } - private fun searchComma(node: ASTNode, commaList: MutableList) { - if (node.elementType == VALUE_ARGUMENT_LIST) { - node.getChildren(null) - .filter { - it.elementType == VALUE_ARGUMENT_LIST - } - .forEach { - searchComma(it, commaList) - } - commaList.add(node) - } - } - + /** + * This method uses recursion to store dot qualified expression node in the order in which they are located + * Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid) + * + *@param node node in which to search + *@param dotList mutable list of ASTNode to store nodes + */ private fun searchDot(node: ASTNode, dotList: MutableList) { if (node.elementType == DOT_QUALIFIED_EXPRESSION) { node.getChildren(null) @@ -564,9 +559,9 @@ class LineLength(configRules: List) : DiktatRule( it.elementType == DOT_QUALIFIED_EXPRESSION } .forEach { - searchDot(it, DotList) + searchDot(it, dotList) } - DotList.add(node) + dotList.add(node) } } @@ -619,7 +614,7 @@ class LineLength(configRules: List) : DiktatRule( } /** - * Finds the first binary expression closer to the separator + * Finds the first binary expression or dat closer to the separator */ @Suppress("UnsafeCallOnNullableType") private fun searchRightSplitAfterType( @@ -630,7 +625,6 @@ class LineLength(configRules: List) : DiktatRule( val list: MutableList = mutableListOf() when (type) { OPERATION_REFERENCE -> searchBinaryExpression(parent, list) - COMMA -> searchComma(parent, list) DOT -> searchDot(parent, list) } return list.map { @@ -736,12 +730,15 @@ class LineLength(configRules: List) : DiktatRule( /** * Class ValueArgumentList show that line should be split in ValueArgumentList: - * If [nodeForSplit] !is null that node should be split after this COMMA - * Else ([nodeForSplit] is null) - node should be split after LPAR, before RPAR and after all COMMA - * @property maximumLineLength + * @property maximumLineLength - max line length */ private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration) : LongLineFixableCases(node) + /** + * Class WhenEntry show that line should be split in WhenEntry node: + * Added LBRACE and RBRACE nodes + * Split line in space after LBRACE node and before RBRACE node + */ private class WhenEntry(node: ASTNode) : LongLineFixableCases(node) /** From 544011fb8c17304067321aab28203b16f50ba0e1 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:27:58 +0300 Subject: [PATCH 07/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 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 304c0c0d6b..8aa237a116 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 @@ -123,6 +123,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress( "TOO_LONG_FUNCTION", + "LongMethod", "ComplexMethod", "GENERIC_VARIABLE_WRONG_DECLARATION", ) @@ -221,7 +222,7 @@ class LineLength(configRules: List) : DiktatRule( ): LongLineFixableCases { funOrPropertyNode?.let { if (it.hasChildOfType(EQ)) { - val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)!!.startOffset).second + val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)?.startOffset ?: 0).second if (positionByOffset < configuration.lineLength / 2) { val returnedClass = parserStringAndDot(node, configuration) if (returnedClass !is None) { @@ -374,22 +375,23 @@ class LineLength(configRules: List) : DiktatRule( node.appendNewlineMergingWhiteSpace(dot, dot) } + @Suppress("UnsafeCallOnNullableType") private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList): Int { val lineLength = wrongArgumentList.maximumLineLength.lineLength val node = wrongArgumentList.node - var offset = 0 + var startOffset = 0 node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - offset = 50 + startOffset = 50 } } ?: node.getFirstChildWithType(RPAR)?.let { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - offset = 50 + startOffset = 50 } - return offset + return startOffset } /** From 58716cfe3e7eaae172335e46b5b31fdfd3faf124 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:32:32 +0300 Subject: [PATCH 08/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 8aa237a116..1a97268c3c 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 @@ -375,7 +375,7 @@ class LineLength(configRules: List) : DiktatRule( node.appendNewlineMergingWhiteSpace(dot, dot) } - @Suppress("UnsafeCallOnNullableType") + @Suppress("UnsafeCallOnNullableType", "MagicNumber") private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList): Int { val lineLength = wrongArgumentList.maximumLineLength.lineLength val node = wrongArgumentList.node From df885f8306c4c9d2bed7e95a3e1f97494e574aa3 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:33:20 +0300 Subject: [PATCH 09/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 1a97268c3c..60d5b5b0ca 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 @@ -384,12 +384,12 @@ class LineLength(configRules: List) : DiktatRule( if (positionByOffset(it.startOffset).second > lineLength) { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - startOffset = 50 + startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } } ?: node.getFirstChildWithType(RPAR)?.let { node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - startOffset = 50 + startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } return startOffset } From fda30f6db0cb64455f1618a1f85ed88110e28498 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:36:19 +0300 Subject: [PATCH 10/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt | 8 ++------ ...onNoFixExpected1.kt => LongExpressionNoFixExpected.kt} | 0 .../paragraph3/long_line/LongExpressionNoFixExpected2.kt | 8 -------- 3 files changed, 2 insertions(+), 14 deletions(-) rename diktat-rules/src/test/resources/test/paragraph3/long_line/{LongExpressionNoFixExpected1.kt => LongExpressionNoFixExpected.kt} (100%) delete mode 100644 diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt 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 d452f27cd0..609331eaa7 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,14 +70,10 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) } @Test - fun `shouldn't fix1`() { - fixAndCompare("LongExpressionNoFixExpected1.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) + fun `shouldn't fix`() { + fixAndCompare("LongExpressionNoFixExpected.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) } - @Test - fun `shouldn't fix2`() { - fixAndCompare("LongExpressionNoFixExpected2.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) - } @Test fun `should fix annotation`() { diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected1.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt similarity index 100% rename from diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected1.kt rename to diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected.kt diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt deleted file mode 100644 index 22e030d248..0000000000 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongExpressionNoFixExpected2.kt +++ /dev/null @@ -1,8 +0,0 @@ -package org.cqfn.diktat.resources.paragraph3.longline - -class veryLoooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong { - // looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg - //looooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooongggggggggggggggggggggggggg - val s = - "d s d d d d ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd" -} From 566f62f05fbe13a8c6ff0f1639468df1b6eae1c0 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 18:39:46 +0300 Subject: [PATCH 11/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/chapter3/LineLengthFixTest.kt | 1 - 1 file changed, 1 deletion(-) 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 609331eaa7..257d2710cc 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 @@ -74,7 +74,6 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) fixAndCompare("LongExpressionNoFixExpected.kt", "LongExpressionNoFixTest.kt", rulesConfigListShortLineLength) } - @Test fun `should fix annotation`() { fixAndCompare("LongLineAnnotationExpected.kt", "LongLineAnnotationTest.kt", rulesConfigListLineLength) From 6b0ba56d7f3d02a1c85c947778cd97f7e8c3bcd3 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 20:36:57 +0300 Subject: [PATCH 12/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 38 ++++++++++--------- .../long_line/LongInlineCommentsExpected.kt | 3 +- 2 files changed, 22 insertions(+), 19 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 60d5b5b0ca..480b4937eb 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 @@ -10,6 +10,7 @@ import org.cqfn.diktat.ruleset.utils.* import com.pinterest.ktlint.core.ast.ElementType.ANDAND import com.pinterest.ktlint.core.ast.ElementType.ARROW import com.pinterest.ktlint.core.ast.ElementType.BINARY_EXPRESSION +import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.COMMA @@ -160,14 +161,12 @@ class LineLength(configRules: List) : DiktatRule( } FUN, PROPERTY -> return checkFunAndProperty(parent) CONDITION -> return checkCondition(parent, configuration) - VALUE_ARGUMENT_LIST -> parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { - parent = it - } ?: run { - if (parent.treeParent.treeParent.elementType == WHEN_ENTRY) { - return WhenEntry(parent.treeParent.treeParent) - } - return ValueArgumentList(parent, configuration) + VALUE_ARGUMENT_LIST -> { + parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { + parent = it + } ?: return checkArgumentsList(parent, configuration) } + WHEN_ENTRY -> return WhenEntry(parent) WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) @@ -177,9 +176,7 @@ class LineLength(configRules: List) : DiktatRule( findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { parent = it } ?: run { - val parentIsPropertyOrFun = listOf(PROPERTY, FUN) - val parenPropertyOrFunNode = findParentNodeWithSpecificTypeMany(parent, parentIsPropertyOrFun) - val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration, parenPropertyOrFunNode) + val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration) if (returnElem !is None) { return returnElem } @@ -201,6 +198,16 @@ class LineLength(configRules: List) : DiktatRule( return null } + private fun checkArgumentsList(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases{ + node.findParentNodeWithSpecificType(WHEN_ENTRY) ?. let { + it.findChildByType(BLOCK) ?. run { + return ValueArgumentList(node, configuration) + } ?: return WhenEntry(it) + } + return ValueArgumentList(node, configuration) + } + + /** * Parses the existing binary expression and passes the necessary parameters to the fix function for splitting */ @@ -217,9 +224,10 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("TOO_MANY_LINES_IN_LAMBDA") private fun checkStringTemplateAndDotQualifiedExpression( node: ASTNode, - configuration: LineLengthConfiguration, - funOrPropertyNode: ASTNode? = null + configuration: LineLengthConfiguration ): LongLineFixableCases { + val isPropertyOrFun = listOf(PROPERTY, FUN) + val funOrPropertyNode = findParentNodeWithSpecificTypeMany(node, isPropertyOrFun) funOrPropertyNode?.let { if (it.hasChildOfType(EQ)) { val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)?.startOffset ?: 0).second @@ -361,12 +369,8 @@ class LineLength(configRules: List) : DiktatRule( private fun fixWhenEntry(wrongWhenEntry: WhenEntry) { val node = wrongWhenEntry.node node.getFirstChildWithType(ARROW)?.let { - node.addChild(PsiWhiteSpaceImpl(" "), it.treeNext) - node.addChild(PsiWhiteSpaceImpl("{"), it.treeNext.treeNext) - node.appendNewlineMergingWhiteSpace(it.treeNext.treeNext.treeNext, it.treeNext.treeNext.treeNext) + node.appendNewlineMergingWhiteSpace(it.treeNext, it.treeNext) } - node.appendNewlineMergingWhiteSpace(node.lastChildNode.treeNext, node.lastChildNode.treeNext) - node.addChild(PsiWhiteSpaceImpl("}"), node.lastChildNode.treeNext) } private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt index 0f2bc91abe..789a4a066e 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongInlineCommentsExpected.kt @@ -45,9 +45,8 @@ fun foo() { // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff ElementType.IF -> handleIfElse(node) ElementType.EOL_COMMENT, ElementType.BLOCK_COMMENT -> handleEolAndBlockComments(node, configuration) - ElementType.KDOC -> { + ElementType.KDOC -> handleKdocComments(node, configuration) -} // ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff else -> { // this is a generated else block From ebf05b1669553c66adf932b3cbe23b2a8f8a4640 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 24 May 2022 23:24:31 +0300 Subject: [PATCH 13/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../diktat/ruleset/rules/chapter3/LineLength.kt | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 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 480b4937eb..c2f03edbbc 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 @@ -161,11 +161,9 @@ class LineLength(configRules: List) : DiktatRule( } FUN, PROPERTY -> return checkFunAndProperty(parent) CONDITION -> return checkCondition(parent, configuration) - VALUE_ARGUMENT_LIST -> { - parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { - parent = it - } ?: return checkArgumentsList(parent, configuration) - } + VALUE_ARGUMENT_LIST -> parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { + parent = it + } ?: return checkArgumentsList(parent, configuration) WHEN_ENTRY -> return WhenEntry(parent) WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) @@ -198,16 +196,15 @@ class LineLength(configRules: List) : DiktatRule( return null } - private fun checkArgumentsList(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases{ - node.findParentNodeWithSpecificType(WHEN_ENTRY) ?. let { - it.findChildByType(BLOCK) ?. run { + private fun checkArgumentsList(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { + node.findParentNodeWithSpecificType(WHEN_ENTRY)?.let { + it.findChildByType(BLOCK)?.run { return ValueArgumentList(node, configuration) } ?: return WhenEntry(it) } return ValueArgumentList(node, configuration) } - /** * Parses the existing binary expression and passes the necessary parameters to the fix function for splitting */ @@ -221,7 +218,7 @@ class LineLength(configRules: List) : DiktatRule( return LongBinaryExpression(node, configuration, leftOffset, binList) } - @Suppress("TOO_MANY_LINES_IN_LAMBDA") + @Suppress("TOO_MANY_LINES_IN_LAMBDA", "GENERIC_VARIABLE_WRONG_DECLARATION") private fun checkStringTemplateAndDotQualifiedExpression( node: ASTNode, configuration: LineLengthConfiguration From 998ee8befae9b0700d71affdd3d94a6c254ded16 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 16:05:19 +0300 Subject: [PATCH 14/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 30 ++++++++++++------- .../LongDotQualifiedExpressionExpected.kt | 17 ++++++++++- .../LongDotQualifiedExpressionTest.kt | 13 ++++++++ 3 files changed, 48 insertions(+), 12 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 c2f03edbbc..709996f574 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 @@ -1,5 +1,6 @@ package org.cqfn.diktat.ruleset.rules.chapter3 +import com.pinterest.ktlint.core.ast.ElementType import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig @@ -50,6 +51,8 @@ import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.RBRACE 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.VALUE_ARGUMENT_LIST @@ -168,7 +171,7 @@ class LineLength(configRules: List) : DiktatRule( WHEN_CONDITION_WITH_EXPRESSION -> return None() EOL_COMMENT -> return checkComment(parent, configuration) FUNCTION_LITERAL -> return Lambda(parent) - STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION -> { + STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { stringOrDot = parent val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { @@ -302,10 +305,14 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { - val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT)?.first + val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT) + val nodeSafeAccess = searchRightSplitAfterType(wrongNode, configuration, SAFE_ACCESS) nodeDot?.let { return DotQualifiedExpression(wrongNode) - } ?: return None() + } ?: nodeSafeAccess?.let { + return DotQualifiedExpression(wrongNode) + } + return None() } private fun checkFunAndProperty(wrongNode: ASTNode) = @@ -372,8 +379,9 @@ class LineLength(configRules: List) : DiktatRule( private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node - val dot = node.getFirstChildWithType(DOT) - node.appendNewlineMergingWhiteSpace(dot, dot) + val dot = node.getFirstChildWithType(DOT) ?: node.getFirstChildWithType(SAFE_ACCESS) + val nodeBeforeDot = dot?.treePrev + node.appendNewlineMergingWhiteSpace(nodeBeforeDot, dot) } @Suppress("UnsafeCallOnNullableType", "MagicNumber") @@ -555,14 +563,14 @@ class LineLength(configRules: List) : DiktatRule( *@param node node in which to search *@param dotList mutable list of ASTNode to store nodes */ - private fun searchDot(node: ASTNode, dotList: MutableList) { - if (node.elementType == DOT_QUALIFIED_EXPRESSION) { + private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION) { node.getChildren(null) .filter { - it.elementType == DOT_QUALIFIED_EXPRESSION + it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION } .forEach { - searchDot(it, dotList) + searchDotOrSafeAccess(it, dotList) } dotList.add(node) } @@ -628,7 +636,7 @@ class LineLength(configRules: List) : DiktatRule( val list: MutableList = mutableListOf() when (type) { OPERATION_REFERENCE -> searchBinaryExpression(parent, list) - DOT -> searchDot(parent, list) + DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) } return list.map { it to positionByOffset(it.getFirstChildWithType(type)?.startOffset ?: configuration.lineLength.toInt()).second @@ -636,7 +644,7 @@ class LineLength(configRules: List) : DiktatRule( .sortedBy { it.second } .reversed() .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(type)?.text?.length ?: 0) <= configuration.lineLength + 1 + offset + (it.getFirstChildWithType(type)?.text?.length ?:0)<= configuration.lineLength + 1 } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt index b25f02b974..f11b671c12 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -1,4 +1,19 @@ package test.paragraph3.long_line val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot -.Qualified.Expression \ No newline at end of file +.Qualified.Expression + +val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot +?.Qualified?.Expression + +val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! +.Dot!!.Qualified!!.Expression + +val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot + .Qualified.Expression + +val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot + ?.Qualified?.Expression + +val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! + .Dot!!.Qualified!!.Expression \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt index be356364b9..b841210240 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -1,3 +1,16 @@ package test.paragraph3.long_line val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression + +val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot?.Qualified?.Expression + +val C = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!!.Dot!!.Qualified!!.Expression + +val D = This.Is.Veeeeryyyyyyy.Loooooong.Dot + .Qualified.Expression + +val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot + ?.Qualified?.Expression + +val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! + .Dot!!.Qualified!!.Expression \ No newline at end of file From ac942e60870f09347e28d910c90563d30435d4b1 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 16:17:56 +0300 Subject: [PATCH 15/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 709996f574..faea4eda80 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 @@ -644,7 +644,7 @@ class LineLength(configRules: List) : DiktatRule( .sortedBy { it.second } .reversed() .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(type)?.text?.length ?:0)<= configuration.lineLength + 1 + offset + (it.getFirstChildWithType(type)?.text?.length ?: 0) <= configuration.lineLength + 1 } } From 8c3d6b8f5c370cea3d98b93d21f1554218fa23a0 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 16:36:10 +0300 Subject: [PATCH 16/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../diktat/ruleset/rules/chapter3/LineLength.kt | 15 ++++++++++----- .../long_line/LongLineRValueExpected.kt | 6 +++--- 2 files changed, 13 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 faea4eda80..8dcc3df317 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 @@ -1,8 +1,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3 -import com.pinterest.ktlint.core.ast.ElementType -import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig +import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE import org.cqfn.diktat.ruleset.rules.DiktatRule @@ -379,9 +378,15 @@ class LineLength(configRules: List) : DiktatRule( private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { val node = wrongDotQualifiedExpression.node - val dot = node.getFirstChildWithType(DOT) ?: node.getFirstChildWithType(SAFE_ACCESS) - val nodeBeforeDot = dot?.treePrev - node.appendNewlineMergingWhiteSpace(nodeBeforeDot, dot) + val dot = node.getFirstChildWithType(DOT) + val safeAccess = node.getFirstChildWithType(SAFE_ACCESS) + val splitNode = if ( (dot?.startOffset ?: 0) > (safeAccess?.startOffset ?: 0)) { + dot + } else { + safeAccess + } + val nodeBeforeDot = splitNode?.treePrev + node.appendNewlineMergingWhiteSpace(nodeBeforeDot, splitNode) } @Suppress("UnsafeCallOnNullableType", "MagicNumber") 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 4e48223507..a92a4ca7e5 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 @@ -8,10 +8,10 @@ fun foo() { val veryLoooooooooooooooooongNamesList = listOf("Jack", "Nick") veryLoooooooooooooooooongNamesList -.forEach { - name -> +.forEach { name -> if (name == "Nick") { - veryLoooooooooooooooooongNamesList.map { val str = "This string shouldn't be split"} + veryLoooooooooooooooooongNamesList +.map { val str = "This string shouldn't be split"} name.map { val str = "This string should be split" } } From ca89cbd3ba85eec4d894138c5fc64f132e3431b8 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 16:40:39 +0300 Subject: [PATCH 17/52] ### Whats added: * corrected logic fix and warn String Template in Linelength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 8dcc3df317..66a13f7690 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 @@ -1,7 +1,7 @@ package org.cqfn.diktat.ruleset.rules.chapter3 -import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.RuleConfiguration +import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE import org.cqfn.diktat.ruleset.rules.DiktatRule @@ -380,7 +380,7 @@ class LineLength(configRules: List) : DiktatRule( val node = wrongDotQualifiedExpression.node val dot = node.getFirstChildWithType(DOT) val safeAccess = node.getFirstChildWithType(SAFE_ACCESS) - val splitNode = if ( (dot?.startOffset ?: 0) > (safeAccess?.startOffset ?: 0)) { + val splitNode = if ((dot?.startOffset ?: 0) > (safeAccess?.startOffset ?: 0)) { dot } else { safeAccess From e75580caf2d5357e3f6b9e05d8be1aa295a5daef Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 25 May 2022 18:00:10 +0300 Subject: [PATCH 18/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../diktat/ruleset/rules/chapter3/LineLength.kt | 14 +++++++------- .../long_line/LongStringTemplateExpected.kt | 3 +++ .../paragraph3/long_line/LongStringTemplateTest.kt | 2 ++ 3 files changed, 12 insertions(+), 7 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 66a13f7690..d2e4fb7e86 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 @@ -144,21 +144,21 @@ class LineLength(configRules: List) : DiktatRule( splitOffset?.let { val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) - if (parentIsBiExprOrParenthesized) { + if (parentIsBiExprOrParenthesized || (parentIsFunOrProperty && splitOffset >= configuration.lineLength)) { parent = parent.treeParent - } else if (parentIsFunOrProperty && splitOffset >= configuration.lineLength) { + } else { + return checkBinaryExpression(parent, configuration) + } + } + ?: run { stringOrDot?.let { - val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration) + val returnElem = checkStringTemplateAndDotQualifiedExpression(it, configuration) if (returnElem !is None) { return returnElem } } parent = parent.treeParent - } else { - return checkBinaryExpression(parent, configuration) } - } - ?: run { parent = parent.treeParent } } } FUN, PROPERTY -> return checkFunAndProperty(parent) diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt index 0c18663665..63ce3b3c8f 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateExpected.kt @@ -51,4 +51,7 @@ class Foo() { } """.trimIndent() } + + val stringName = "This is long string template with binary expression. test should be up in level binary" + +" expression and cannot split in operation reference and should be split this long string template" + "this string should be after operated reference" } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateTest.kt index 005cf1a91c..92188058d9 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongStringTemplateTest.kt @@ -38,4 +38,6 @@ class Foo() { } """.trimIndent() } + + val stringName = "This is long string template with binary expression. test should be up in level binary expression and cannot split in operation reference and should be split this long string template" + "this string should be after operated reference" } From 4b5a2d0b1b4eb74dc2aa582819257d351a0297d2 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 13:59:29 +0300 Subject: [PATCH 19/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 37 ++++++------------- .../cqfn/diktat/ruleset/utils/AstNodeUtils.kt | 4 +- 2 files changed, 14 insertions(+), 27 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 d2e4fb7e86..9c4bfa8bae 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 @@ -306,12 +306,11 @@ class LineLength(configRules: List) : DiktatRule( private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT) val nodeSafeAccess = searchRightSplitAfterType(wrongNode, configuration, SAFE_ACCESS) - nodeDot?.let { - return DotQualifiedExpression(wrongNode) + return nodeDot?.let { + DotQualifiedExpression(wrongNode) } ?: nodeSafeAccess?.let { - return DotQualifiedExpression(wrongNode) - } - return None() + DotQualifiedExpression(wrongNode) + } ?: None() } private fun checkFunAndProperty(wrongNode: ASTNode) = @@ -563,7 +562,7 @@ class LineLength(configRules: List) : DiktatRule( /** * This method uses recursion to store dot qualified expression node in the order in which they are located - * Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid) + * Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid)) * *@param node node in which to search *@param dotList mutable list of ASTNode to store nodes @@ -630,7 +629,7 @@ class LineLength(configRules: List) : DiktatRule( } /** - * Finds the first binary expression or dat closer to the separator + * Finds the first binary expression or dot and safe access closer to the separator */ @Suppress("UnsafeCallOnNullableType") private fun searchRightSplitAfterType( @@ -676,29 +675,17 @@ class LineLength(configRules: List) : DiktatRule( private class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) /** - * @property node node + * Class Comment show that long line should be split in comment * @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 */ - - /** - * Class Comment show that long line should be split in comment - * @property hasNewLineBefore - * @property indexLastSpace - */ private class Comment( node: ASTNode, val hasNewLineBefore: Boolean, val indexLastSpace: Int = 0 ) : LongLineFixableCases(node) - /** - * @property node node - * @property delimiterIndex index to split - * @property isOneLineString flag is string is one line - */ - /** * Class StringTemplate show that long line should be split in string template * @property delimiterIndex @@ -718,9 +705,9 @@ class LineLength(configRules: List) : DiktatRule( /** * Class LongBinaryExpression show that long line should be split between other parts long binary expression, * after one of operation reference - * @property maximumLineLength - * @property leftOffset - * @property binList + * @property maximumLineLength is number of maximum line length + * @property leftOffset is offset before start [node] + * @property binList is list of Binary Expression which are children of [node] */ private class LongBinaryExpression( node: ASTNode, @@ -752,8 +739,8 @@ class LineLength(configRules: List) : DiktatRule( /** * Class WhenEntry show that line should be split in WhenEntry node: - * Added LBRACE and RBRACE nodes - * Split line in space after LBRACE node and before RBRACE node + * Added [LBRACE] and [RBRACE] nodes + * Split line in space after [LBRACE] node and before [RBRACE] node */ private class WhenEntry(node: ASTNode) : LongLineFixableCases(node) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt index e1205cd49b..677a959175 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/utils/AstNodeUtils.kt @@ -544,8 +544,8 @@ fun ASTNode.leaveExactlyNumNewLines(num: Int) { } /** - * If [whiteSpaceNode] is not null and has type [WHITE_SPACE], prepend a line break to it's text. - * Otherwise, insert a new node with a line break before [beforeNode] + * If [whiteSpaceNode] is not null and has type [WHITE_SPACE] and this [WHITE_SPACE] contains 1 line, prepend a line break to it's text. + * If [whiteSpaceNode] is null or has`t type [WHITE_SPACE], insert a new node with a line break before [beforeNode] * * @param whiteSpaceNode a node that can possibly be modified * @param beforeNode a node before which a new WHITE_SPACE node will be inserted From e7fc444e60f71e965e78c1de247c0b1949ec65ff Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 14:07:00 +0300 Subject: [PATCH 20/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc in method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 12 +++++++----- 1 file changed, 7 insertions(+), 5 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 9c4bfa8bae..2819c32840 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 @@ -567,16 +567,18 @@ class LineLength(configRules: List) : DiktatRule( *@param node node in which to search *@param dotList mutable list of ASTNode to store nodes */ - private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { - if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION) { + private fun searchDot(node: ASTNode, dotList: MutableList) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION) { node.getChildren(null) .filter { - it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION + it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION || it.elementType == POSTFIX_EXPRESSION } .forEach { - searchDotOrSafeAccess(it, dotList) + searchDot(it, dotList) } - dotList.add(node) + if (node.elementType != POSTFIX_EXPRESSION) { + dotList.add(node) + } } } From 09812b60b32ece698461f443031fcc10f90fd1d0 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 14:09:32 +0300 Subject: [PATCH 21/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc in method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 2819c32840..3701ec9e78 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 @@ -567,14 +567,14 @@ class LineLength(configRules: List) : DiktatRule( *@param node node in which to search *@param dotList mutable list of ASTNode to store nodes */ - private fun searchDot(node: ASTNode, dotList: MutableList) { + private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION) { node.getChildren(null) .filter { it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION || it.elementType == POSTFIX_EXPRESSION } .forEach { - searchDot(it, dotList) + searchDotOrSafeAccess(it, dotList) } if (node.elementType != POSTFIX_EXPRESSION) { dotList.add(node) From a5e91dc998dd0ccca76b531f92ef379428745e53 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 14:24:13 +0300 Subject: [PATCH 22/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc in method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../long_line/LongDotQualifiedExpressionExpected.kt | 5 ++++- .../paragraph3/long_line/LongDotQualifiedExpressionTest.kt | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt index f11b671c12..3bd4b53efd 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -16,4 +16,7 @@ val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot ?.Qualified?.Expression val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! - .Dot!!.Qualified!!.Expression \ No newline at end of file + .Dot!!.Qualified!!.Expression + +val G = +ThisIsVeryLonDotQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt index b841210240..f8d5a8718e 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -13,4 +13,6 @@ val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot ?.Qualified?.Expression val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! - .Dot!!.Qualified!!.Expression \ No newline at end of file + .Dot!!.Qualified!!.Expression + +val G = ThisIsVeryLonDotQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file From a06a4e3a10b0fcdee7f114b46524b6f6cc3677fa Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 14:49:59 +0300 Subject: [PATCH 23/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc in method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 1 + .../long_line/LongDotQualifiedExpressionExpected.kt | 5 ++++- .../paragraph3/long_line/LongDotQualifiedExpressionTest.kt | 4 +++- 3 files changed, 8 insertions(+), 2 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 3701ec9e78..344beed022 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 @@ -93,6 +93,7 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } + println(node.text) } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt index 3bd4b53efd..040fc6fba7 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -1,5 +1,8 @@ package test.paragraph3.long_line +val G = +ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal + val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot .Qualified.Expression @@ -19,4 +22,4 @@ val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! .Dot!!.Qualified!!.Expression val G = -ThisIsVeryLonDotQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file +ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt index f8d5a8718e..507a42ea8e 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -1,5 +1,7 @@ package test.paragraph3.long_line +val G = ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal + val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression val B = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot?.Qualified?.Expression @@ -15,4 +17,4 @@ val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! .Dot!!.Qualified!!.Expression -val G = ThisIsVeryLonDotQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file +val G = ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file From 410a743881cf4622adeed730400a811b15ae6c00 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 16:23:13 +0300 Subject: [PATCH 24/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * corrected code and KDoc in method appendNewlineMergingWhiteSpace in AstNodeUtils.kt * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 344beed022..6d118ce9ae 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 @@ -646,7 +646,7 @@ class LineLength(configRules: List) : DiktatRule( DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) } return list.map { - it to positionByOffset(it.getFirstChildWithType(type)?.startOffset ?: configuration.lineLength.toInt()).second + it to (it.getFirstChildWithType(type)?.startOffset ?: (configuration.lineLength.toInt() + 10)) } .sortedBy { it.second } .reversed() From ada81be677cdd9dbfc7a86210c13dc068ee3801d Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 17:13:43 +0300 Subject: [PATCH 25/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6d118ce9ae..03aef1e4c1 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 @@ -646,7 +646,7 @@ class LineLength(configRules: List) : DiktatRule( DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) } return list.map { - it to (it.getFirstChildWithType(type)?.startOffset ?: (configuration.lineLength.toInt() + 10)) + it to (it.getFirstChildWithType(type)?.startOffset ?: (configuration.lineLength.toInt() + 1)) } .sortedBy { it.second } .reversed() From 417e2d76138f46587fd08478124c9aed75aa8a22 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 17:17:30 +0300 Subject: [PATCH 26/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 1 - 1 file changed, 1 deletion(-) 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 03aef1e4c1..2bcadf72e1 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 @@ -93,7 +93,6 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } - println(node.text) } } From 4d2f87cd1f874a1b85fac00d174c9cf94a900d21 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 19:02:16 +0300 Subject: [PATCH 27/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 52 +++++++++++++++++-- .../LongDotQualifiedExpressionExpected.kt | 5 +- .../LongDotQualifiedExpressionTest.kt | 4 +- 3 files changed, 50 insertions(+), 11 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 2bcadf72e1..239f64b55e 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 @@ -140,7 +140,7 @@ class LineLength(configRules: List) : DiktatRule( findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { - val splitOffset = searchRightSplitAfterType(parent, configuration, OPERATION_REFERENCE)?.second + val splitOffset = searchRightSplitAfterOperationReference(parent, configuration)?.second splitOffset?.let { val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) @@ -304,8 +304,8 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { - val nodeDot = searchRightSplitAfterType(wrongNode, configuration, DOT) - val nodeSafeAccess = searchRightSplitAfterType(wrongNode, configuration, SAFE_ACCESS) + val nodeDot = searchRightSplitBeforeDotOrSafeAccess(wrongNode, configuration, DOT) + val nodeSafeAccess = searchRightSplitBeforeDotOrSafeAccess(wrongNode, configuration, SAFE_ACCESS) return nodeDot?.let { DotQualifiedExpression(wrongNode) } ?: nodeSafeAccess?.let { @@ -645,7 +645,12 @@ class LineLength(configRules: List) : DiktatRule( DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) } return list.map { - it to (it.getFirstChildWithType(type)?.startOffset ?: (configuration.lineLength.toInt() + 1)) + val offset = it.getFirstChildWithType(type)?.run { + positionByOffset(this.startOffset).second + } ?: run { + configuration.lineLength.toInt() + 10 + } + it to offset } .sortedBy { it.second } .reversed() @@ -654,6 +659,45 @@ class LineLength(configRules: List) : DiktatRule( } } + + private fun searchRightSplitAfterOperationReference( + parent: ASTNode, + configuration: LineLengthConfiguration, + ): Pair? { + val list: MutableList = mutableListOf() + searchBinaryExpression(parent, list) + return list.map { + it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + } + .sortedBy { it.second } + .reversed() + .firstOrNull { (it, offset) -> + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 + } + } + + private fun searchRightSplitBeforeDotOrSafeAccess( + parent: ASTNode, + configuration: LineLengthConfiguration, + type: IElementType + ): Pair? { + val list: MutableList = mutableListOf() + searchDotOrSafeAccess(parent, list) + return list.map { + val offset = it.getFirstChildWithType(type)?.run { + positionByOffset(this.startOffset).second + } ?: run { + configuration.lineLength.toInt() + 10 + } + it to offset + } + .sortedBy { it.second } + .reversed() + .firstOrNull { (it, offset) -> + offset <= configuration.lineLength + 1 + } + } + /** * * [RuleConfiguration] for maximum line length diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt index 040fc6fba7..ab9766e750 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionExpected.kt @@ -1,7 +1,7 @@ package test.paragraph3.long_line val G = -ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal + ThisIsVeryyyyLooooonNameDooootQualifiedExpressioWithoutDot.lalalala.lalalal val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot .Qualified.Expression @@ -20,6 +20,3 @@ val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! .Dot!!.Qualified!!.Expression - -val G = -ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file diff --git a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt index 507a42ea8e..9bbde9b17c 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/long_line/LongDotQualifiedExpressionTest.kt @@ -1,6 +1,6 @@ package test.paragraph3.long_line -val G = ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal +val G = ThisIsVeryyyyLooooonNameDooootQualifiedExpressioWithoutDot.lalalala.lalalal val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot.Qualified.Expression @@ -16,5 +16,3 @@ val E = This?.Is?.Veeeeryyyyyyy?.Loooooong?.Dot val F = This!!.Is!!.Veeeeryyyyyyy!!.Loooooong!! .Dot!!.Qualified!!.Expression - -val G = ThisIsVeryyyyLooooonDooootQualifiedExpressioWithoutDot.lalalala.lalalal \ No newline at end of file From ebf5e2b283592eae00bcd9b66173d5c3349418db Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 19:13:21 +0300 Subject: [PATCH 28/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 34 ++++--------------- 1 file changed, 6 insertions(+), 28 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 239f64b55e..ef96fe2064 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 @@ -631,35 +631,9 @@ class LineLength(configRules: List) : DiktatRule( } /** - * Finds the first binary expression or dot and safe access closer to the separator + * Finds the first binary expression closer to the separator */ @Suppress("UnsafeCallOnNullableType") - private fun searchRightSplitAfterType( - parent: ASTNode, - configuration: LineLengthConfiguration, - type: IElementType - ): Pair? { - val list: MutableList = mutableListOf() - when (type) { - OPERATION_REFERENCE -> searchBinaryExpression(parent, list) - DOT, SAFE_ACCESS -> searchDotOrSafeAccess(parent, list) - } - return list.map { - val offset = it.getFirstChildWithType(type)?.run { - positionByOffset(this.startOffset).second - } ?: run { - configuration.lineLength.toInt() + 10 - } - it to offset - } - .sortedBy { it.second } - .reversed() - .firstOrNull { (it, offset) -> - offset + (it.getFirstChildWithType(type)?.text?.length ?: 0) <= configuration.lineLength + 1 - } - } - - private fun searchRightSplitAfterOperationReference( parent: ASTNode, configuration: LineLengthConfiguration, @@ -676,6 +650,10 @@ class LineLength(configRules: List) : DiktatRule( } } + /** + * Finds the first dot or safe access closer to the separator + */ + @Suppress("MAGIC_NUMBER") private fun searchRightSplitBeforeDotOrSafeAccess( parent: ASTNode, configuration: LineLengthConfiguration, @@ -768,7 +746,7 @@ class LineLength(configRules: List) : DiktatRule( private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) /** - * Class Lambda show that long line should be split in Lambda: in space after LBRACE node and before RBRACE node + * Class Lambda show that long line should be split in Lambda: in space after [LBRACE] node and before [RBRACE] node */ private class Lambda(node: ASTNode) : LongLineFixableCases(node) From 6201472ea99b0d3426b8b45601e32dd154d05fa6 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 19:19:36 +0300 Subject: [PATCH 29/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 ef96fe2064..0e1ae31832 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 @@ -653,7 +653,7 @@ class LineLength(configRules: List) : DiktatRule( /** * Finds the first dot or safe access closer to the separator */ - @Suppress("MAGIC_NUMBER") + @Suppress("MagicNumber") private fun searchRightSplitBeforeDotOrSafeAccess( parent: ASTNode, configuration: LineLengthConfiguration, From b7060c1d8400395963a57a893749c2d0cec31960 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 31 May 2022 19:32:26 +0300 Subject: [PATCH 30/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0e1ae31832..ef96fe2064 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 @@ -653,7 +653,7 @@ class LineLength(configRules: List) : DiktatRule( /** * Finds the first dot or safe access closer to the separator */ - @Suppress("MagicNumber") + @Suppress("MAGIC_NUMBER") private fun searchRightSplitBeforeDotOrSafeAccess( parent: ASTNode, configuration: LineLengthConfiguration, From a3630f19d74cd93c957e4e22457f057a9a8a5fd9 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 11:21:35 +0300 Subject: [PATCH 31/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ef96fe2064..c8718a5dcb 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 @@ -661,11 +661,12 @@ class LineLength(configRules: List) : DiktatRule( ): Pair? { val list: MutableList = mutableListOf() searchDotOrSafeAccess(parent, list) + val offsetFromMaximum = 10 return list.map { val offset = it.getFirstChildWithType(type)?.run { positionByOffset(this.startOffset).second } ?: run { - configuration.lineLength.toInt() + 10 + configuration.lineLength.toInt() + offsetFromMaximum } it to offset } From 2bcc2f88c8875ac952aa7e1dc5a878fa4f3f235f Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 11:26:29 +0300 Subject: [PATCH 32/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 c8718a5dcb..67e74061a8 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 @@ -653,7 +653,7 @@ class LineLength(configRules: List) : DiktatRule( /** * Finds the first dot or safe access closer to the separator */ - @Suppress("MAGIC_NUMBER") + @Suppress("MAGIC_NUMBER", "MagicNumber") private fun searchRightSplitBeforeDotOrSafeAccess( parent: ASTNode, configuration: LineLengthConfiguration, From 3a37ead16740b75f59f38bc6829d9c3d76dab5ae Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 12:09:47 +0300 Subject: [PATCH 33/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 104 ++++++++++++------ 1 file changed, 70 insertions(+), 34 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 67e74061a8..fcf9bf7071 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 @@ -103,11 +103,12 @@ class LineLength(configRules: List) : DiktatRule( if (line.length > configuration.lineLength) { val newNode = node.psi.findElementAt(offset + configuration.lineLength.toInt() - 1)!!.node if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || - !isKdocValid(newNode) + !isKdocValid(newNode) ) { positionByOffset = node.treeParent.calculateLineColByOffset() val fixableType = isFixable(newNode, configuration) - LONG_LINE.warnAndFix(configRules, emitWarn, isFixMode, + LONG_LINE.warnAndFix( + configRules, emitWarn, isFixMode, "max line length ${configuration.lineLength}, but was ${line.length}", offset + node.startOffset, node, fixableType !is None ) { @@ -136,13 +137,15 @@ class LineLength(configRules: List) : DiktatRule( do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) + val parentIsValArgListOrFunLitOrWhenEntry = + listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { val splitOffset = searchRightSplitAfterOperationReference(parent, configuration)?.second splitOffset?.let { - val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) + val parentIsBiExprOrParenthesized = + parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) if (parentIsBiExprOrParenthesized || (parentIsFunOrProperty && splitOffset >= configuration.lineLength)) { parent = parent.treeParent @@ -172,7 +175,8 @@ class LineLength(configRules: List) : DiktatRule( FUNCTION_LITERAL -> return Lambda(parent) STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { stringOrDot = parent - val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) + val parentIsBinExpOrValArgListOrWhenEntry = + listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { parent = it } ?: run { @@ -243,11 +247,11 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -274,7 +278,8 @@ class LineLength(configRules: List) : DiktatRule( } else { positionByOffset(node.startOffset).second } - val delimiterIndex = node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + val delimiterIndex = + node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') if (delimiterIndex == -1) { // we can't split this string, however may be we can move it entirely: // case when new line should be inserted after `+`. Example: "first" + "second" @@ -291,7 +296,8 @@ class LineLength(configRules: List) : DiktatRule( return 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 { @@ -303,7 +309,10 @@ class LineLength(configRules: List) : DiktatRule( return StringTemplate(node, correcterDelimiter, multiLineOffset == 0) } - private fun parserDotQualifiedExpression(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { + private fun parserDotQualifiedExpression( + wrongNode: ASTNode, + configuration: LineLengthConfiguration + ): LongLineFixableCases { val nodeDot = searchRightSplitBeforeDotOrSafeAccess(wrongNode, configuration, DOT) val nodeSafeAccess = searchRightSplitBeforeDotOrSafeAccess(wrongNode, configuration, SAFE_ACCESS) return nodeDot?.let { @@ -314,7 +323,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun checkFunAndProperty(wrongNode: ASTNode) = - if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() + if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() private fun checkComment(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val leftOffset = positionByOffset(wrongNode.startOffset).second @@ -322,8 +331,12 @@ class LineLength(configRules: List) : DiktatRule( if (stringBeforeCommentContent.length >= configuration.lineLength.toInt() - leftOffset) { return None() } - val indexLastSpace = wrongNode.text.substring(stringBeforeCommentContent.length, configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') - val isNewLine = wrongNode.treePrev?.isWhiteSpaceWithNewline() ?: wrongNode.treeParent?.treePrev?.isWhiteSpaceWithNewline() ?: false + val indexLastSpace = + wrongNode.text.substring(stringBeforeCommentContent.length, configuration.lineLength.toInt() - leftOffset) + .lastIndexOf(' ') + val isNewLine = + wrongNode.treePrev?.isWhiteSpaceWithNewline() ?: wrongNode.treeParent?.treePrev?.isWhiteSpaceWithNewline() + ?: false if (isNewLine && indexLastSpace == -1) { return None() } @@ -355,7 +368,10 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("UnsafeCallOnNullableType", "WHEN_WITHOUT_ELSE") private fun fixError(fixableType: LongLineFixableCases) { when (fixableType) { - is FunAndProperty -> fixableType.node.appendNewlineMergingWhiteSpace(null, fixableType.node.findChildByType(EQ)!!.treeNext) + is FunAndProperty -> fixableType.node.appendNewlineMergingWhiteSpace( + null, + fixableType.node.findChildByType(EQ)!!.treeNext + ) is Comment -> fixComment(fixableType) is LongBinaryExpression -> fixLongBinaryExpression(fixableType) is BinaryExpression -> fixBinaryExpression(fixableType.node) @@ -395,12 +411,18 @@ class LineLength(configRules: List) : DiktatRule( var startOffset = 0 node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace( + node.findChildByType(LPAR)!!.treeNext, + node.findChildByType(LPAR)!!.treeNext + ) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } } ?: node.getFirstChildWithType(RPAR)?.let { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace( + node.findChildByType(LPAR)!!.treeNext, + node.findChildByType(LPAR)!!.treeNext + ) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } @@ -462,7 +484,10 @@ class LineLength(configRules: List) : DiktatRule( val parent = wrongNode.treeParent parent.removeChild(wrongNode) newLineNodeOnPreviousLine.treeParent.addChild(wrongNode, newLineNodeOnPreviousLine.treeNext) - newLineNodeOnPreviousLine.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newLineNodeOnPreviousLine.treeNext.treeNext) + newLineNodeOnPreviousLine.treeParent.addChild( + PsiWhiteSpaceImpl("\n"), + newLineNodeOnPreviousLine.treeNext.treeNext + ) } } } @@ -492,8 +517,14 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun fixLambda(node: ASTNode) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) + node.appendNewlineMergingWhiteSpace( + node.findChildByType(LBRACE)!!.treeNext, + node.findChildByType(LBRACE)!!.treeNext + ) + node.appendNewlineMergingWhiteSpace( + node.findChildByType(RBRACE)!!.treePrev, + node.findChildByType(RBRACE)!!.treePrev + ) } @Suppress("UnsafeCallOnNullableType", "COMMENT_WHITE_SPACE") @@ -502,11 +533,11 @@ class LineLength(configRules: List) : DiktatRule( val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex) val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length) val textBetweenParts = - if (wrongStringTemplate.isOneLineString) { - "\" +\n\"" - } else { - "\n" - } + if (wrongStringTemplate.isOneLineString) { + "\" +\n\"" + } else { + "\n" + } val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, correctNode) } @@ -516,7 +547,8 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongBinaryExpression) { - val anySplitNode = searchSomeSplitInBinaryExpression(wrongBinaryExpression.node, wrongBinaryExpression.maximumLineLength) + val anySplitNode = + searchSomeSplitInBinaryExpression(wrongBinaryExpression.node, wrongBinaryExpression.maximumLineLength) val rigthSplitnode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] val nodeOperationReference = rigthSplitnode?.first?.getFirstChildWithType(OPERATION_REFERENCE) rigthSplitnode?.let { @@ -544,7 +576,8 @@ class LineLength(configRules: List) : DiktatRule( */ private fun searchBinaryExpression(node: ASTNode, binList: MutableList) { if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || - node.hasChildOfType(POSTFIX_EXPRESSION)) { + node.hasChildOfType(POSTFIX_EXPRESSION) + ) { node.getChildren(null) .filter { it.elementType == BINARY_EXPRESSION || it.elementType == PARENTHESIZED || @@ -560,6 +593,9 @@ class LineLength(configRules: List) : DiktatRule( } } + private fun isTypeDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode):Boolean = + node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION + /** * This method uses recursion to store dot qualified expression node in the order in which they are located * Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid)) @@ -568,10 +604,10 @@ class LineLength(configRules: List) : DiktatRule( *@param dotList mutable list of ASTNode to store nodes */ private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { - if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION) { + if (isTypeDotQuaOrSafeAccessOrPostfixExpression(node)) { node.getChildren(null) .filter { - it.elementType == DOT_QUALIFIED_EXPRESSION || it.elementType == SAFE_ACCESS_EXPRESSION || it.elementType == POSTFIX_EXPRESSION + isTypeDotQuaOrSafeAccessOrPostfixExpression(it) } .forEach { searchDotOrSafeAccess(it, dotList) @@ -662,7 +698,8 @@ class LineLength(configRules: List) : DiktatRule( val list: MutableList = mutableListOf() searchDotOrSafeAccess(parent, list) val offsetFromMaximum = 10 - return list.map { + return list.asSequence() + .map { val offset = it.getFirstChildWithType(type)?.run { positionByOffset(this.startOffset).second } ?: run { @@ -671,8 +708,7 @@ class LineLength(configRules: List) : DiktatRule( it to offset } .sortedBy { it.second } - .reversed() - .firstOrNull { (it, offset) -> + .lastOrNull { (it, offset) -> offset <= configuration.lineLength + 1 } } From 21c44f13c678332baa1cb2654fcf58232ecd7cca Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 12:29:14 +0300 Subject: [PATCH 34/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 72 ++++++------------- 1 file changed, 23 insertions(+), 49 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 fcf9bf7071..bc2681c7ea 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 @@ -102,9 +102,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 - if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || - !isKdocValid(newNode) - ) { + if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) || !isKdocValid(newNode)) { positionByOffset = node.treeParent.calculateLineColByOffset() val fixableType = isFixable(newNode, configuration) LONG_LINE.warnAndFix( @@ -137,15 +135,13 @@ class LineLength(configRules: List) : DiktatRule( do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - val parentIsValArgListOrFunLitOrWhenEntry = - listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) + val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { val splitOffset = searchRightSplitAfterOperationReference(parent, configuration)?.second splitOffset?.let { - val parentIsBiExprOrParenthesized = - parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) + val parentIsBiExprOrParenthesized = parent.treeParent.elementType in listOf(BINARY_EXPRESSION, PARENTHESIZED) val parentIsFunOrProperty = parent.treeParent.elementType in listOf(FUN, PROPERTY) if (parentIsBiExprOrParenthesized || (parentIsFunOrProperty && splitOffset >= configuration.lineLength)) { parent = parent.treeParent @@ -175,8 +171,7 @@ class LineLength(configRules: List) : DiktatRule( FUNCTION_LITERAL -> return Lambda(parent) STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { stringOrDot = parent - val parentIsBinExpOrValArgListOrWhenEntry = - listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) + val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { parent = it } ?: run { @@ -331,12 +326,8 @@ class LineLength(configRules: List) : DiktatRule( if (stringBeforeCommentContent.length >= configuration.lineLength.toInt() - leftOffset) { return None() } - val indexLastSpace = - wrongNode.text.substring(stringBeforeCommentContent.length, configuration.lineLength.toInt() - leftOffset) - .lastIndexOf(' ') - val isNewLine = - wrongNode.treePrev?.isWhiteSpaceWithNewline() ?: wrongNode.treeParent?.treePrev?.isWhiteSpaceWithNewline() - ?: false + val indexLastSpace = wrongNode.text.substring(stringBeforeCommentContent.length, configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + val isNewLine = wrongNode.treePrev?.isWhiteSpaceWithNewline() ?: wrongNode.treeParent?.treePrev?.isWhiteSpaceWithNewline() ?: false if (isNewLine && indexLastSpace == -1) { return None() } @@ -411,18 +402,12 @@ class LineLength(configRules: List) : DiktatRule( var startOffset = 0 node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { - node.appendNewlineMergingWhiteSpace( - node.findChildByType(LPAR)!!.treeNext, - node.findChildByType(LPAR)!!.treeNext - ) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } } ?: node.getFirstChildWithType(RPAR)?.let { - node.appendNewlineMergingWhiteSpace( - node.findChildByType(LPAR)!!.treeNext, - node.findChildByType(LPAR)!!.treeNext - ) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() } @@ -484,10 +469,7 @@ class LineLength(configRules: List) : DiktatRule( val parent = wrongNode.treeParent parent.removeChild(wrongNode) newLineNodeOnPreviousLine.treeParent.addChild(wrongNode, newLineNodeOnPreviousLine.treeNext) - newLineNodeOnPreviousLine.treeParent.addChild( - PsiWhiteSpaceImpl("\n"), - newLineNodeOnPreviousLine.treeNext.treeNext - ) + newLineNodeOnPreviousLine.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newLineNodeOnPreviousLine.treeNext.treeNext) } } } @@ -517,14 +499,8 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("UnsafeCallOnNullableType") private fun fixLambda(node: ASTNode) { - node.appendNewlineMergingWhiteSpace( - node.findChildByType(LBRACE)!!.treeNext, - node.findChildByType(LBRACE)!!.treeNext - ) - node.appendNewlineMergingWhiteSpace( - node.findChildByType(RBRACE)!!.treePrev, - node.findChildByType(RBRACE)!!.treePrev - ) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) } @Suppress("UnsafeCallOnNullableType", "COMMENT_WHITE_SPACE") @@ -575,9 +551,7 @@ class LineLength(configRules: List) : DiktatRule( *@param binList mutable list of ASTNode to store nodes */ private fun searchBinaryExpression(node: ASTNode, binList: MutableList) { - if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || - node.hasChildOfType(POSTFIX_EXPRESSION) - ) { + if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || node.hasChildOfType(POSTFIX_EXPRESSION)) { node.getChildren(null) .filter { it.elementType == BINARY_EXPRESSION || it.elementType == PARENTHESIZED || @@ -676,12 +650,12 @@ class LineLength(configRules: List) : DiktatRule( ): Pair? { val list: MutableList = mutableListOf() searchBinaryExpression(parent, list) - return list.map { - it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second - } + return list.asSequence() + .map { + it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + } .sortedBy { it.second } - .reversed() - .firstOrNull { (it, offset) -> + .lastOrNull { (it, offset) -> offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 } } @@ -700,13 +674,13 @@ class LineLength(configRules: List) : DiktatRule( val offsetFromMaximum = 10 return list.asSequence() .map { - val offset = it.getFirstChildWithType(type)?.run { - positionByOffset(this.startOffset).second - } ?: run { - configuration.lineLength.toInt() + offsetFromMaximum + val offset = it.getFirstChildWithType(type)?.run { + positionByOffset(this.startOffset).second + } ?: run { + configuration.lineLength.toInt() + offsetFromMaximum + } + it to offset } - it to offset - } .sortedBy { it.second } .lastOrNull { (it, offset) -> offset <= configuration.lineLength + 1 From 778ee661cd1a327116d25eb9b5508552552e23c6 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 12:37:31 +0300 Subject: [PATCH 35/52] ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * correct code with running detect ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 32 +++++++++---------- 1 file changed, 16 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 bc2681c7ea..c0a1f21e1a 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 @@ -242,11 +242,11 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -274,7 +274,7 @@ class LineLength(configRules: List) : DiktatRule( positionByOffset(node.startOffset).second } val delimiterIndex = - node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') if (delimiterIndex == -1) { // we can't split this string, however may be we can move it entirely: // case when new line should be inserted after `+`. Example: "first" + "second" @@ -292,7 +292,7 @@ class LineLength(configRules: List) : DiktatRule( } // 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) + (multiLineOffset == 0) && (leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2) val correcterDelimiter = if (shouldAddTwoSpaces) { node.text.substring(0, delimiterIndex - 2).lastIndexOf(' ') } else { @@ -318,7 +318,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun checkFunAndProperty(wrongNode: ASTNode) = - if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() + if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() private fun checkComment(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val leftOffset = positionByOffset(wrongNode.startOffset).second @@ -509,11 +509,11 @@ class LineLength(configRules: List) : DiktatRule( val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex) val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length) val textBetweenParts = - if (wrongStringTemplate.isOneLineString) { - "\" +\n\"" - } else { - "\n" - } + if (wrongStringTemplate.isOneLineString) { + "\" +\n\"" + } else { + "\n" + } val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, correctNode) } @@ -524,7 +524,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("UnsafeCallOnNullableType") private fun fixLongBinaryExpression(wrongBinaryExpression: LongBinaryExpression) { val anySplitNode = - searchSomeSplitInBinaryExpression(wrongBinaryExpression.node, wrongBinaryExpression.maximumLineLength) + searchSomeSplitInBinaryExpression(wrongBinaryExpression.node, wrongBinaryExpression.maximumLineLength) val rigthSplitnode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] val nodeOperationReference = rigthSplitnode?.first?.getFirstChildWithType(OPERATION_REFERENCE) rigthSplitnode?.let { @@ -567,8 +567,8 @@ class LineLength(configRules: List) : DiktatRule( } } - private fun isTypeDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode):Boolean = - node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION + private fun isTypeDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode): Boolean = + node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION /** * This method uses recursion to store dot qualified expression node in the order in which they are located From 7c1675c1a69e86f349a1529ecda1d3cc8df9ccfd Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 1 Jun 2022 16:42:38 +0300 Subject: [PATCH 36/52] merged condition and other logics, fixed recursive logic, renamed function, delete IElementType ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code with running detect * corrected commented code ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 33 ++++--------------- 1 file changed, 6 insertions(+), 27 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 c0a1f21e1a..cd705877ed 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 @@ -136,7 +136,7 @@ class LineLength(configRules: List) : DiktatRule( when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) - findParentNodeWithSpecificTypeMany(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { + findParentNodeMatching(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { val splitOffset = searchRightSplitAfterOperationReference(parent, configuration)?.second @@ -161,7 +161,6 @@ class LineLength(configRules: List) : DiktatRule( } } FUN, PROPERTY -> return checkFunAndProperty(parent) - CONDITION -> return checkCondition(parent, configuration) VALUE_ARGUMENT_LIST -> parent.findParentNodeWithSpecificType(BINARY_EXPRESSION)?.let { parent = it } ?: return checkArgumentsList(parent, configuration) @@ -171,8 +170,8 @@ class LineLength(configRules: List) : DiktatRule( FUNCTION_LITERAL -> return Lambda(parent) STRING_TEMPLATE, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION -> { stringOrDot = parent - val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) - findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { + val parentIsBinExpOrValArgListOrWhenEntry = listOf(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION) + findParentNodeMatching(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let { parent = it } ?: run { val returnElem = checkStringTemplateAndDotQualifiedExpression(parent, configuration) @@ -188,7 +187,7 @@ class LineLength(configRules: List) : DiktatRule( return None() } - private fun findParentNodeWithSpecificTypeMany(node: ASTNode, listType: List): ASTNode? { + private fun findParentNodeMatching(node: ASTNode, listType: List): ASTNode? { listType.forEach { type -> node.findParentNodeWithSpecificType(type)?.let { return it @@ -225,7 +224,7 @@ class LineLength(configRules: List) : DiktatRule( configuration: LineLengthConfiguration ): LongLineFixableCases { val isPropertyOrFun = listOf(PROPERTY, FUN) - val funOrPropertyNode = findParentNodeWithSpecificTypeMany(node, isPropertyOrFun) + val funOrPropertyNode = findParentNodeMatching(node, isPropertyOrFun) funOrPropertyNode?.let { if (it.hasChildOfType(EQ)) { val positionByOffset = positionByOffset(it.getFirstChildWithType(EQ)?.startOffset ?: 0).second @@ -334,16 +333,6 @@ class LineLength(configRules: List) : DiktatRule( return Comment(wrongNode, isNewLine, indexLastSpace + stringBeforeCommentContent.length) } - private fun checkCondition(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { - val leftOffset = positionByOffset(wrongNode.firstChildNode.startOffset).second - val binList: MutableList = mutableListOf() - searchBinaryExpression(wrongNode, binList) - if (binList.size == 1) { - return BinaryExpression(wrongNode) - } - return LongBinaryExpression(wrongNode, configuration, leftOffset, binList) - } - // fixme json method private fun isKdocValid(node: ASTNode) = try { if (node.elementType == KDOC_TEXT) { @@ -553,10 +542,6 @@ class LineLength(configRules: List) : DiktatRule( private fun searchBinaryExpression(node: ASTNode, binList: MutableList) { if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || node.hasChildOfType(POSTFIX_EXPRESSION)) { node.getChildren(null) - .filter { - it.elementType == BINARY_EXPRESSION || it.elementType == PARENTHESIZED || - it.elementType == POSTFIX_EXPRESSION - } .forEach { searchBinaryExpression(it, binList) } @@ -567,9 +552,6 @@ class LineLength(configRules: List) : DiktatRule( } } - private fun isTypeDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode): Boolean = - node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION - /** * This method uses recursion to store dot qualified expression node in the order in which they are located * Also dotList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid)) @@ -578,11 +560,8 @@ class LineLength(configRules: List) : DiktatRule( *@param dotList mutable list of ASTNode to store nodes */ private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList) { - if (isTypeDotQuaOrSafeAccessOrPostfixExpression(node)) { + if (node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION) { node.getChildren(null) - .filter { - isTypeDotQuaOrSafeAccessOrPostfixExpression(it) - } .forEach { searchDotOrSafeAccess(it, dotList) } From 79b4040353d4038cbce80aefbdf1885ee2ae81d4 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 17:54:23 +0300 Subject: [PATCH 37/52] moved fix logic to class methods ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code with running detect * corrected commented code ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 495 +++++++++--------- 1 file changed, 249 insertions(+), 246 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 cd705877ed..6122f4818d 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 @@ -14,7 +14,6 @@ import com.pinterest.ktlint.core.ast.ElementType.BLOCK import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT import com.pinterest.ktlint.core.ast.ElementType.COMMA -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.ELVIS @@ -84,7 +83,7 @@ class LineLength(configRules: List) : DiktatRule( configRules.getRuleConfig(LONG_LINE)?.configuration ?: emptyMap() ) } - private lateinit var positionByOffset: (Int) -> Pair + lateinit var positionByOffset: (Int) -> Pair override fun logic(node: ASTNode) { if (node.elementType == FILE) { @@ -93,6 +92,7 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } + println(node.text) } } @@ -112,7 +112,7 @@ class LineLength(configRules: List) : DiktatRule( ) { // we should keep in mind, that in the course of fixing we change the offset val textLenBeforeFix = node.textLength - fixError(fixableType) + fixableType.fix() val textLenAfterFix = node.textLength // offset for all next nodes changed to this delta offset += (textLenAfterFix - textLenBeforeFix) @@ -135,7 +135,7 @@ class LineLength(configRules: List) : DiktatRule( do { when (parent.elementType) { BINARY_EXPRESSION, PARENTHESIZED -> { - val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) + val parentIsValArgListOrFunLitOrWhenEntry = listOf(VALUE_ARGUMENT_LIST, FUNCTION_LITERAL, WHEN_CONDITION_WITH_EXPRESSION) findParentNodeMatching(parent, parentIsValArgListOrFunLitOrWhenEntry)?.let { parent = it } ?: run { @@ -199,10 +199,10 @@ class LineLength(configRules: List) : DiktatRule( private fun checkArgumentsList(node: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { node.findParentNodeWithSpecificType(WHEN_ENTRY)?.let { it.findChildByType(BLOCK)?.run { - return ValueArgumentList(node, configuration) + return ValueArgumentList(node, configuration, positionByOffset) } ?: return WhenEntry(it) } - return ValueArgumentList(node, configuration) + return ValueArgumentList(node, configuration, positionByOffset) } /** @@ -215,7 +215,7 @@ class LineLength(configRules: List) : DiktatRule( if (binList.size == 1) { return BinaryExpression(node) } - return LongBinaryExpression(node, configuration, leftOffset, binList) + return LongBinaryExpression(node, configuration, leftOffset, binList, positionByOffset) } @Suppress("TOO_MANY_LINES_IN_LAMBDA", "GENERIC_VARIABLE_WRONG_DECLARATION") @@ -223,7 +223,7 @@ class LineLength(configRules: List) : DiktatRule( node: ASTNode, configuration: LineLengthConfiguration ): LongLineFixableCases { - val isPropertyOrFun = listOf(PROPERTY, FUN) + val isPropertyOrFun = listOf(PROPERTY, FUN) val funOrPropertyNode = findParentNodeMatching(node, isPropertyOrFun) funOrPropertyNode?.let { if (it.hasChildOfType(EQ)) { @@ -241,11 +241,11 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -273,7 +273,7 @@ class LineLength(configRules: List) : DiktatRule( positionByOffset(node.startOffset).second } val delimiterIndex = - node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') if (delimiterIndex == -1) { // we can't split this string, however may be we can move it entirely: // case when new line should be inserted after `+`. Example: "first" + "second" @@ -291,7 +291,7 @@ class LineLength(configRules: List) : DiktatRule( } // 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) + (multiLineOffset == 0) && (leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2) val correcterDelimiter = if (shouldAddTwoSpaces) { node.text.substring(0, delimiterIndex - 2).lastIndexOf(' ') } else { @@ -317,7 +317,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun checkFunAndProperty(wrongNode: ASTNode) = - if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() + if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() private fun checkComment(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val leftOffset = positionByOffset(wrongNode.startOffset).second @@ -345,193 +345,6 @@ class LineLength(configRules: List) : DiktatRule( false } - @Suppress("UnsafeCallOnNullableType", "WHEN_WITHOUT_ELSE") - private fun fixError(fixableType: LongLineFixableCases) { - when (fixableType) { - is FunAndProperty -> fixableType.node.appendNewlineMergingWhiteSpace( - null, - fixableType.node.findChildByType(EQ)!!.treeNext - ) - is Comment -> fixComment(fixableType) - is LongBinaryExpression -> fixLongBinaryExpression(fixableType) - is BinaryExpression -> fixBinaryExpression(fixableType.node) - is StringTemplate -> fixStringTemplate(fixableType) - is DotQualifiedExpression -> fixDotQualifiedExpression(fixableType) - is ValueArgumentList -> fixArgumentList(fixableType) - is Lambda -> fixLambda(fixableType.node) - is WhenEntry -> fixWhenEntry(fixableType) - is None -> return - } - } - - private fun fixWhenEntry(wrongWhenEntry: WhenEntry) { - val node = wrongWhenEntry.node - node.getFirstChildWithType(ARROW)?.let { - node.appendNewlineMergingWhiteSpace(it.treeNext, it.treeNext) - } - } - - private fun fixDotQualifiedExpression(wrongDotQualifiedExpression: DotQualifiedExpression) { - val node = wrongDotQualifiedExpression.node - val dot = node.getFirstChildWithType(DOT) - val safeAccess = node.getFirstChildWithType(SAFE_ACCESS) - val splitNode = if ((dot?.startOffset ?: 0) > (safeAccess?.startOffset ?: 0)) { - dot - } else { - safeAccess - } - val nodeBeforeDot = splitNode?.treePrev - node.appendNewlineMergingWhiteSpace(nodeBeforeDot, splitNode) - } - - @Suppress("UnsafeCallOnNullableType", "MagicNumber") - private fun fixArgumentsListFirstArgument(wrongArgumentList: ValueArgumentList): Int { - val lineLength = wrongArgumentList.maximumLineLength.lineLength - val node = wrongArgumentList.node - var startOffset = 0 - node.getFirstChildWithType(COMMA)?.let { - if (positionByOffset(it.startOffset).second > lineLength) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() - } - } ?: node.getFirstChildWithType(RPAR)?.let { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) - startOffset = wrongArgumentList.maximumLineLength.lineLength.toInt() - } - return startOffset - } - - /** - * Fix arguments in arguments list - */ - private fun fixArgumentList(wrongArgumentList: ValueArgumentList) { - val lineLength = wrongArgumentList.maximumLineLength.lineLength - val node = wrongArgumentList.node - val offset = fixArgumentsListFirstArgument(wrongArgumentList) - val listComma = node.getAllChildrenWithType(COMMA).map { - it to positionByOffset(it.startOffset - offset).second - }.sortedBy { it.second } - var lineNumber = 1 - listComma.forEachIndexed { index, pair -> - if (pair.second >= lineNumber * lineLength) { - lineNumber++ - val commaSplit = if (index > 0) { - listComma[index - 1].first - } else { - pair.first - } - node.appendNewlineMergingWhiteSpace(commaSplit.treeNext, commaSplit.treeNext) - } - } - node.getFirstChildWithType(RPAR)?.let { - if (positionByOffset(it.treePrev.startOffset).second + it.treePrev.text.length - offset > lineLength * lineNumber && listComma.isNotEmpty()) { - listComma.last().first.run { - node.appendNewlineMergingWhiteSpace(this.treeNext, this.treeNext) - } - } - } - } - - private fun fixComment(wrongComment: Comment) { - val wrongNode = wrongComment.node - if (wrongComment.hasNewLineBefore) { - val indexLastSpace = wrongComment.indexLastSpace - val nodeText = "//${wrongNode.text.substring(indexLastSpace, wrongNode.text.length)}" - wrongNode.treeParent.apply { - addChild(LeafPsiElement(EOL_COMMENT, wrongNode.text.substring(0, indexLastSpace)), wrongNode) - addChild(PsiWhiteSpaceImpl("\n"), wrongNode) - addChild(LeafPsiElement(EOL_COMMENT, nodeText), wrongNode) - removeChild(wrongNode) - } - } else { - if (wrongNode.treePrev.isWhiteSpace()) { - wrongNode.treeParent.removeChild(wrongNode.treePrev) - } - - val newLineNodeOnPreviousLine = wrongNode.findAllNodesWithConditionOnLine(wrongNode.getLineNumber() - 1) { - it.elementType == WHITE_SPACE && it.textContains('\n') - }?.lastOrNull() - - newLineNodeOnPreviousLine?.let { - val parent = wrongNode.treeParent - parent.removeChild(wrongNode) - newLineNodeOnPreviousLine.treeParent.addChild(wrongNode, newLineNodeOnPreviousLine.treeNext) - newLineNodeOnPreviousLine.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newLineNodeOnPreviousLine.treeNext.treeNext) - } - } - } - - /** - * Fix a binary expression - - * If the transfer is done on the Elvis operator, then transfers it to a new line - * If not on the Elvis operator, then transfers it to a new line after the operation reference - */ - @Suppress("UnsafeCallOnNullableType") - private fun fixBinaryExpression(node: ASTNode) { - val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE) - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext - } else { - if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { - nodeOperationReference.treePrev - } else { - nodeOperationReference - } - } - node.appendNewlineMergingWhiteSpace(nextNode, nextNode) - } - - /** - * Splits Lambda expressions - add splits lines, thereby making the lambda expression a separate line - */ - @Suppress("UnsafeCallOnNullableType") - private fun fixLambda(node: ASTNode) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) - } - - @Suppress("UnsafeCallOnNullableType", "COMMENT_WHITE_SPACE") - private fun fixStringTemplate(wrongStringTemplate: StringTemplate) { - val incorrectText = wrongStringTemplate.node.text - val firstPart = incorrectText.substring(0, wrongStringTemplate.delimiterIndex) - val secondPart = incorrectText.substring(wrongStringTemplate.delimiterIndex, incorrectText.length) - val textBetweenParts = - if (wrongStringTemplate.isOneLineString) { - "\" +\n\"" - } else { - "\n" - } - val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") - wrongStringTemplate.node.treeParent.replaceChild(wrongStringTemplate.node, correctNode) - } - - /** - * Finds where it is better to fix a Binary expression and fixes it - */ - @Suppress("UnsafeCallOnNullableType") - private fun fixLongBinaryExpression(wrongBinaryExpression: LongBinaryExpression) { - val anySplitNode = - searchSomeSplitInBinaryExpression(wrongBinaryExpression.node, wrongBinaryExpression.maximumLineLength) - val rigthSplitnode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] - val nodeOperationReference = rigthSplitnode?.first?.getFirstChildWithType(OPERATION_REFERENCE) - rigthSplitnode?.let { - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext - } else { - if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { - nodeOperationReference.treePrev - } else { - nodeOperationReference - } - } - if (!nextNode.text.contains(("\n"))) { - rigthSplitnode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) - } - } - } - /** * This method uses recursion to store binary node in the order in which they are located * Also binList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid) @@ -571,35 +384,6 @@ class LineLength(configRules: List) : DiktatRule( } } - /** - * This method stored all the nodes that have BINARY_EXPRESSION or PREFIX_EXPRESSION element type. - * Return List of the Pair - * First elem in List - Logic Binary Expression (&& ||) - * Second elem in List - Comparison Binary Expression (> < == >= <= !=) - * Other types (Arithmetical and Bit operation) (+ - * / % >> << *= += -= /= %= ++ -- ! in !in etc) - */ - @Suppress("TYPE_ALIAS", "UnsafeCallOnNullableType") - private fun searchSomeSplitInBinaryExpression(parent: ASTNode, configuration: LineLengthConfiguration): List?> { - val logicListOperationReference = listOf(OROR, ANDAND) - val compressionListOperationReference = listOf(GT, LT, EQEQ, GTEQ, LTEQ, EXCLEQ) - val binList: MutableList = mutableListOf() - searchBinaryExpression(parent, binList) - val rightBinList = binList.map { - it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second - } - .sortedBy { it.second } - .reversed() - val returnList: MutableList?> = mutableListOf() - addInSmartListBinExpression(returnList, rightBinList, logicListOperationReference, configuration) - addInSmartListBinExpression(returnList, rightBinList, compressionListOperationReference, configuration) - val expression = rightBinList.firstOrNull { (it, offset) -> - val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)!!.firstChildNode.elementType - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL - } - returnList.add(expression) - return returnList - } /** * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] @@ -665,7 +449,8 @@ class LineLength(configRules: List) : DiktatRule( offset <= configuration.lineLength + 1 } } - + + /** * * [RuleConfiguration] for maximum line length @@ -681,12 +466,17 @@ class LineLength(configRules: List) : DiktatRule( * Class LongLineFixableCases is parent class for several specific error classes */ @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs - sealed class LongLineFixableCases(val node: ASTNode) + abstract class LongLineFixableCases(val node: ASTNode){ + //var positionByOffset = node.treeParent.calculateLineColByOffset() + abstract fun fix() + } /** * Class None show error long line have unidentified type or something else that we can't analyze */ - private class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) + class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) { + override fun fix() {} + } /** * Class Comment show that long line should be split in comment @@ -694,11 +484,37 @@ class LineLength(configRules: List) : DiktatRule( * and inline comments (which should be moved entirely to the previous line) * @property indexLastSpace index of last space to substring comment */ - private class Comment( + class Comment( node: ASTNode, val hasNewLineBefore: Boolean, val indexLastSpace: Int = 0 - ) : LongLineFixableCases(node) + ) : LongLineFixableCases(node) { + override fun fix() { + if (this.hasNewLineBefore) { + val indexLastSpace = this.indexLastSpace + val nodeText = "//${node.text.substring(indexLastSpace, node.text.length)}" + node.treeParent.apply { + addChild(LeafPsiElement(EOL_COMMENT, node.text.substring(0, indexLastSpace)), node) + addChild(PsiWhiteSpaceImpl("\n"), node) + addChild(LeafPsiElement(EOL_COMMENT, nodeText), node) + removeChild(node) + } + } else { + if (node.treePrev.isWhiteSpace()) { + node.treeParent.removeChild(node.treePrev) + } + val newLineNodeOnPreviousLine = node.findAllNodesWithConditionOnLine(node.getLineNumber() - 1) { + it.elementType == WHITE_SPACE && it.textContains('\n') + }?.lastOrNull() + newLineNodeOnPreviousLine?.let { + val parent = node.treeParent + parent.removeChild(node) + newLineNodeOnPreviousLine.treeParent.addChild(node, newLineNodeOnPreviousLine.treeNext) + newLineNodeOnPreviousLine.treeParent.addChild(PsiWhiteSpaceImpl("\n"), newLineNodeOnPreviousLine.treeNext.treeNext) + } + } + } + } /** * Class StringTemplate show that long line should be split in string template @@ -709,12 +525,40 @@ class LineLength(configRules: List) : DiktatRule( node: ASTNode, val delimiterIndex: Int, val isOneLineString: Boolean - ) : LongLineFixableCases(node) + ) : LongLineFixableCases(node) { + override fun fix() { + val incorrectText = node.text + val firstPart = incorrectText.substring(0, delimiterIndex) + val secondPart = incorrectText.substring(delimiterIndex, incorrectText.length) + val textBetweenParts = + if (isOneLineString) { + "\" +\n\"" + } else { + "\n" + } + val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") + node.treeParent.replaceChild(node, correctNode) + } + } /** * Class BinaryExpression show that long line should be split in short binary expression? after operation reference */ - private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) + private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) { + override fun fix() { + val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE) + val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { + nodeOperationReference.treeNext + } else { + if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { + nodeOperationReference.treePrev + } else { + nodeOperationReference + } + } + node.appendNewlineMergingWhiteSpace(nextNode, nextNode) + } + } /** * Class LongBinaryExpression show that long line should be split between other parts long binary expression, @@ -727,36 +571,195 @@ class LineLength(configRules: List) : DiktatRule( node: ASTNode, val maximumLineLength: LineLengthConfiguration, val leftOffset: Int, - val binList: MutableList - ) : LongLineFixableCases(node) + val binList: MutableList, + var positionByOffset: (Int) -> Pair + ) : LongLineFixableCases(node) { + /** + * Fix a binary expression - + * If the transfer is done on the Elvis operator, then transfers it to a new line + * If not on the Elvis operator, then transfers it to a new line after the operation reference + */ + override fun fix() { + val anySplitNode = searchSomeSplitInBinaryExpression(node, maximumLineLength) + val rightSplitNode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] + val nodeOperationReference = rightSplitNode?.first?.getFirstChildWithType(OPERATION_REFERENCE) + rightSplitNode?.let { + val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { + nodeOperationReference.treeNext + } else { + if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { + nodeOperationReference.treePrev + } else { + nodeOperationReference + } + } + if (!nextNode.text.contains(("\n"))) { + rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) + } + } + } + + /** + * This method stored all the nodes that have BINARY_EXPRESSION or PREFIX_EXPRESSION element type. + * Return List of the Pair + * First elem in List - Logic Binary Expression (&& ||) + * Second elem in List - Comparison Binary Expression (> < == >= <= !=) + * Other types (Arithmetical and Bit operation) (+ - * / % >> << *= += -= /= %= ++ -- ! in !in etc) + */ + private fun searchSomeSplitInBinaryExpression(parent: ASTNode, configuration: LineLengthConfiguration): List?> { + val logicListOperationReference = listOf(OROR, ANDAND) + val compressionListOperationReference = listOf(GT, LT, EQEQ, GTEQ, LTEQ, EXCLEQ) + val binList: MutableList = mutableListOf() + searchBinaryExpression(parent, binList) + val rightBinList = binList.map { + it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + } + .sortedBy { it.second } + .reversed() + val returnList: MutableList?> = mutableListOf() + addInSmartListBinExpression(returnList, rightBinList, logicListOperationReference, configuration) + addInSmartListBinExpression(returnList, rightBinList, compressionListOperationReference, configuration) + val expression = rightBinList.firstOrNull { (it, offset) -> + val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)!!.firstChildNode.elementType + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && + binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL + } + returnList.add(expression) + return returnList + } + + private fun searchBinaryExpression(node: ASTNode, binList: MutableList) { + if (node.hasChildOfType(BINARY_EXPRESSION) || node.hasChildOfType(PARENTHESIZED) || node.hasChildOfType(POSTFIX_EXPRESSION)) { + node.getChildren(null) + .forEach { + searchBinaryExpression(it, binList) + } + } + if (node.elementType == BINARY_EXPRESSION) { + binList.add(node) + binList.add(node.treeParent.findChildByType(PREFIX_EXPRESSION) ?: return) + } + } + + /** + * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] + */ + private fun addInSmartListBinExpression( + returnList: MutableList?>, + rightBinList: List>, + typesList: List, + configuration: LineLengthConfiguration + ) { + val expression = rightBinList.firstOrNull { (it, offset) -> + val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && + binOperationReference!!.firstChildNode.elementType in typesList + } + returnList.add(expression) + } + } /** * Class FunAndProperty show that long line should be split in Fun Or Property: after EQ (between head and body this function) */ - private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) + private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) { + override fun fix(){ + node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)!!.treeNext) + } + } /** * Class Lambda show that long line should be split in Lambda: in space after [LBRACE] node and before [RBRACE] node */ - private class Lambda(node: ASTNode) : LongLineFixableCases(node) + private class Lambda(node: ASTNode) : LongLineFixableCases(node) { + /** + * Splits Lambda expressions - add splits lines, thereby making the lambda expression a separate line + */ + override fun fix() { + node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) + } + } /** * Class DotQualifiedExpression show that line should be split in DotQualifiedExpression */ - private class DotQualifiedExpression(node: ASTNode) : LongLineFixableCases(node) + private class DotQualifiedExpression(node: ASTNode) : LongLineFixableCases(node){ + override fun fix() { + val dot = node.getFirstChildWithType(DOT) + val safeAccess = node.getFirstChildWithType(SAFE_ACCESS) + val splitNode = if ((dot?.startOffset ?: 0) > (safeAccess?.startOffset ?: 0)) { + dot + } else { + safeAccess + } + val nodeBeforeDot = splitNode?.treePrev + node.appendNewlineMergingWhiteSpace(nodeBeforeDot, splitNode) + } + } /** * Class ValueArgumentList show that line should be split in ValueArgumentList: * @property maximumLineLength - max line length */ - private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration) : LongLineFixableCases(node) + private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration, var positionByOffset: (Int) -> Pair) : LongLineFixableCases(node) { + override fun fix() { + val lineLength = maximumLineLength.lineLength + val offset = fixFirst() + val listComma = node.getAllChildrenWithType(COMMA).map { + it to positionByOffset(it.startOffset - offset).second + }.sortedBy { it.second } + var lineNumber = 1 + listComma.forEachIndexed { index, pair -> + if (pair.second >= lineNumber * lineLength) { + lineNumber++ + val commaSplit = if (index > 0) { + listComma[index - 1].first + } else { + pair.first + } + node.appendNewlineMergingWhiteSpace(commaSplit.treeNext, commaSplit.treeNext) + } + } + node.getFirstChildWithType(RPAR)?.let { + if (positionByOffset(it.treePrev.startOffset).second + it.treePrev.text.length - offset > lineLength * lineNumber && listComma.isNotEmpty()) { + listComma.last().first.let { + node.appendNewlineMergingWhiteSpace(it.treeNext, it.treeNext) + } + } + } + } + + private fun fixFirst(): Int { + val lineLength = maximumLineLength.lineLength + var startOffset = 0 + node.getFirstChildWithType(COMMA)?.let { + if (positionByOffset(it.startOffset).second > lineLength) { + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) + startOffset = this.maximumLineLength.lineLength.toInt() + } + } ?: node.getFirstChildWithType(RPAR)?.let { + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) + startOffset = this.maximumLineLength.lineLength.toInt() + } + return startOffset + } + } /** * Class WhenEntry show that line should be split in WhenEntry node: * Added [LBRACE] and [RBRACE] nodes * Split line in space after [LBRACE] node and before [RBRACE] node */ - private class WhenEntry(node: ASTNode) : LongLineFixableCases(node) + private class WhenEntry(node: ASTNode) : LongLineFixableCases(node) { + override fun fix() { + node.getFirstChildWithType(ARROW)?.let { + node.appendNewlineMergingWhiteSpace(it.treeNext, it.treeNext) + } + } + } /** * val text = "first part" + From a2b1b60232475b5b720fa3ec2bf5ebde4e1cf6b3 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 18:38:56 +0300 Subject: [PATCH 38/52] moved fix logic to class methods ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code with running detect * corrected commented code ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 54 +++++++++++-------- 1 file changed, 31 insertions(+), 23 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 6122f4818d..8801f56b24 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 @@ -83,7 +83,7 @@ class LineLength(configRules: List) : DiktatRule( configRules.getRuleConfig(LONG_LINE)?.configuration ?: emptyMap() ) } - lateinit var positionByOffset: (Int) -> Pair + private lateinit var positionByOffset: (Int) -> Pair override fun logic(node: ASTNode) { if (node.elementType == FILE) { @@ -241,11 +241,11 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -273,7 +273,7 @@ class LineLength(configRules: List) : DiktatRule( positionByOffset(node.startOffset).second } val delimiterIndex = - node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') if (delimiterIndex == -1) { // we can't split this string, however may be we can move it entirely: // case when new line should be inserted after `+`. Example: "first" + "second" @@ -291,7 +291,7 @@ class LineLength(configRules: List) : DiktatRule( } // 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) + (multiLineOffset == 0) && (leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2) val correcterDelimiter = if (shouldAddTwoSpaces) { node.text.substring(0, delimiterIndex - 2).lastIndexOf(' ') } else { @@ -317,7 +317,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun checkFunAndProperty(wrongNode: ASTNode) = - if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() + if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() private fun checkComment(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val leftOffset = positionByOffset(wrongNode.startOffset).second @@ -384,7 +384,6 @@ class LineLength(configRules: List) : DiktatRule( } } - /** * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] */ @@ -450,7 +449,6 @@ class LineLength(configRules: List) : DiktatRule( } } - /** * * [RuleConfiguration] for maximum line length @@ -466,15 +464,17 @@ class LineLength(configRules: List) : DiktatRule( * Class LongLineFixableCases is parent class for several specific error classes */ @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs - abstract class LongLineFixableCases(val node: ASTNode){ - //var positionByOffset = node.treeParent.calculateLineColByOffset() + abstract class LongLineFixableCases(val node: ASTNode) { + /** + fix anything nodes + */ abstract fun fix() } /** * Class None show error long line have unidentified type or something else that we can't analyze */ - class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) { + private class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) { override fun fix() {} } @@ -484,7 +484,7 @@ class LineLength(configRules: List) : DiktatRule( * and inline comments (which should be moved entirely to the previous line) * @property indexLastSpace index of last space to substring comment */ - class Comment( + private class Comment( node: ASTNode, val hasNewLineBefore: Boolean, val indexLastSpace: Int = 0 @@ -531,11 +531,11 @@ class LineLength(configRules: List) : DiktatRule( val firstPart = incorrectText.substring(0, delimiterIndex) val secondPart = incorrectText.substring(delimiterIndex, incorrectText.length) val textBetweenParts = - if (isOneLineString) { - "\" +\n\"" - } else { - "\n" - } + if (isOneLineString) { + "\" +\n\"" + } else { + "\n" + } val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") node.treeParent.replaceChild(node, correctNode) } @@ -566,6 +566,7 @@ class LineLength(configRules: List) : DiktatRule( * @property maximumLineLength is number of maximum line length * @property leftOffset is offset before start [node] * @property binList is list of Binary Expression which are children of [node] + * @property positionByOffset */ private class LongBinaryExpression( node: ASTNode, @@ -606,6 +607,7 @@ class LineLength(configRules: List) : DiktatRule( * Second elem in List - Comparison Binary Expression (> < == >= <= !=) * Other types (Arithmetical and Bit operation) (+ - * / % >> << *= += -= /= %= ++ -- ! in !in etc) */ + @Suppress("TYPE_ALIAS") private fun searchSomeSplitInBinaryExpression(parent: ASTNode, configuration: LineLengthConfiguration): List?> { val logicListOperationReference = listOf(OROR, ANDAND) val compressionListOperationReference = listOf(GT, LT, EQEQ, GTEQ, LTEQ, EXCLEQ) @@ -644,6 +646,7 @@ class LineLength(configRules: List) : DiktatRule( /** * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] */ + @Suppress("TYPE_ALIAS") private fun addInSmartListBinExpression( returnList: MutableList?>, rightBinList: List>, @@ -663,7 +666,7 @@ class LineLength(configRules: List) : DiktatRule( * Class FunAndProperty show that long line should be split in Fun Or Property: after EQ (between head and body this function) */ private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) { - override fun fix(){ + override fun fix() { node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)!!.treeNext) } } @@ -684,7 +687,7 @@ class LineLength(configRules: List) : DiktatRule( /** * Class DotQualifiedExpression show that line should be split in DotQualifiedExpression */ - private class DotQualifiedExpression(node: ASTNode) : LongLineFixableCases(node){ + private class DotQualifiedExpression(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { val dot = node.getFirstChildWithType(DOT) val safeAccess = node.getFirstChildWithType(SAFE_ACCESS) @@ -701,8 +704,13 @@ class LineLength(configRules: List) : DiktatRule( /** * Class ValueArgumentList show that line should be split in ValueArgumentList: * @property maximumLineLength - max line length + * @property positionByOffset */ - private class ValueArgumentList(node: ASTNode, val maximumLineLength: LineLengthConfiguration, var positionByOffset: (Int) -> Pair) : LongLineFixableCases(node) { + private class ValueArgumentList( + node: ASTNode, + val maximumLineLength: LineLengthConfiguration, + var positionByOffset: (Int) -> Pair + ) : LongLineFixableCases(node) { override fun fix() { val lineLength = maximumLineLength.lineLength val offset = fixFirst() From b74337946a7a47174c766b9112ac08be3ef30763 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 18:46:59 +0300 Subject: [PATCH 39/52] moved fix logic to class methods ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code with running detect * corrected commented code ### Issue (#1243) --- .../config/reader/JsonResourceConfigReader.kt | 2 +- .../ruleset/rules/chapter3/LineLength.kt | 31 +++++++++---------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt index f1182057b0..9d175d2677 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt @@ -46,7 +46,7 @@ abstract class JsonResourceConfigReader { * @return [BufferedReader] representing loaded resource */ protected open fun getConfigFile(resourceFileName: String): BufferedReader? = - classLoader.getResourceAsStream(resourceFileName)?.bufferedReader() + classLoader.getResourceAsStream(resourceFileName)?.bufferedReader() /** * you can specify your own parser, in example for parsing stream as a json 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 d651de3d83..f1b2291020 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 @@ -241,11 +241,11 @@ class LineLength(configRules: List) : DiktatRule( } private fun parserStringAndDot(node: ASTNode, configuration: LineLengthConfiguration) = - if (node.elementType == STRING_TEMPLATE) { - parserStringTemplate(node, configuration) - } else { - parserDotQualifiedExpression(node, configuration) - } + if (node.elementType == STRING_TEMPLATE) { + parserStringTemplate(node, configuration) + } else { + parserDotQualifiedExpression(node, configuration) + } /** * This class finds where the string can be split @@ -273,7 +273,7 @@ class LineLength(configRules: List) : DiktatRule( positionByOffset(node.startOffset).second } val delimiterIndex = - node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') + node.text.substring(0, multiLineOffset + configuration.lineLength.toInt() - leftOffset).lastIndexOf(' ') if (delimiterIndex == -1) { // we can't split this string, however may be we can move it entirely: // case when new line should be inserted after `+`. Example: "first" + "second" @@ -291,7 +291,7 @@ class LineLength(configRules: List) : DiktatRule( } // 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) + (multiLineOffset == 0) && (leftOffset + delimiterIndex > configuration.lineLength.toInt() - 2) val correcterDelimiter = if (shouldAddTwoSpaces) { node.text.substring(0, delimiterIndex - 2).lastIndexOf(' ') } else { @@ -317,7 +317,7 @@ class LineLength(configRules: List) : DiktatRule( } private fun checkFunAndProperty(wrongNode: ASTNode) = - if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() + if (wrongNode.hasChildOfType(EQ)) FunAndProperty(wrongNode) else None() private fun checkComment(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases { val leftOffset = positionByOffset(wrongNode.startOffset).second @@ -345,7 +345,6 @@ class LineLength(configRules: List) : DiktatRule( false } - /** * This method uses recursion to store binary node in the order in which they are located * Also binList contains nodes with PREFIX_EXPRESSION element type ( !isFoo(), !isValid) @@ -532,11 +531,11 @@ class LineLength(configRules: List) : DiktatRule( val firstPart = incorrectText.substring(0, delimiterIndex) val secondPart = incorrectText.substring(delimiterIndex, incorrectText.length) val textBetweenParts = - if (isOneLineString) { - "\" +\n\"" - } else { - "\n" - } + if (isOneLineString) { + "\" +\n\"" + } else { + "\n" + } val correctNode = KotlinParser().createNode("$firstPart$textBetweenParts$secondPart") node.treeParent.replaceChild(node, correctNode) } @@ -625,7 +624,7 @@ class LineLength(configRules: List) : DiktatRule( val expression = rightBinList.firstOrNull { (it, offset) -> val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)!!.firstChildNode.elementType offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL + binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL } returnList.add(expression) return returnList @@ -657,7 +656,7 @@ class LineLength(configRules: List) : DiktatRule( val expression = rightBinList.firstOrNull { (it, offset) -> val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference!!.firstChildNode.elementType in typesList + binOperationReference!!.firstChildNode.elementType in typesList } returnList.add(expression) } From d1feaf325ecbc4e02eed49c9ebfeeed276910523 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 18:47:12 +0300 Subject: [PATCH 40/52] moved fix logic to class methods ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code with running detect * corrected commented code ### Issue (#1243) --- .../org/cqfn/diktat/common/config/rules/RulesConfigReader.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index 652de14739..da672a2cac 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -202,7 +202,7 @@ fun List.isRuleEnabled(rule: Rule): Boolean { * @return true if the code block is marked with annotation that is in `ignored list` in the rule */ fun List.isAnnotatedWithIgnoredAnnotation(rule: Rule, annotations: Set): Boolean = - getRuleConfig(rule) + getRuleConfig(rule) ?.ignoreAnnotated ?.map { it.trim() } ?.map { it.trim('"') } From 6ccdb65dff442fc7eab494b1490a7f78b75dd359 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 19:57:23 +0300 Subject: [PATCH 41/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../common/config/rules/RulesConfigReader.kt | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index da672a2cac..066f6f179d 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -202,13 +202,13 @@ fun List.isRuleEnabled(rule: Rule): Boolean { * @return true if the code block is marked with annotation that is in `ignored list` in the rule */ fun List.isAnnotatedWithIgnoredAnnotation(rule: Rule, annotations: Set): Boolean = - getRuleConfig(rule) - ?.ignoreAnnotated - ?.map { it.trim() } - ?.map { it.trim('"') } - ?.intersect(annotations) - ?.isNotEmpty() - ?: false + getRuleConfig(rule) + ?.ignoreAnnotated + ?.map { it.trim() } + ?.map { it.trim('"') } + ?.intersect(annotations) + ?.isNotEmpty() + ?: false /** * Parse string into KotlinVersion From 29f14b1b28a86337f3e4819752c790b923c13cd4 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 19:58:54 +0300 Subject: [PATCH 42/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../org/cqfn/diktat/common/config/rules/RulesConfigReader.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index 066f6f179d..0cd23a9369 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -202,7 +202,7 @@ fun List.isRuleEnabled(rule: Rule): Boolean { * @return true if the code block is marked with annotation that is in `ignored list` in the rule */ fun List.isAnnotatedWithIgnoredAnnotation(rule: Rule, annotations: Set): Boolean = - getRuleConfig(rule) + getRuleConfig(rule) ?.ignoreAnnotated ?.map { it.trim() } ?.map { it.trim('"') } From b62d473dd8df4cd8427da109744a51490f6a9f00 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 20:03:43 +0300 Subject: [PATCH 43/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../cqfn/diktat/test/framework/config/TestConfig.kt | 2 +- .../test/framework/processing/TestProcessingFactory.kt | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt index ed696d6f12..71928094c9 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt @@ -31,7 +31,7 @@ class TestConfig internal constructor( // FixMe: not used by for now, fix the description when the class will be ready override fun toString() = - """(executionCommand: $executionCommand, expectedResultFile: $expectedResultFile, inPlace: $inPlace, + """(executionCommand: $executionCommand, expectedResultFile: $expectedResultFile, inPlace: $inPlace, executionType: $executionCommand)""" /** diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt index 24f1f1a369..30740e676d 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt @@ -27,7 +27,7 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { ?.let { File(it.file) } ?: run { log.error("Not able to get directory with test configuration files: " + - argReader.properties.testConfigsRelativePath) + argReader.properties.testConfigsRelativePath) exitProcess(STATUS_FIVE) } try { @@ -57,7 +57,7 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { } val testStream: Stream = - if (argReader.properties.isParallelMode) testList.parallelStream() else testList.stream() + if (argReader.properties.isParallelMode) testList.parallelStream() else testList.stream() testStream .map { test: String -> findTestInResources(test) } @@ -71,9 +71,9 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { } private fun findTestInResources(test: String): TestConfig? = - TestConfigReader("${argReader.properties.testConfigsRelativePath}/$test.json", javaClass.classLoader) - .config - ?.setTestName(test) + TestConfigReader("${argReader.properties.testConfigsRelativePath}/$test.json", javaClass.classLoader) + .config + ?.setTestName(test) @Suppress("FUNCTION_BOOLEAN_PREFIX") private fun processTest(testConfig: TestConfig): Boolean { From 20a549847c5b9f7e8c309b7cf5627b0579761066 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 20:12:46 +0300 Subject: [PATCH 44/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../config/reader/JsonResourceConfigReader.kt | 2 +- .../common/config/rules/RulesConfigReader.kt | 14 +++++++------- .../diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- .../diktat/test/framework/config/TestConfig.kt | 2 +- .../framework/processing/TestProcessingFactory.kt | 10 +++++----- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt index 9d175d2677..f1182057b0 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/reader/JsonResourceConfigReader.kt @@ -46,7 +46,7 @@ abstract class JsonResourceConfigReader { * @return [BufferedReader] representing loaded resource */ protected open fun getConfigFile(resourceFileName: String): BufferedReader? = - classLoader.getResourceAsStream(resourceFileName)?.bufferedReader() + classLoader.getResourceAsStream(resourceFileName)?.bufferedReader() /** * you can specify your own parser, in example for parsing stream as a json diff --git a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt index 0cd23a9369..652de14739 100644 --- a/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt +++ b/diktat-common/src/main/kotlin/org/cqfn/diktat/common/config/rules/RulesConfigReader.kt @@ -202,13 +202,13 @@ fun List.isRuleEnabled(rule: Rule): Boolean { * @return true if the code block is marked with annotation that is in `ignored list` in the rule */ fun List.isAnnotatedWithIgnoredAnnotation(rule: Rule, annotations: Set): Boolean = - getRuleConfig(rule) - ?.ignoreAnnotated - ?.map { it.trim() } - ?.map { it.trim('"') } - ?.intersect(annotations) - ?.isNotEmpty() - ?: false + getRuleConfig(rule) + ?.ignoreAnnotated + ?.map { it.trim() } + ?.map { it.trim('"') } + ?.intersect(annotations) + ?.isNotEmpty() + ?: false /** * Parse string into KotlinVersion 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 f1b2291020..b037219e73 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 @@ -466,7 +466,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { /** - fix anything nodes + Abstract fix - fix anything nodes */ abstract fun fix() } diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt index 71928094c9..ed696d6f12 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/config/TestConfig.kt @@ -31,7 +31,7 @@ class TestConfig internal constructor( // FixMe: not used by for now, fix the description when the class will be ready override fun toString() = - """(executionCommand: $executionCommand, expectedResultFile: $expectedResultFile, inPlace: $inPlace, + """(executionCommand: $executionCommand, expectedResultFile: $expectedResultFile, inPlace: $inPlace, executionType: $executionCommand)""" /** diff --git a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt index 30740e676d..24f1f1a369 100644 --- a/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt +++ b/diktat-test-framework/src/main/kotlin/org/cqfn/diktat/test/framework/processing/TestProcessingFactory.kt @@ -27,7 +27,7 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { ?.let { File(it.file) } ?: run { log.error("Not able to get directory with test configuration files: " + - argReader.properties.testConfigsRelativePath) + argReader.properties.testConfigsRelativePath) exitProcess(STATUS_FIVE) } try { @@ -57,7 +57,7 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { } val testStream: Stream = - if (argReader.properties.isParallelMode) testList.parallelStream() else testList.stream() + if (argReader.properties.isParallelMode) testList.parallelStream() else testList.stream() testStream .map { test: String -> findTestInResources(test) } @@ -71,9 +71,9 @@ class TestProcessingFactory(private val argReader: TestArgumentsReader) { } private fun findTestInResources(test: String): TestConfig? = - TestConfigReader("${argReader.properties.testConfigsRelativePath}/$test.json", javaClass.classLoader) - .config - ?.setTestName(test) + TestConfigReader("${argReader.properties.testConfigsRelativePath}/$test.json", javaClass.classLoader) + .config + ?.setTestName(test) @Suppress("FUNCTION_BOOLEAN_PREFIX") private fun processTest(testConfig: TestConfig): Boolean { From 276fd00f5da726d3afd56b8e43681763b9d9b416 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 20:34:01 +0300 Subject: [PATCH 45/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../ruleset/rules/chapter3/LineLength.kt | 50 ++++++------------- 1 file changed, 16 insertions(+), 34 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 b037219e73..7dcb15b5dd 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 @@ -384,24 +384,6 @@ class LineLength(configRules: List) : DiktatRule( } } - /** - * Runs through the sorted list [rightBinList], finds its last element, the type of which is included in the set [typesList] and adds it in the list [returnList] - */ - @Suppress("TYPE_ALIAS", "UnsafeCallOnNullableType") - private fun addInSmartListBinExpression( - returnList: MutableList?>, - rightBinList: List>, - typesList: List, - configuration: LineLengthConfiguration - ) { - val expression = rightBinList.firstOrNull { (it, offset) -> - val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference!!.firstChildNode.elementType in typesList - } - returnList.add(expression) - } - /** * Finds the first binary expression closer to the separator */ @@ -444,7 +426,7 @@ class LineLength(configRules: List) : DiktatRule( it to offset } .sortedBy { it.second } - .lastOrNull { (it, offset) -> + .lastOrNull { (_, offset) -> offset <= configuration.lineLength + 1 } } @@ -547,8 +529,8 @@ class LineLength(configRules: List) : DiktatRule( private class BinaryExpression(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { val nodeOperationReference = node.findChildByType(OPERATION_REFERENCE) - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext + val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) { + nodeOperationReference?.treeNext } else { if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { nodeOperationReference.treePrev @@ -585,8 +567,8 @@ class LineLength(configRules: List) : DiktatRule( val rightSplitNode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] val nodeOperationReference = rightSplitNode?.first?.getFirstChildWithType(OPERATION_REFERENCE) rightSplitNode?.let { - val nextNode = if (nodeOperationReference!!.firstChildNode.elementType != ELVIS) { - nodeOperationReference.treeNext + val nextNode = if (nodeOperationReference?.firstChildNode?.elementType != ELVIS) { + nodeOperationReference?.treeNext } else { if (nodeOperationReference.treePrev.elementType == WHITE_SPACE) { nodeOperationReference.treePrev @@ -594,7 +576,7 @@ class LineLength(configRules: List) : DiktatRule( nodeOperationReference } } - if (!nextNode.text.contains(("\n"))) { + if (!nextNode?.text?.contains(("\n"))!!) { rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) } } @@ -614,7 +596,7 @@ class LineLength(configRules: List) : DiktatRule( val binList: MutableList = mutableListOf() searchBinaryExpression(parent, binList) val rightBinList = binList.map { - it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)!!.startOffset).second + it to positionByOffset(it.getFirstChildWithType(OPERATION_REFERENCE)?.startOffset ?: 0).second } .sortedBy { it.second } .reversed() @@ -622,8 +604,8 @@ class LineLength(configRules: List) : DiktatRule( addInSmartListBinExpression(returnList, rightBinList, logicListOperationReference, configuration) addInSmartListBinExpression(returnList, rightBinList, compressionListOperationReference, configuration) val expression = rightBinList.firstOrNull { (it, offset) -> - val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)!!.firstChildNode.elementType - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && + val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE)?.firstChildNode?.elementType + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 && binOperationReference !in logicListOperationReference && binOperationReference !in compressionListOperationReference && binOperationReference != EXCL } returnList.add(expression) @@ -655,8 +637,8 @@ class LineLength(configRules: List) : DiktatRule( ) { val expression = rightBinList.firstOrNull { (it, offset) -> val binOperationReference = it.getFirstChildWithType(OPERATION_REFERENCE) - offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text!!.length ?: 0) <= configuration.lineLength + 1 && - binOperationReference!!.firstChildNode.elementType in typesList + offset + (it.getFirstChildWithType(OPERATION_REFERENCE)?.text?.length ?: 0) <= configuration.lineLength + 1 && + binOperationReference?.firstChildNode?.elementType in typesList } returnList.add(expression) } @@ -667,7 +649,7 @@ class LineLength(configRules: List) : DiktatRule( */ private class FunAndProperty(node: ASTNode) : LongLineFixableCases(node) { override fun fix() { - node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)!!.treeNext) + node.appendNewlineMergingWhiteSpace(null, node.findChildByType(EQ)?.treeNext) } } @@ -679,8 +661,8 @@ class LineLength(configRules: List) : DiktatRule( * Splits Lambda expressions - add splits lines, thereby making the lambda expression a separate line */ override fun fix() { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)!!.treeNext, node.findChildByType(LBRACE)!!.treeNext) - node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)!!.treePrev, node.findChildByType(RBRACE)!!.treePrev) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LBRACE)?.treeNext, node.findChildByType(LBRACE)?.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(RBRACE)?.treePrev, node.findChildByType(RBRACE)?.treePrev) } } @@ -743,12 +725,12 @@ class LineLength(configRules: List) : DiktatRule( var startOffset = 0 node.getFirstChildWithType(COMMA)?.let { if (positionByOffset(it.startOffset).second > lineLength) { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)?.treeNext, node.findChildByType(LPAR)?.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = this.maximumLineLength.lineLength.toInt() } } ?: node.getFirstChildWithType(RPAR)?.let { - node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)!!.treeNext, node.findChildByType(LPAR)!!.treeNext) + node.appendNewlineMergingWhiteSpace(node.findChildByType(LPAR)?.treeNext, node.findChildByType(LPAR)?.treeNext) node.appendNewlineMergingWhiteSpace(node.findChildByType(RPAR), node.findChildByType(RPAR)) startOffset = this.maximumLineLength.lineLength.toInt() } From 61d4bacc33a41fcde870cb59bb02a7d950bbbc82 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 20:43:18 +0300 Subject: [PATCH 46/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 7dcb15b5dd..cf9ebd23e6 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 @@ -92,7 +92,6 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } - println(node.text) } } @@ -457,6 +456,7 @@ class LineLength(configRules: List) : DiktatRule( * Class None show error long line have unidentified type or something else that we can't analyze */ private class None : LongLineFixableCases(KotlinParser().createNode("ERROR")) { + @Suppress("EmptyFunctionBlock") override fun fix() {} } @@ -576,7 +576,7 @@ class LineLength(configRules: List) : DiktatRule( nodeOperationReference } } - if (!nextNode?.text?.contains(("\n"))!!) { + if (nextNode?.text?.contains(("\n")) == true) { rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) } } From 228a24f7c4a5f4e22cc165cd91b9c61acb7adf75 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Tue, 7 Jun 2022 21:13:00 +0300 Subject: [PATCH 47/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 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 cf9ebd23e6..39510b2783 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 @@ -5,7 +5,15 @@ import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.Warnings.LONG_LINE import org.cqfn.diktat.ruleset.rules.DiktatRule -import org.cqfn.diktat.ruleset.utils.* +import org.cqfn.diktat.ruleset.utils.KotlinParser +import org.cqfn.diktat.ruleset.utils.appendNewlineMergingWhiteSpace +import org.cqfn.diktat.ruleset.utils.calculateLineColByOffset +import org.cqfn.diktat.ruleset.utils.findAllNodesWithConditionOnLine +import org.cqfn.diktat.ruleset.utils.findParentNodeWithSpecificType +import org.cqfn.diktat.ruleset.utils.getAllChildrenWithType +import org.cqfn.diktat.ruleset.utils.getFirstChildWithType +import org.cqfn.diktat.ruleset.utils.getLineNumber +import org.cqfn.diktat.ruleset.utils.hasChildOfType import com.pinterest.ktlint.core.ast.ElementType.ANDAND import com.pinterest.ktlint.core.ast.ElementType.ARROW @@ -447,7 +455,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { /** - Abstract fix - fix anything nodes + Abstract fix - fix anything nodes */ abstract fun fix() } From 76f261bdd996a2b2c0b6ed4d9fbf15e57fee0323 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 8 Jun 2022 14:19:57 +0300 Subject: [PATCH 48/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) 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 39510b2783..0cbb2bc95d 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 @@ -100,6 +100,7 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } + println(node.text) } } @@ -454,6 +455,7 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { + /** Abstract fix - fix anything nodes */ @@ -584,7 +586,7 @@ class LineLength(configRules: List) : DiktatRule( nodeOperationReference } } - if (nextNode?.text?.contains(("\n")) == true) { + if (!nextNode?.text?.contains(("\n"))!!) { rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode) } } From 68d4b1a4d63121a452a3e8352331c1d90f9f8988 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 8 Jun 2022 14:43:15 +0300 Subject: [PATCH 49/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 1 - 1 file changed, 1 deletion(-) 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 0cbb2bc95d..574b2969a6 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 @@ -455,7 +455,6 @@ class LineLength(configRules: List) : DiktatRule( */ @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { - /** Abstract fix - fix anything nodes */ From e494594378c9de8e851edd7b480bd7dd538fac90 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 8 Jun 2022 14:58:59 +0300 Subject: [PATCH 50/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 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 574b2969a6..6a20030e87 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 @@ -100,7 +100,6 @@ class LineLength(configRules: List) : DiktatRule( checkLength(it, configuration) } } - println(node.text) } } @@ -456,7 +455,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { /** - Abstract fix - fix anything nodes + * Abstract fix - fix anything nodes */ abstract fun fix() } @@ -571,6 +570,7 @@ class LineLength(configRules: List) : DiktatRule( * If the transfer is done on the Elvis operator, then transfers it to a new line * If not on the Elvis operator, then transfers it to a new line after the operation reference */ + @Suppress("UnsafeCallOnNullableType") override fun fix() { val anySplitNode = searchSomeSplitInBinaryExpression(node, maximumLineLength) val rightSplitNode = anySplitNode[0] ?: anySplitNode[1] ?: anySplitNode[2] From 2a4ce0fd0623b6408771a741cf3e8dfaf54ca25c Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Wed, 8 Jun 2022 15:10:26 +0300 Subject: [PATCH 51/52] corrected code ### Whats added: * corrected logic fix and warn String Template in LineLength rule * added logic fix and warn long Dot Qualified Expression and Safe Access Expression in LineLength rule * added logic fix and warn Value Arguments List in LineLength rule * added and corrected fix and warn tests in LineLength rule * moved fix logic to class methods * fix code with diktat:fix@diktat * added comments to classes and functions * corrected code ### Issue (#1243) --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/LineLength.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 6a20030e87..03e90f5d97 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 @@ -455,7 +455,7 @@ class LineLength(configRules: List) : DiktatRule( @Suppress("KDOC_NO_CONSTRUCTOR_PROPERTY", "MISSING_KDOC_CLASS_ELEMENTS") // todo add proper docs abstract class LongLineFixableCases(val node: ASTNode) { /** - * Abstract fix - fix anything nodes + * Abstract fix - fix anything nodes */ abstract fun fix() } From 910738a75f42a37401f45b3ed95cddda3cb17a23 Mon Sep 17 00:00:00 2001 From: Arrgentum Date: Thu, 16 Jun 2022 11:18:43 +0300 Subject: [PATCH 52/52] fix all --- .../org/cqfn/diktat/util/SuppressingTest.kt | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/SuppressingTest.kt b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/SuppressingTest.kt index e9bf4eea1b..1c0ad14c66 100644 --- a/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/SuppressingTest.kt +++ b/diktat-rules/src/test/kotlin/org/cqfn/diktat/util/SuppressingTest.kt @@ -1,26 +1,11 @@ package org.cqfn.diktat.util import org.cqfn.diktat.common.config.rules.RulesConfig -import org.cqfn.diktat.ruleset.constants.Warnings.BACKTICKS_PROHIBITED -import org.cqfn.diktat.ruleset.constants.Warnings.CLASS_NAME_INCORRECT -import org.cqfn.diktat.ruleset.constants.Warnings.CONFUSING_IDENTIFIER_NAMING -import org.cqfn.diktat.ruleset.constants.Warnings.CONSTANT_UPPERCASE -import org.cqfn.diktat.ruleset.constants.Warnings.ENUM_VALUE -import org.cqfn.diktat.ruleset.constants.Warnings.EXCEPTION_SUFFIX -import org.cqfn.diktat.ruleset.constants.Warnings.FUNCTION_BOOLEAN_PREFIX -import org.cqfn.diktat.ruleset.constants.Warnings.GENERIC_NAME import org.cqfn.diktat.ruleset.constants.Warnings.IDENTIFIER_LENGTH -import org.cqfn.diktat.ruleset.constants.Warnings.OBJECT_NAME_INCORRECT -import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_HAS_PREFIX -import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT -import org.cqfn.diktat.ruleset.constants.Warnings.VARIABLE_NAME_INCORRECT_FORMAT import org.cqfn.diktat.ruleset.rules.DIKTAT_RULE_SET_ID import org.cqfn.diktat.ruleset.rules.chapter1.IdentifierNaming import com.pinterest.ktlint.core.LintError -import generated.WarningNames -import org.junit.jupiter.api.Tag -import org.junit.jupiter.api.Tags import org.junit.jupiter.api.Test class SuppressingTest : LintTestBase(::IdentifierNaming) {