Skip to content

Commit

Permalink
## Returned to the previous version
Browse files Browse the repository at this point in the history
## Issue (#1403)
  • Loading branch information
Arrgentum committed Jun 27, 2022
1 parent 75bf80f commit dc3d4fb
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,77 +131,18 @@ class NewlinesRule(configRules: List<RulesConfig>) : DiktatRule(
when (node.elementType) {
SEMICOLON -> handleSemicolon(node)
OPERATION_REFERENCE, EQ -> handleOperatorWithLineBreakAfter(node)
// this logic regulates indentation with elements - so that the symbol and the subsequent expression are on the same line
in lineBreakBeforeOperators -> handleOperatorWithLineBreakBefore(node)
LPAR -> handleOpeningParentheses(node)
COMMA -> handleComma(node)
COLON -> handleColon(node)
BLOCK -> handleLambdaBody(node)
RETURN -> handleReturnStatement(node)
SUPER_TYPE_LIST, VALUE_PARAMETER_LIST, VALUE_ARGUMENT_LIST -> handleList(node)
// this logic splits long expressions into multiple lines
DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, POSTFIX_EXPRESSION -> handDotQualifiedAndSafeAccessExpression(node)
else -> {
}
}
}

@Suppress("GENERIC_VARIABLE_WRONG_DECLARATION", "MagicNumber")
private fun handDotQualifiedAndSafeAccessExpression(node: ASTNode) {
val listParentTypesNoFix = listOf(PACKAGE_DIRECTIVE, IMPORT_DIRECTIVE, VALUE_PARAMETER_LIST,
VALUE_ARGUMENT_LIST, DOT_QUALIFIED_EXPRESSION, SAFE_ACCESS_EXPRESSION, POSTFIX_EXPRESSION)
if (isNotFindParentNodeWithSpecificManyType(node, listParentTypesNoFix)) {
val listDot = node.findAllNodesWithCondition(
withSelf = true,
excludeChildrenCondition = { !isDotQuaOrSafeAccessOrPostfixExpression(it) }
) {
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
}
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)
}
}
}
}
}

/**
* Return false, if you find parent with types in list else return true
*/
private fun isNotFindParentNodeWithSpecificManyType(node: ASTNode, list: List<IElementType>): Boolean {
list.forEach { elem ->
node.findParentNodeWithSpecificType(elem)?.let {
return false
}
}
return true
}

/**
* Fix Dot Qualified Expression and Safe Access Expression -
* 1) Append new White Space node before second and subsequent node Dot or Safe Access
* in Dot Qualified Expression? Safe Access Expression and Postfix Expression
* 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 ->
val dotNode = astNode.getFirstChildWithType(DOT) ?: astNode.getFirstChildWithType(SAFE_ACCESS)
val nodeBeforeDot = dotNode?.treePrev
if (index > 0) {
astNode.appendNewlineMergingWhiteSpace(nodeBeforeDot, dotNode)
}
}
}

private fun isDotQuaOrSafeAccessOrPostfixExpression(node: ASTNode) =
node.elementType == DOT_QUALIFIED_EXPRESSION || node.elementType == SAFE_ACCESS_EXPRESSION || node.elementType == POSTFIX_EXPRESSION

/**
* Check that EOL semicolon is used only in enums
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,4 @@ class NewlinesRuleFixTest : FixTestBase("test/paragraph3/newlines", ::NewlinesRu
fun `list argument in lambda`() {
fixAndCompare("ListArgumentLambdaExpected.kt", "ListArgumentLambdaTest.kt")
}

@Test
@Tag(WarningNames.WRONG_NEWLINES)
fun `long dot qualified expression`() {
fixAndCompare("LongDotQualifiedExpressionExpected.kt", "LongDotQualifiedExpressionTest.kt")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| }?.qux()
|}
""".trimMargin(),
LintError(2, 5, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(2, 11, ruleId, "$functionalStyleWarn .", true),
LintError(3, 26, ruleId, "$functionalStyleWarn .", true),
LintError(5, 10, ruleId, "$functionalStyleWarn ?.", true),
Expand Down Expand Up @@ -814,9 +813,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| x.map().gre().few().qwe()
|}
""".trimMargin(),
LintError(2, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(3, 22, ruleId, "${WRONG_NEWLINES.warnText()} should follow functional style at .", true),
LintError(13, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(13, 23, ruleId, "${WRONG_NEWLINES.warnText()} should follow functional style at .", true)
)
}
Expand Down Expand Up @@ -844,9 +841,7 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| .few()
|}
""".trimMargin(),
LintError(2, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(4, 22, ruleId, "$functionalStyleWarn .", true),
LintError(8, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(9, 22, ruleId, "$functionalStyleWarn .", true)
)
}
Expand Down Expand Up @@ -972,7 +967,6 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| x.gf().fge().qwe().fd()
|}
""".trimMargin(),
LintError(6, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(6, 22, ruleId, "$functionalStyleWarn .", true), rulesConfigList = rulesConfigList
)
}
Expand Down Expand Up @@ -1000,7 +994,6 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
| .qwe()
|}
""".trimMargin(),
LintError(9, 4, ruleId, dotQuaOrSafeAccessOrPostfixExpression, true),
LintError(9, 29, ruleId, "$functionalStyleWarn .", true)
)
}
Expand Down Expand Up @@ -1166,4 +1159,16 @@ class NewlinesRuleWarnTest : LintTestBase(::NewlinesRule) {
fileName = "build.gradle.kts"
)
}

@Test
fun `Long Dot Qualified Exoression should not be separated`() {
lintMethod(
"""
| 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)
|
""".trimMargin()
)
}
}

0 comments on commit dc3d4fb

Please sign in to comment.