Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enum order breaks white space #726

Merged
merged 9 commits into from
Feb 10, 2021
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@ 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
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
Expand Down Expand Up @@ -80,12 +83,18 @@ class SortRule(private val configRules: List<RulesConfig>) : Rule("sort-rule") {
sortList: List<ASTNode>,
nonSortList: List<ASTNode>,
node: ASTNode) {
val spaceBefore = nonSortList.map { astNode ->
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
val nodeBefore = astNode.prevSibling { it.elementType == WHITE_SPACE || it.elementType == ENUM_ENTRY }
if (nodeBefore?.elementType == WHITE_SPACE) {
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
nodeBefore
} else {
null
}
kentr0w marked this conversation as resolved.
Show resolved Hide resolved
}
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)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ package test.paragraph3.sort_error

enum class Alp {
BLUE(0x0000FF),
GREEN(0x00FF00),
RED(0xFF0000),
GREEN(0x00FF00),
RED(0xFF0000),
;
}

enum class Warnings {
TALKING {
override fun signal() = TALKING
},
WAITING {
WAITING {
override fun signal() = TALKING
},
;
Expand All @@ -23,7 +23,7 @@ enum class Warnings {
TALKING {
override fun signal() = TALKING
},
WAITING {
WAITING {
override fun signal() = TALKING
};

Expand All @@ -32,13 +32,21 @@ 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
}

enum class IssueType {
PROJECT_STRUCTURE,TESTS,VCS
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,13 @@ enum class Alp {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
;
;
}

enum class IssueType {
VCS, PROJECT_STRUCTURE, TESTS
}

enum class IssueType {
VCS,PROJECT_STRUCTURE,TESTS
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class Example {
}
}

enum class IssueType {
PROJECT_STRUCTURE, TESTS, VCS
}

class Foo {
/**
* @implNote lorem ipsum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ class Example {
}
}

enum class IssueType {
VCS, PROJECT_STRUCTURE, TESTS
}

class Foo {
/**
Expand Down