Skip to content

Commit

Permalink
Polish
Browse files Browse the repository at this point in the history
### What's done:
* Polish
  • Loading branch information
kgevorkyan committed Jul 16, 2021
1 parent 74b607a commit 56cf4fa
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,16 @@ import com.pinterest.ktlint.core.ast.ElementType.BOOLEAN_CONSTANT
import com.pinterest.ktlint.core.ast.ElementType.CALL_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.CHARACTER_CONSTANT
import com.pinterest.ktlint.core.ast.ElementType.CONDITION
import com.pinterest.ktlint.core.ast.ElementType.DOT
import com.pinterest.ktlint.core.ast.ElementType.DOT_QUALIFIED_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT
import com.pinterest.ktlint.core.ast.ElementType.EQ
import com.pinterest.ktlint.core.ast.ElementType.FILE
import com.pinterest.ktlint.core.ast.ElementType.FLOAT_CONSTANT
import com.pinterest.ktlint.core.ast.ElementType.FUN
import com.pinterest.ktlint.core.ast.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.core.ast.ElementType.IF
import com.pinterest.ktlint.core.ast.ElementType.IMPORT_LIST
import com.pinterest.ktlint.core.ast.ElementType.INTEGER_CONSTANT
import com.pinterest.ktlint.core.ast.ElementType.KDOC_MARKDOWN_INLINE_LINK
import com.pinterest.ktlint.core.ast.ElementType.KDOC_TEXT
import com.pinterest.ktlint.core.ast.ElementType.LAMBDA_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.LITERAL_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LONG_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.LPAR
Expand All @@ -43,8 +39,6 @@ import com.pinterest.ktlint.core.ast.ElementType.PREFIX_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.PROPERTY
import com.pinterest.ktlint.core.ast.ElementType.REFERENCE_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.RPAR
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS
import com.pinterest.ktlint.core.ast.ElementType.SAFE_ACCESS_EXPRESSION
import com.pinterest.ktlint.core.ast.ElementType.SHORT_STRING_TEMPLATE_ENTRY
import com.pinterest.ktlint.core.ast.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE
Expand Down Expand Up @@ -93,12 +87,10 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
node.text.lines().forEach { line ->
if (line.length > configuration.lineLength) {
val newNode = node.psi.findElementAt(offset + configuration.lineLength.toInt() - 1)!!.node
println("\n------------------\n[isFixMode: ${isFixMode}] NEW_NODE ${newNode.elementType} | ${newNode.text} IN ${node.psi.text.substring(offset + configuration.lineLength.toInt() - 1, minOf(offset + configuration.lineLength.toInt() + 5, offset + line.length))}")
if ((newNode.elementType != KDOC_TEXT && newNode.elementType != KDOC_MARKDOWN_INLINE_LINK) ||
!isKdocValid(newNode)) {
positionByOffset = node.treeParent.calculateLineColByOffset()
val fixableType = isFixable(newNode, configuration)
println("CAN BE FIXED? ${fixableType != LongLineFixableCases.None}")
LONG_LINE.warnAndFix(configRules, emitWarn, isFixMode,
"max line length ${configuration.lineLength}, but was ${line.length}",
offset + node.startOffset, node, fixableType != LongLineFixableCases.None) {
Expand Down Expand Up @@ -179,7 +171,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
// can't fix this case
return LongLineFixableCases.None
}
println("DEL INDEX ${delimiterIndex} ${node.text.substring(0, delimiterIndex)}")
// check, that space to split is a part of text - not code
// If the space split is part of the code, then there is a chance of breaking the code when fixing, that why we should ignore it
val isSpaceIsWhiteSpace = node.psi.findElementAt(delimiterIndex)!!.node.isWhiteSpace()
Expand Down Expand Up @@ -228,7 +219,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(

@Suppress("UnsafeCallOnNullableType", "TOO_LONG_FUNCTION")
private fun checkProperty(wrongNode: ASTNode, configuration: LineLengthConfiguration): LongLineFixableCases {
println("Check property")
var newParent = wrongNode
while (newParent.hasChildOfType(PARENTHESIZED)) {
newParent = wrongNode.findChildByType(PARENTHESIZED)!!
Expand All @@ -237,11 +227,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
if (newParent.hasChildOfType(BINARY_EXPRESSION)) {
val leftOffset = positionByOffset(newParent.findChildByType(BINARY_EXPRESSION)!!.startOffset).second
val binList: MutableList<ASTNode> = mutableListOf()
println("WRONG NODE : ${wrongNode.text}")
dfsForProperty(wrongNode, binList)
print("BINARY EXPR: [")
binList.forEach { print("${it.text} | ") }
print("]\n")
if (binList.size == 1) {
return LongLineFixableCases.None
}
Expand Down Expand Up @@ -338,7 +324,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
* This method fix too long binary expression: split after OPERATION_REFERENCE closest to max length
*
* In this method we collect all binary expression in correct order and then
* we collect their if their length less than max.
* we collect their if their length less then max.
*/
@Suppress("UnsafeCallOnNullableType")
private fun fixLongBinaryExpression(wrongBinaryExpression: LongLineFixableCases.Condition) {
Expand All @@ -347,12 +333,8 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
var binaryText = ""
binList.forEachIndexed { index, astNode ->
binaryText += findAllText(astNode)
println("binaryText [${leftOffset + binaryText.length } vs ${wrongBinaryExpression.maximumLineLength}] [${binaryText}]")
if (leftOffset + binaryText.length > wrongBinaryExpression.maximumLineLength && index != 0) {
println("---")
val commonParent = astNode.parent({ println("${it.elementType} ${it.text} ast ${astNode.elementType}"); it.elementType == BINARY_EXPRESSION && it in binList[index - 1].parents() })!!
println("\nASTNODE ${astNode.elementType} | ${astNode.text}")
println("COMMON PARENT ${commonParent.elementType} | ${commonParent.text}")
val commonParent = astNode.parent({ it.elementType == BINARY_EXPRESSION && it in binList[index - 1].parents() })!!
val nextNode = commonParent.findChildByType(OPERATION_REFERENCE)!!.treeNext
if (!nextNode.text.contains("\n")) {
commonParent.appendNewlineMergingWhiteSpace(nextNode, nextNode)
Expand Down Expand Up @@ -392,9 +374,7 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
node = astNode.parent({ newNode -> newNode.nextSibling { it.elementType == OPERATION_REFERENCE } != null },
strict = false)
?: return text

node = node.nextSibling { it.elementType == OPERATION_REFERENCE }!!

if (node.treePrev.elementType == WHITE_SPACE) {
text += node.treePrev.text
}
Expand Down Expand Up @@ -453,26 +433,20 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
}
}

// Depth-first search. Collect all children to the right of the equal sign with specific type (propertyList), by which
// we can split expression. Such logic needed because AST representation of complex conditions is quite strange
/**
* Collect by Depth-first search (DFS) all children to the right side of the equal sign with specific type [propertyList],
* by which we can split expression.
* Such logic needed, because AST representation of complex conditions is quite loaded
*
* @param node target node to be processed
* @param binList where to store the corresponding results
*/
private fun dfsForProperty(node: ASTNode, binList: MutableList<ASTNode>) {
node.getChildren(null).forEach {
//println("CHILD: ${it.elementType} | ${it.text}")
if (it.elementType in propertyList) {
val parentType = it.treeParent?.elementType
if (it.elementType == REFERENCE_EXPRESSION &&
(parentType == CALL_EXPRESSION)
//|| parentType == DOT_QUALIFIED_EXPRESSION || parentType == SAFE_ACCESS_EXPRESSION)
) {
//println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}")
//if (it.treeParent?.treeParent?.elementType != DOT_QUALIFIED_EXPRESSION && it.treeParent?.treeParent?.elementType != SAFE_ACCESS_EXPRESSION) {
//println("PAR: ${it.treeParent.elementType} | ${it.treeParent.text}")
//println("GRAND PAR: ${it.treeParent.treeParent.elementType} | ${it.treeParent.treeParent.text}")
binList.tryAdd(it.treeParent)
//}
if (it.elementType == REFERENCE_EXPRESSION && it.treeParent?.elementType == CALL_EXPRESSION) {
binList.tryAdd(it.treeParent)
} else {
//println("CHILD: ${it.elementType} | ${it.text}")
//binList.add(it)
binList.tryAdd(it)
}
}
Expand All @@ -481,7 +455,6 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
}

private fun MutableList<ASTNode>.tryAdd(node: ASTNode) {
//println(node.treeParent?.elementType)
if (node !in this) {
this.add(node)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,6 @@ import org.cqfn.diktat.util.FixTestBase
import org.junit.jupiter.api.Test

class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength) {
private val rulesConfigListLargeLineLength: List<RulesConfig> = listOf(
RulesConfig(LONG_LINE.name, true,
mapOf("lineLength" to "180"))
)
private val rulesConfigListDefaultLineLength: List<RulesConfig> = listOf(
RulesConfig(LONG_LINE.name, true,
mapOf("lineLength" to "120"))
Expand Down Expand Up @@ -44,6 +40,11 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
fixAndCompare("LongLineExpressionExpected.kt", "LongLineExpressionTest.kt", rulesConfigListLineLength)
}

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

@Test
fun `should fix long function`() {
fixAndCompare("LongLineFunExpected.kt", "LongLineFunTest.kt", rulesConfigListLineLength)
Expand All @@ -68,15 +69,4 @@ class LineLengthFixTest : FixTestBase("test/paragraph3/long_line", ::LineLength)
fun `should fix annotation`() {
fixAndCompare("LongLineAnnotationExpected.kt", "LongLineAnnotationTest.kt", rulesConfigListLineLength)
}

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

@Test
fun `should fix long binary expression 2, limit 180`() {
//fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLargeLineLength)
fixAndCompare("LongBinaryExpressionExpected2.kt", "LongBinaryExpressionTest2.kt", rulesConfigListLineLength)
}
}

This file was deleted.

This file was deleted.

0 comments on commit 56cf4fa

Please sign in to comment.