Skip to content

Commit

Permalink
Don't separate fully-qualified as a regular dot qualified expression (
Browse files Browse the repository at this point in the history
#1411)

## Whats added:
* Added logic fully-qualified - don't separate fully-qualified as a regular `dot qualified expression`
* Added 1 fix test and 5 tests examples
* Correct warning message

## Issue #1403
  • Loading branch information
Arrgentum authored Jun 29, 2022
1 parent 267be2d commit e9476dc
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,18 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
isDotQuaOrSafeAccessOrPostfixExpression(it) && it.elementType != POSTFIX_EXPRESSION
}.reversed()
if (listDot.size > 3) {
val without = listDot.filterNot {
val whiteSpaceBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev
whiteSpaceBeforeDotOrSafeAccess?.elementType == WHITE_SPACE && whiteSpaceBeforeDotOrSafeAccess.text.lines().size > 1
val without = listDot.filterIndexed { index, it ->
val nodeBeforeDotOrSafeAccess = it.findChildByType(DOT)?.treePrev ?: it.findChildByType(SAFE_ACCESS)?.treePrev
val firstElem = it.firstChildNode
val isTextContainsParenthesized = isTextContainsFunctionCall(firstElem)
val isNotWhiteSpaceBeforeDotOrSafeAccessContainNewLine = nodeBeforeDotOrSafeAccess?.elementType != WHITE_SPACE ||
(nodeBeforeDotOrSafeAccess.elementType != WHITE_SPACE && !nodeBeforeDotOrSafeAccess.textContains('\n'))
isTextContainsParenthesized && (index > 0) && isNotWhiteSpaceBeforeDotOrSafeAccessContainNewLine
}
if (without.size > 1 || (without.size == 1 && without[0] != listDot[0])) {
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "should be split before second and other dot/safe access", node.startOffset, node) {
fixDotQualifiedExpression(listDot)
if (without.isNotEmpty()) {
WRONG_NEWLINES.warnAndFix(configRules, emitWarn, isFixMode, "wrong split long `dot qualified expression` or `safe access expression`",
node.startOffset, node) {
fixDotQualifiedExpression(without)
}
}
}
Expand All @@ -190,12 +195,10 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
* 2) If before first Dot or Safe Access node stay White Space node with \n - remove this node
*/
private fun fixDotQualifiedExpression(list: List<ASTNode>) {
list.forEachIndexed { index, astNode ->
list.forEach { astNode ->
val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS)
val nodeBeforeDot = dotNode?.treePrev
if (index > 0) {
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode)
}
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode)
}
}

Expand Down Expand Up @@ -624,12 +627,14 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
}
return if (dropLeadingProperties) {
// fixme: we can't distinguish fully qualified names (like java.lang) from chain of property accesses (like list.size) for now
parentExpressionList?.dropWhile { !it.treeParent.textContains('(') && !it.treeParent.textContains('{') }
parentExpressionList?.dropWhile { !isTextContainsFunctionCall(it.treeParent) }
} else {
parentExpressionList
}
}

private fun isTextContainsFunctionCall(node: ASTNode): Boolean = node.textContains('(') || node.textContains('{')

private fun List<ASTNode>.isNotValidCalls(node: ASTNode): Boolean {
if (this.size == 1) {
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
mapOf("maxCallsInOneLine" to "1"))
)
private val ruleId = "$DIKTAT_RULE_SET_ID:${NewlinesRule.NAME_ID}"
private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} should be split before second and other dot/safe access"
private val dotQuaOrSafeAccessOrPostfixExpression = "${WRONG_NEWLINES.warnText()} wrong split long `dot qualified expression` or `safe access expression`"
private val shouldBreakAfter = "${WRONG_NEWLINES.warnText()} should break a line after and not before"
private val shouldBreakBefore = "${WRONG_NEWLINES.warnText()} should break a line before and not after"
private val functionalStyleWarn = "${WRONG_NEWLINES.warnText()} should follow functional style at"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package test.paragraph3.newlines

val elem1 = firstArgumentDot?.secondArgumentDot
val elem1 = firstArgumentDot()?.secondArgumentDot
?.thirdArgumentDot
?.fourthArgumentDot
?.fifthArgumentDot
?.sixthArgumentDot


val elem2 = firstArgumentDot?.secondArgumentDot
val elem2 = firstArgumentDot?.secondArgumentDot()
?.thirdArgumentDot
?.fourthArgumentDot
?.fifthArgumentDot
?.sixthArgumentDot


val elem3 = firstArgumentDot?.secondArgumentDot
?.thirdArgumentDot
val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot()
?.fourthArgumentDot
?.fifthArgumentDot
?.sixthArgumentDot
Expand All @@ -24,70 +23,79 @@ val elem3 = firstArgumentDot?.secondArgumentDot
val elem4 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot + firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot


val elem5 = firstArgumentDot!!.secondArgumentDot!!
val elem5 = firstArgumentDot()!!.secondArgumentDot()!!
.thirdArgumentDot!!
.fourthArgumentDot!!
.fifthArgumentDot!!
.sixthArgumentDot
.sixthArgumentDot()


val elem6 = firstArgumentDot!!.secondArgumentDot!!
.thirdArgumentDot!!
val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot()!!
.fourthArgumentDot!!
.fifthArgumentDot!!
.fifthArgumentDot()!!
.sixthArgumentDot


val elem7 = firstArgumentDot!!.secondArgumentDot!!
val elem7 = firstArgumentDot!!.secondArgumentDot()!!
.thirdArgumentDot!!
.fourthArgumentDot!!
.fourthArgumentDot()!!
.fifthArgumentDot!!
.sixthArgumentDot


