Trailing comma: cleanup code after review

#KT-34744
This commit is contained in:
Dmitry Gridin
2020-01-23 01:32:22 +07:00
parent ac8a8ecab4
commit 80194dd536
4 changed files with 13 additions and 13 deletions
@@ -671,5 +671,5 @@ fun getTrailingCommaByClosingElement(closingElement: PsiElement?): PsiElement? {
fun getTrailingCommaByElementsList(elementList: PsiElement?): PsiElement? { fun getTrailingCommaByElementsList(elementList: PsiElement?): PsiElement? {
val lastChild = elementList?.lastChild?.let { if (it !is PsiComment) it else it.getPrevSiblingIgnoringWhitespaceAndComments() } val lastChild = elementList?.lastChild?.let { if (it !is PsiComment) it else it.getPrevSiblingIgnoringWhitespaceAndComments() }
return lastChild?.run { if (node.elementType == KtTokens.COMMA) this else null } return lastChild?.takeIf { it.node.elementType == KtTokens.COMMA }
} }
@@ -588,7 +588,8 @@ abstract class KotlinCommonBlock(
when { when {
elementType === VALUE_ARGUMENT_LIST -> { elementType === VALUE_ARGUMENT_LIST -> {
val wrapSetting = commonSettings.CALL_PARAMETERS_WRAP val wrapSetting = commonSettings.CALL_PARAMETERS_WRAP
if (!node.trailingCommaIsAllowed && (wrapSetting == CommonCodeStyleSettings.WRAP_AS_NEEDED || wrapSetting == CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM) && if (!node.trailingCommaIsAllowed &&
(wrapSetting == CommonCodeStyleSettings.WRAP_AS_NEEDED || wrapSetting == CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM) &&
!needWrapArgumentList(nodePsi) !needWrapArgumentList(nodePsi)
) { ) {
return ::noWrapping return ::noWrapping
@@ -752,12 +753,10 @@ abstract class KotlinCommonBlock(
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement) fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
private val ASTNode.trailingCommaIsAllowed: Boolean private val ASTNode.trailingCommaIsAllowed: Boolean
get() = if (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA || get() = (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA) &&
) psi?.let(PsiElement::isMultiline) == true
psi?.let(PsiElement::isMultiline) == true
else
false
private fun ASTNode.notDelimiterSiblingNodeInSequence( private fun ASTNode.notDelimiterSiblingNodeInSequence(
forward: Boolean, forward: Boolean,
@@ -76,11 +76,12 @@ fun <T : PsiElement> T.needTrailingComma(
additionalCheck: () -> Boolean = { true }, additionalCheck: () -> Boolean = { true },
globalStartOffset: T.() -> Int? = PsiElement::startOffset, globalStartOffset: T.() -> Int? = PsiElement::startOffset,
globalEndOffset: T.() -> Int? = PsiElement::endOffset globalEndOffset: T.() -> Int? = PsiElement::endOffset
) = (trailingComma() != null || settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA) && additionalCheck() && run(fun(): Boolean { ): Boolean {
if (trailingComma() == null && !settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA || !additionalCheck()) return false
val startOffset = globalStartOffset() ?: return false val startOffset = globalStartOffset() ?: return false
val endOffset = globalEndOffset() ?: return false val endOffset = globalEndOffset() ?: return false
return containsLineBreakInThis(startOffset, endOffset) return containsLineBreakInThis(startOffset, endOffset)
}) }
fun PsiElement.containsLineBreakInThis(globalStartOffset: Int, globalEndOffset: Int): Boolean { fun PsiElement.containsLineBreakInThis(globalStartOffset: Int, globalEndOffset: Int): Boolean {
val textRange = TextRange.create(globalStartOffset, globalEndOffset).shiftLeft(startOffset) val textRange = TextRange.create(globalStartOffset, globalEndOffset).shiftLeft(startOffset)
@@ -129,7 +129,7 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
when { when {
needComma(parent, settings, false) -> { needComma(parent, settings, false) -> {
// add a missing comma // add a missing comma
if (elementType !== KtTokens.COMMA && trailingCommaAllowedInModule(parent)) { if (elementType != KtTokens.COMMA && trailingCommaAllowedInModule(parent)) {
lastElement.addCommaAfter(KtPsiFactory(parent)) lastElement.addCommaAfter(KtPsiFactory(parent))
} }
@@ -138,7 +138,7 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
needComma(parent, settings) -> { needComma(parent, settings) -> {
correctCommaPosition(parent) correctCommaPosition(parent)
} }
elementType === KtTokens.COMMA -> { elementType == KtTokens.COMMA -> {
// remove redundant comma // remove redundant comma
lastElement.delete() lastElement.delete()
} }
@@ -225,4 +225,4 @@ fun PsiElement.leaf(forward: Boolean = true, filter: (PsiElement) -> Boolean): P
if (forward) nextLeaf(filter) if (forward) nextLeaf(filter)
else prevLeaf(filter) else prevLeaf(filter)
val PsiElement.isComma: Boolean get() = elementType === KtTokens.COMMA val PsiElement.isComma: Boolean get() = elementType == KtTokens.COMMA