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 @@ -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
Expand Down Expand Up @@ -80,12 +82,11 @@ class SortRule(private val configRules: List<RulesConfig>) : Rule("sort-rule") {
sortList: List<ASTNode>,
nonSortList: List<ASTNode>,
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) }
petertrr marked this conversation as resolved.
Show resolved Hide resolved
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,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
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,9 @@ enum class Alp {
RED(0xFF0000),
GREEN(0x00FF00),
BLUE(0x0000FF)
;
;
}

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