Skip to content

Commit

Permalink
### Whats added:
Browse files Browse the repository at this point in the history
 * 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)
  • Loading branch information
Arrgentum committed May 25, 2022
1 parent ebf05b1 commit 998ee8b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -168,7 +171,7 @@ class LineLength(configRules: List<RulesConfig>) : 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<IElementType>(BINARY_EXPRESSION, VALUE_ARGUMENT_LIST, WHEN_CONDITION_WITH_EXPRESSION)
findParentNodeWithSpecificTypeMany(parent, parentIsBinExpOrValArgListOrWhenEntry)?.let {
Expand Down Expand Up @@ -302,10 +305,14 @@ class LineLength(configRules: List<RulesConfig>) : 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) =
Expand Down Expand Up @@ -372,8 +379,9 @@ class LineLength(configRules: List<RulesConfig>) : 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")
Expand Down Expand Up @@ -555,14 +563,14 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
*@param node node in which to search
*@param dotList mutable list of ASTNode to store nodes
*/
private fun searchDot(node: ASTNode, dotList: MutableList<ASTNode>) {
if (node.elementType == DOT_QUALIFIED_EXPRESSION) {
private fun searchDotOrSafeAccess(node: ASTNode, dotList: MutableList<ASTNode>) {
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)
}
Expand Down Expand Up @@ -628,15 +636,15 @@ class LineLength(configRules: List<RulesConfig>) : DiktatRule(
val list: MutableList<ASTNode> = 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
}
.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
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
package test.paragraph3.long_line

val A = This.Is.Veeeeryyyyyyy.Loooooong.Dot
.Qualified.Expression
.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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 998ee8b

Please sign in to comment.