Skip to content

Commit

Permalink
corrected code
Browse files Browse the repository at this point in the history
### 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)
  • Loading branch information
Arrgentum committed Jun 7, 2022
1 parent 20a5498 commit 276fd00
Showing 1 changed file with 16 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -384,24 +384,6 @@ class LineLength(configRules: List<RulesConfig>) : 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<Pair<ASTNode, Int>?>,
rightBinList: List<Pair<ASTNode, Int>>,
typesList: List<IElementType>,
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
*/
Expand Down Expand Up @@ -444,7 +426,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
it to offset
}
.sortedBy { it.second }
.lastOrNull { (it, offset) ->
.lastOrNull { (_, offset) ->
offset <= configuration.lineLength + 1
}
}
Expand Down Expand Up @@ -547,8 +529,8 @@ class LineLength(configRules: List<RulesConfig>) : 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
Expand Down Expand Up @@ -585,16 +567,16 @@ class LineLength(configRules: List<RulesConfig>) : 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
} else {
nodeOperationReference
}
}
if (!nextNode.text.contains(("\n"))) {
if (!nextNode?.text?.contains(("\n"))!!) {
rightSplitNode.first.appendNewlineMergingWhiteSpace(nextNode, nextNode)
}
}
Expand All @@ -614,16 +596,16 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
val binList: MutableList<ASTNode> = 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()
val returnList: MutableList<Pair<ASTNode, Int>?> = 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 &&
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)
Expand Down Expand Up @@ -655,8 +637,8 @@ class LineLength(configRules: List<RulesConfig>) : 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)
}
Expand All @@ -667,7 +649,7 @@ class LineLength(configRules: List<RulesConfig>) : 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)
}
}

Expand All @@ -679,8 +661,8 @@ class LineLength(configRules: List<RulesConfig>) : 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)
}
}

Expand Down Expand Up @@ -743,12 +725,12 @@ class LineLength(configRules: List<RulesConfig>) : 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()
}
Expand Down

0 comments on commit 276fd00

Please sign in to comment.