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? {
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 {
elementType === VALUE_ARGUMENT_LIST -> {
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)
) {
return ::noWrapping
@@ -752,12 +753,10 @@ abstract class KotlinCommonBlock(
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
private val ASTNode.trailingCommaIsAllowed: Boolean
get() = if (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA
)
psi?.let(PsiElement::isMultiline) == true
else
false
get() = (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA) &&
psi?.let(PsiElement::isMultiline) == true
private fun ASTNode.notDelimiterSiblingNodeInSequence(
forward: Boolean,
@@ -76,11 +76,12 @@ fun <T : PsiElement> T.needTrailingComma(
additionalCheck: () -> Boolean = { true },
globalStartOffset: T.() -> Int? = PsiElement::startOffset,
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 endOffset = globalEndOffset() ?: return false
return containsLineBreakInThis(startOffset, endOffset)
})
}
fun PsiElement.containsLineBreakInThis(globalStartOffset: Int, globalEndOffset: Int): Boolean {
val textRange = TextRange.create(globalStartOffset, globalEndOffset).shiftLeft(startOffset)
@@ -129,7 +129,7 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
when {
needComma(parent, settings, false) -> {
// add a missing comma
if (elementType !== KtTokens.COMMA && trailingCommaAllowedInModule(parent)) {
if (elementType != KtTokens.COMMA && trailingCommaAllowedInModule(parent)) {
lastElement.addCommaAfter(KtPsiFactory(parent))
}
@@ -138,7 +138,7 @@ private class TrailingCommaPostFormatVisitor(val settings: CodeStyleSettings) :
needComma(parent, settings) -> {
correctCommaPosition(parent)
}
elementType === KtTokens.COMMA -> {
elementType == KtTokens.COMMA -> {
// remove redundant comma
lastElement.delete()
}
@@ -225,4 +225,4 @@ fun PsiElement.leaf(forward: Boolean = true, filter: (PsiElement) -> Boolean): P
if (forward) nextLeaf(filter)
else prevLeaf(filter)
val PsiElement.isComma: Boolean get() = elementType === KtTokens.COMMA
val PsiElement.isComma: Boolean get() = elementType == KtTokens.COMMA