From 3ae19c6ea2b9d01f989d2a19fa4b0638071e6794 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 25 Jan 2021 13:34:44 +0300 Subject: [PATCH 1/6] Enum order breaks white space ### What's done: Rewrote algorithm to swap element --- .../org/cqfn/diktat/ruleset/rules/SortRule.kt | 7 ++++--- .../sort_error/ConstantsExpected.kt | 8 ++++---- .../paragraph3/sort_error/EnumSortExpected.kt | 20 +++++++++++-------- .../paragraph3/sort_error/EnumSortTest.kt | 6 +++++- .../smoke/src/main/kotlin/Example3Expected.kt | 4 ++++ .../smoke/src/main/kotlin/Example3Test.kt | 3 +++ 6 files changed, 32 insertions(+), 16 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt index 2366992cb5..b119a2fa7a 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt @@ -17,8 +17,10 @@ import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON +import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextSibling +import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl @@ -80,12 +82,11 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { sortList: List, nonSortList: List, node: ASTNode) { + val spaceBefore = nonSortList.map { astNode -> astNode.prevSibling { it.elementType == WHITE_SPACE } } val nodeBefore: ASTNode? = nonSortList.last().treeNext node.removeRange(nonSortList.first(), nonSortList.last().treeNext) sortList.forEachIndexed { nodeIndex, astNode -> - if (nodeIndex != 0) { - node.addChild(PsiWhiteSpaceImpl("\n"), nodeBefore) - } + spaceBefore[nodeIndex]?.let { node.addChild(it, nodeBefore) } node.addChild(astNode, nodeBefore) } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/ConstantsExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/ConstantsExpected.kt index ba8d1eedb5..c10d078c44 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/ConstantsExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/ConstantsExpected.kt @@ -3,8 +3,8 @@ package test.paragraph3.sort_error class Test { companion object { private const val B = 4 -private const val C = 4 -private const val D = 4 + private const val C = 4 + private const val D = 4 private val SIMPLE_VALUE = listOf(IDENTIFIER, WHITE_SPACE, COMMA, SEMICOLON) } } @@ -28,7 +28,7 @@ class Test2 { private const val D = 4 private val SIMPLE_VALUE = listOf(IDENTIFIER, WHITE_SPACE, COMMA, SEMICOLON) private const val Ba = 4 -private const val Baa = 4 -private const val Bb = 4 + private const val Baa = 4 + private const val Bb = 4 } } diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt index 348610a1a6..2bb3225b10 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt @@ -2,8 +2,8 @@ package test.paragraph3.sort_error enum class Alp { BLUE(0x0000FF), -GREEN(0x00FF00), -RED(0xFF0000), + GREEN(0x00FF00), + RED(0xFF0000), ; } @@ -11,7 +11,7 @@ enum class Warnings { TALKING { override fun signal() = TALKING }, -WAITING { + WAITING { override fun signal() = TALKING }, ; @@ -23,7 +23,7 @@ enum class Warnings { TALKING { override fun signal() = TALKING }, -WAITING { + WAITING { override fun signal() = TALKING }; @@ -32,13 +32,17 @@ WAITING { enum class Alp { BLUE(0x0000FF), -GREEN(0x00FF00), -RED(0xFF0000), + GREEN(0x00FF00), + RED(0xFF0000), } enum class Alp { BLUE(0x0000FF), -GREEN(0x00FF00), -RED(0xFF0000) + GREEN(0x00FF00), + RED(0xFF0000) ; } + +enum class IssueType { + PROJECT_STRUCTURE, TESTS, VCS +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt index e663ba99f7..bed565375f 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt @@ -40,5 +40,9 @@ enum class Alp { RED(0xFF0000), GREEN(0x00FF00), BLUE(0x0000FF) - ; +; +} + +enum class IssueType { + VCS, PROJECT_STRUCTURE, TESTS } diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt index 515da9b064..389ef88a3b 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Expected.kt @@ -45,6 +45,10 @@ class Example { } } +enum class IssueType { + PROJECT_STRUCTURE, TESTS, VCS +} + class Foo { /** * @implNote lorem ipsum diff --git a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt index ddd801edf8..ee6d20b539 100644 --- a/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt +++ b/diktat-rules/src/test/resources/test/smoke/src/main/kotlin/Example3Test.kt @@ -41,6 +41,9 @@ class Example { } } +enum class IssueType { + VCS, PROJECT_STRUCTURE, TESTS +} class Foo { /** From a99ab64e369be67789b1140abd27eb088de10cbf Mon Sep 17 00:00:00 2001 From: kentr0w Date: Mon, 25 Jan 2021 18:07:32 +0300 Subject: [PATCH 2/6] Enum order breaks white space ### What's done: Fixed after review --- .../kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt | 10 +++++++++- .../test/paragraph3/sort_error/EnumSortExpected.kt | 4 ++++ .../test/paragraph3/sort_error/EnumSortTest.kt | 4 ++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt index b119a2fa7a..56474baaf2 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/SortRule.kt @@ -13,6 +13,7 @@ import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.CONST_KEYWORD +import com.pinterest.ktlint.core.ast.ElementType.ENUM_ENTRY import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.ElementType.PROPERTY @@ -82,7 +83,14 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { sortList: List, nonSortList: List, node: ASTNode) { - val spaceBefore = nonSortList.map { astNode -> astNode.prevSibling { it.elementType == WHITE_SPACE } } + val spaceBefore = nonSortList.map { astNode -> + val nodeBefore = astNode.prevSibling { it.elementType == WHITE_SPACE || it.elementType == ENUM_ENTRY } + if (nodeBefore?.elementType == WHITE_SPACE) { + nodeBefore + } else { + null + } + } val nodeBefore: ASTNode? = nonSortList.last().treeNext node.removeRange(nonSortList.first(), nonSortList.last().treeNext) sortList.forEachIndexed { nodeIndex, astNode -> diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt index 2bb3225b10..a1712e7a52 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt @@ -46,3 +46,7 @@ enum class Alp { enum class IssueType { PROJECT_STRUCTURE, TESTS, VCS } + +enum class IssueType { + PROJECT_STRUCTURE,TESTS,VCS +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt index bed565375f..d93298e2a5 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt @@ -46,3 +46,7 @@ enum class Alp { enum class IssueType { VCS, PROJECT_STRUCTURE, TESTS } + +enum class IssueType { + VCS,PROJECT_STRUCTURE,TESTS +} From c1e69f020422d7c17fe5ed2cebe66271cd30a917 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 9 Feb 2021 14:48:46 +0300 Subject: [PATCH 3/6] Enum order breaks white space ### What's done: Fixed according to new condition with comment --- .../diktat/ruleset/rules/chapter3/SortRule.kt | 40 ++++++++++++++++--- .../paragraph3/sort_error/EnumSortExpected.kt | 12 ++++++ .../paragraph3/sort_error/EnumSortTest.kt | 9 +++++ 3 files changed, 56 insertions(+), 5 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt index 3370dc7329..5587632bdd 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt @@ -5,27 +5,31 @@ import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig import org.cqfn.diktat.ruleset.constants.EmitType import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_DECLARATIONS_ORDER +import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType import org.cqfn.diktat.ruleset.utils.hasChildOfType import org.cqfn.diktat.ruleset.utils.isClassEnum import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.CONST_KEYWORD import com.pinterest.ktlint.core.ast.ElementType.ENUM_ENTRY +import com.pinterest.ktlint.core.ast.ElementType.EOL_COMMENT import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE +import com.pinterest.ktlint.core.ast.isPartOfComment import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline +import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.nextSibling import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl import org.jetbrains.kotlin.psi.KtObjectDeclaration +import org.jetbrains.kotlin.psi.psiUtil.siblings /** * Rule that sorts class properties and enum members alphabetically @@ -99,6 +103,32 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { } } + private fun swapSortEnumEntry( + sortList: List, + nonSortList: List, + node: ASTNode) { + val spaceBefore = if (node.findAllNodesWithSpecificType(EOL_COMMENT).isNotEmpty()) { + val lastEntry = node.findAllNodesWithSpecificType(ENUM_ENTRY).last() + if (lastEntry.hasChildOfType(EOL_COMMENT) && !lastEntry.hasChildOfType(COMMA)) { + lastEntry.addChild(LeafPsiElement(COMMA, ","), lastEntry.findChildByType(EOL_COMMENT)) + } + listOf(null) + List(nonSortList.size - 1) {listOf(PsiWhiteSpaceImpl("\n"))} + } else { + nonSortList.map { astNode -> + astNode.siblings(false).toList().takeWhile { it.isWhiteSpace() || it.isPartOfComment() }.ifEmpty { null } + } + } + val nodeBefore: ASTNode? = nonSortList.last().treeNext + node.removeRange(nonSortList.first(), nonSortList.last().treeNext) + sortList.mapIndexed { index, astNode -> + spaceBefore[index]?.let { prevList -> prevList.map { node.addChild(it, nodeBefore) } } + if (!astNode.hasChildOfType(COMMA)) { + astNode.addChild(LeafPsiElement(COMMA, ","), null) + } + node.addChild(astNode, nodeBefore) + } + } + @Suppress("TYPE_ALIAS") private fun createOrderListOfList(propertyList: List): List> { var oneOrderList = mutableListOf(propertyList.first()) @@ -117,7 +147,7 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { @Suppress("UnsafeCallOnNullableType") private fun sortEnum(node: ASTNode) { - val enumEntryList = node.getChildren(null).filter { it.elementType == ElementType.ENUM_ENTRY } + val enumEntryList = node.getChildren(null).filter { it.elementType == ENUM_ENTRY } if (enumEntryList.size <= 1) { return } @@ -126,10 +156,10 @@ class SortRule(private val configRules: List) : Rule("sort-rule") { WRONG_DECLARATIONS_ORDER.warnAndFix(configRules, emitWarn, isFixMode, "enum entries order is incorrect", node.startOffset, node) { val (isEndSemicolon, isEndSpace) = removeLastSemicolonAndSpace(enumEntryList.last()) val hasTrailingComma = (sortList.last() != enumEntryList.last() && enumEntryList.last().hasChildOfType(COMMA)) - swapSortNodes(sortList, enumEntryList, node) + swapSortEnumEntry(sortList, enumEntryList, node) if (!hasTrailingComma) { - enumEntryList.last().addChild(LeafPsiElement(COMMA, ","), null) - sortList.last().removeChild(sortList.last().findChildByType(COMMA)!!) + val lastEntry = node.findAllNodesWithSpecificType(ENUM_ENTRY).last() + lastEntry.removeChild(lastEntry.findChildByType(COMMA)!!) } if (isEndSpace) { sortList.last().addChild(PsiWhiteSpaceImpl("\n"), null) diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt index a1712e7a52..e48588d699 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortExpected.kt @@ -50,3 +50,15 @@ enum class IssueType { enum class IssueType { PROJECT_STRUCTURE,TESTS,VCS } + +enum class IssueType { + PROJECT_STRUCTURE, // comment +TESTS, +VCS +} + +enum class IssueType { + PROJECT_STRUCTURE ,// comment +TESTS, +VCS +} diff --git a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt index d93298e2a5..1c87f8c520 100644 --- a/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt +++ b/diktat-rules/src/test/resources/test/paragraph3/sort_error/EnumSortTest.kt @@ -50,3 +50,12 @@ enum class IssueType { enum class IssueType { VCS,PROJECT_STRUCTURE,TESTS } + +enum class IssueType { + VCS,PROJECT_STRUCTURE, // comment + TESTS +} + +enum class IssueType { + VCS, TESTS, PROJECT_STRUCTURE // comment +} From c28e2b570161919ba21298ead02c4cfdbe6b3e39 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 9 Feb 2021 14:50:46 +0300 Subject: [PATCH 4/6] Enum order breaks white space ### What's done: Fixed according to new condition with comment --- .../kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt | 3 --- 1 file changed, 3 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt index 28a888adbf..7ff4e64ed7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt @@ -3,15 +3,12 @@ package org.cqfn.diktat.ruleset.rules.chapter3 import org.cqfn.diktat.common.config.rules.RuleConfiguration import org.cqfn.diktat.common.config.rules.RulesConfig import org.cqfn.diktat.common.config.rules.getRuleConfig -import org.cqfn.diktat.ruleset.constants.EmitType import org.cqfn.diktat.ruleset.constants.Warnings.WRONG_DECLARATIONS_ORDER import org.cqfn.diktat.ruleset.rules.DiktatRule import org.cqfn.diktat.ruleset.utils.findAllNodesWithSpecificType import org.cqfn.diktat.ruleset.utils.hasChildOfType import org.cqfn.diktat.ruleset.utils.isClassEnum -import com.pinterest.ktlint.core.Rule -import com.pinterest.ktlint.core.ast.ElementType import com.pinterest.ktlint.core.ast.ElementType.CLASS_BODY import com.pinterest.ktlint.core.ast.ElementType.COMMA import com.pinterest.ktlint.core.ast.ElementType.CONST_KEYWORD From 8ef90a31577c5be41cd3950569cc83b2c241ac0e Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 9 Feb 2021 15:17:15 +0300 Subject: [PATCH 5/6] Enum order breaks white space ### What's done: Fixed according to our code-style --- .../cqfn/diktat/ruleset/rules/chapter3/SortRule.kt | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt index 7ff4e64ed7..98cd3c0053 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt @@ -20,8 +20,8 @@ import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isPartOfComment -import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.isWhiteSpace +import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextSibling import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode @@ -98,15 +98,19 @@ class SortRule(configRules: List) : DiktatRule("sort-rule", configR sortList: List, nonSortList: List, node: ASTNode) { - val spaceBefore = if (node.findAllNodesWithSpecificType(EOL_COMMENT).isNotEmpty()) { + val spaceBefore = if (node.findAllNodesWithSpecificType(EOL_COMMENT).isNotEmpty()) { val lastEntry = node.findAllNodesWithSpecificType(ENUM_ENTRY).last() if (lastEntry.hasChildOfType(EOL_COMMENT) && !lastEntry.hasChildOfType(COMMA)) { lastEntry.addChild(LeafPsiElement(COMMA, ","), lastEntry.findChildByType(EOL_COMMENT)) } - listOf(null) + List(nonSortList.size - 1) {listOf(PsiWhiteSpaceImpl("\n"))} + listOf(null) + List(nonSortList.size - 1) { listOf(PsiWhiteSpaceImpl("\n")) } } else { nonSortList.map { astNode -> - astNode.siblings(false).toList().takeWhile { it.isWhiteSpace() || it.isPartOfComment() }.ifEmpty { null } + astNode + .siblings(false) + .toList() + .takeWhile { it.isWhiteSpace() || it.isPartOfComment() } + .ifEmpty { null } } } val nodeBefore: ASTNode? = nonSortList.last().treeNext From c5100ae01403ac7d5858a79539d48e3c22f134c9 Mon Sep 17 00:00:00 2001 From: kentr0w Date: Tue, 9 Feb 2021 19:00:08 +0300 Subject: [PATCH 6/6] Enum order breaks white space ### What's done: Fixed after review. Simplified code --- .../diktat/ruleset/rules/chapter3/SortRule.kt | 48 +++++++------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt index 98cd3c0053..935e7805f7 100644 --- a/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt +++ b/diktat-rules/src/main/kotlin/org/cqfn/diktat/ruleset/rules/chapter3/SortRule.kt @@ -18,12 +18,10 @@ import com.pinterest.ktlint.core.ast.ElementType.IDENTIFIER import com.pinterest.ktlint.core.ast.ElementType.MODIFIER_LIST import com.pinterest.ktlint.core.ast.ElementType.PROPERTY import com.pinterest.ktlint.core.ast.ElementType.SEMICOLON -import com.pinterest.ktlint.core.ast.ElementType.WHITE_SPACE import com.pinterest.ktlint.core.ast.isPartOfComment import com.pinterest.ktlint.core.ast.isWhiteSpace import com.pinterest.ktlint.core.ast.isWhiteSpaceWithNewline import com.pinterest.ktlint.core.ast.nextSibling -import com.pinterest.ktlint.core.ast.prevSibling import org.jetbrains.kotlin.com.intellij.lang.ASTNode import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.PsiWhiteSpaceImpl @@ -74,36 +72,24 @@ class SortRule(configRules: List) : DiktatRule("sort-rule", configR } } + @OptIn(ExperimentalStdlibApi::class) private fun swapSortNodes( sortList: List, nonSortList: List, node: ASTNode) { - val spaceBefore = nonSortList.map { astNode -> - val nodeBefore = astNode.prevSibling { it.elementType == WHITE_SPACE || it.elementType == ENUM_ENTRY } - if (nodeBefore?.elementType == WHITE_SPACE) { - nodeBefore - } else { - null + val isEnum = nonSortList.first().elementType == ENUM_ENTRY + val spaceBefore = if (node.findAllNodesWithSpecificType(EOL_COMMENT).isNotEmpty() && isEnum) { + nonSortList.last().run { + if (this.hasChildOfType(EOL_COMMENT) && !this.hasChildOfType(COMMA)) { + this.addChild(LeafPsiElement(COMMA, ","), this.findChildByType(EOL_COMMENT)) + } } - } - val nodeBefore: ASTNode? = nonSortList.last().treeNext - node.removeRange(nonSortList.first(), nonSortList.last().treeNext) - sortList.forEachIndexed { nodeIndex, astNode -> - spaceBefore[nodeIndex]?.let { node.addChild(it, nodeBefore) } - node.addChild(astNode, nodeBefore) - } - } - - private fun swapSortEnumEntry( - sortList: List, - nonSortList: List, - node: ASTNode) { - val spaceBefore = if (node.findAllNodesWithSpecificType(EOL_COMMENT).isNotEmpty()) { - val lastEntry = node.findAllNodesWithSpecificType(ENUM_ENTRY).last() - if (lastEntry.hasChildOfType(EOL_COMMENT) && !lastEntry.hasChildOfType(COMMA)) { - lastEntry.addChild(LeafPsiElement(COMMA, ","), lastEntry.findChildByType(EOL_COMMENT)) + buildList(nonSortList.size) { + add(null) + repeat(nonSortList.size - 1) { + add(listOf(PsiWhiteSpaceImpl("\n"))) + } } - listOf(null) + List(nonSortList.size - 1) { listOf(PsiWhiteSpaceImpl("\n")) } } else { nonSortList.map { astNode -> astNode @@ -113,14 +99,14 @@ class SortRule(configRules: List) : DiktatRule("sort-rule", configR .ifEmpty { null } } } - val nodeBefore: ASTNode? = nonSortList.last().treeNext + val nodeInsertBefore: ASTNode? = nonSortList.last().treeNext node.removeRange(nonSortList.first(), nonSortList.last().treeNext) sortList.mapIndexed { index, astNode -> - spaceBefore[index]?.let { prevList -> prevList.map { node.addChild(it, nodeBefore) } } - if (!astNode.hasChildOfType(COMMA)) { + spaceBefore[index]?.let { prevList -> prevList.map { node.addChild(it, nodeInsertBefore) } } + if (!astNode.hasChildOfType(COMMA) && isEnum) { astNode.addChild(LeafPsiElement(COMMA, ","), null) } - node.addChild(astNode, nodeBefore) + node.addChild(astNode, nodeInsertBefore) } } @@ -151,7 +137,7 @@ class SortRule(configRules: List) : DiktatRule("sort-rule", configR WRONG_DECLARATIONS_ORDER.warnAndFix(configRules, emitWarn, isFixMode, "enum entries order is incorrect", node.startOffset, node) { val (isEndSemicolon, isEndSpace) = removeLastSemicolonAndSpace(enumEntryList.last()) val hasTrailingComma = (sortList.last() != enumEntryList.last() && enumEntryList.last().hasChildOfType(COMMA)) - swapSortEnumEntry(sortList, enumEntryList, node) + swapSortNodes(sortList, enumEntryList, node) if (!hasTrailingComma) { val lastEntry = node.findAllNodesWithSpecificType(ENUM_ENTRY).last() lastEntry.removeChild(lastEntry.findChildByType(COMMA)!!)