val elem8 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot
val elem8 = firstArgumentDot()!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot


val elem9 = firstArgumentDot.secondArgumentDot
.thirdArgumentDot
val elem9 = firstArgumentDot().secondArgumentDot
.thirdArgumentDot()
.fourthArgumentDot
.fifthArgumentDot
.sixthArgumentDot


val elem10 = firstArgumentDot.secondArgumentDot
val elem10 = firstArgumentDot.secondArgumentDot()
.thirdArgumentDot
.fourthArgumentDot
.fifthArgumentDot
.fifthArgumentDot()
.sixthArgumentDot


val elem11 = firstArgumentDot.secondArgumentDot
.thirdArgumentDot
val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot()
.fourthArgumentDot
.fifthArgumentDot
.sixthArgumentDot


val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot
val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot().thirdArgumentDot.fourthArgumentDot


val elem13 = firstArgumentDot!!.secondArgumentDot
?.thirdArgumentDot
val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot()
.fourthArgumentDot!!
.fifthArgumentDot
.fifthArgumentDot()
?.sixthArgumentDot


val elem14 = firstArgumentDot.secondArgumentDot
?.thirdArgumentDot!!
val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot()!!
.fourthArgumentDot
?.fifthArgumentDot
.sixthArgumentDot


val elem15 = firstArgumentDot?.secondArgumentDot!!
.thirdArgumentDot
.fourthArgumentDot
val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot()
.fifthArgumentDot!!
.sixthArgumentDot


val elem16 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot


val elem17 = firstArgumentDot!!.secondArgumentDot.thirdArgumentDot!!.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot


val elem18 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot


private val holder: java.util.concurrent.atomic.AtomicReference<T> = java.util.concurrent.atomic.AtomicReference(valueToStore)


private val holder: kotlin.native.concurrent.AtomicReference<T> = kotlin.native.concurrent.AtomicReference(valueToStore)
Original file line number Diff line number Diff line change
@@ -1,51 +1,66 @@
package test.paragraph3.newlines

val elem1 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot
val elem1 = firstArgumentDot()?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot


val elem2 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot
val elem2 = firstArgumentDot?.secondArgumentDot()?.thirdArgumentDot
?.fourthArgumentDot?.fifthArgumentDot?.sixthArgumentDot


val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot
val elem3 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot()?.fourthArgumentDot
?.fifthArgumentDot?.sixthArgumentDot


val elem4 = firstArgumentDot?.secondArgumentDot?.thirdArgumentDot + firstArgumentDot?.secondArgumentDot?.thirdArgumentDot?.fourthArgumentDot


val elem5 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot
val elem5 = firstArgumentDot()!!.secondArgumentDot()!!.thirdArgumentDot!!.fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot()


val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!
.fourthArgumentDot!!.fifthArgumentDot!!.sixthArgumentDot
val elem6 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot()!!
.fourthArgumentDot!!.fifthArgumentDot()!!.sixthArgumentDot


val elem7 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot!!
val elem7 = firstArgumentDot!!.secondArgumentDot()!!.thirdArgumentDot!!.fourthArgumentDot()!!
.fifthArgumentDot!!.sixthArgumentDot


val elem8 = firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot
val elem8 = firstArgumentDot()!!.secondArgumentDot!!.thirdArgumentDot + firstArgumentDot!!.secondArgumentDot!!.thirdArgumentDot!!.fourthArgumentDot


val elem9 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot
val elem9 = firstArgumentDot().secondArgumentDot.thirdArgumentDot().fourthArgumentDot.fifthArgumentDot.sixthArgumentDot


val elem10 = firstArgumentDot.secondArgumentDot.thirdArgumentDot
.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot
val elem10 = firstArgumentDot.secondArgumentDot().thirdArgumentDot
.fourthArgumentDot.fifthArgumentDot().sixthArgumentDot


val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot
val elem11 = firstArgumentDot.secondArgumentDot.thirdArgumentDot().fourthArgumentDot
.fifthArgumentDot.sixthArgumentDot


val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot
val elem12 = firstArgumentDot.secondArgumentDot.thirdArgumentDot + firstArgumentDot.secondArgumentDot().thirdArgumentDot.fourthArgumentDot


val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot!!.fifthArgumentDot?.sixthArgumentDot
val elem13 = firstArgumentDot!!.secondArgumentDot?.thirdArgumentDot().fourthArgumentDot!!.fifthArgumentDot()?.sixthArgumentDot


val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot!!.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot
val elem14 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot()!!.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot


val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot
val elem15 = firstArgumentDot?.secondArgumentDot!!.thirdArgumentDot.fourthArgumentDot().fifthArgumentDot!!.sixthArgumentDot


val elem16 = firstArgumentDot.secondArgumentDot.thirdArgumentDot.fourthArgumentDot.fifthArgumentDot.sixthArgumentDot


val elem17 = firstArgumentDot!!.secondArgumentDot.thirdArgumentDot!!.fourthArgumentDot.fifthArgumentDot!!.sixthArgumentDot


val elem18 = firstArgumentDot.secondArgumentDot?.thirdArgumentDot.fourthArgumentDot?.fifthArgumentDot.sixthArgumentDot


private val holder: java.util.concurrent.atomic.AtomicReference<T> = java.util.concurrent.atomic.AtomicReference(valueToStore)


private val holder: kotlin.native.concurrent.AtomicReference<T> = kotlin.native.concurrent.AtomicReference(valueToStore)

0 comments on commit e9476dc

Please sign in to comment.