Move call-site trailing comma to registry
#KT-34744
This commit is contained in:
@@ -19,10 +19,7 @@ import com.intellij.psi.tree.TokenSet
|
||||
import org.jetbrains.kotlin.KtNodeTypes.*
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.formatter.NodeIndentStrategy.Companion.strategy
|
||||
import org.jetbrains.kotlin.idea.util.containsLineBreakInThis
|
||||
import org.jetbrains.kotlin.idea.util.isMultiline
|
||||
import org.jetbrains.kotlin.idea.util.needTrailingComma
|
||||
import org.jetbrains.kotlin.idea.util.requireNode
|
||||
import org.jetbrains.kotlin.idea.util.*
|
||||
import org.jetbrains.kotlin.kdoc.lexer.KDocTokens
|
||||
import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes
|
||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||
@@ -588,7 +585,7 @@ abstract class KotlinCommonBlock(
|
||||
when {
|
||||
elementType === VALUE_ARGUMENT_LIST -> {
|
||||
val wrapSetting = commonSettings.CALL_PARAMETERS_WRAP
|
||||
if (!node.trailingCommaIsAllowed &&
|
||||
if (!node.addTrailingComma &&
|
||||
(wrapSetting == CommonCodeStyleSettings.WRAP_AS_NEEDED || wrapSetting == CommonCodeStyleSettings.WRAP_ON_EVERY_ITEM) &&
|
||||
!needWrapArgumentList(nodePsi)
|
||||
) {
|
||||
@@ -597,7 +594,7 @@ abstract class KotlinCommonBlock(
|
||||
return getWrappingStrategyForItemList(
|
||||
wrapSetting,
|
||||
VALUE_ARGUMENT,
|
||||
node.trailingCommaIsAllowed,
|
||||
node.addTrailingComma,
|
||||
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR),
|
||||
)
|
||||
}
|
||||
@@ -607,7 +604,7 @@ abstract class KotlinCommonBlock(
|
||||
FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> return getWrappingStrategyForItemList(
|
||||
commonSettings.METHOD_PARAMETERS_WRAP,
|
||||
VALUE_PARAMETER,
|
||||
node.trailingCommaIsAllowed,
|
||||
node.addTrailingComma,
|
||||
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR),
|
||||
)
|
||||
FUNCTION_TYPE -> return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
|
||||
@@ -752,8 +749,8 @@ abstract class KotlinCommonBlock(
|
||||
private fun defaultTrailingCommaWrappingStrategy(leftAnchor: IElementType, rightAnchor: IElementType): WrappingStrategy =
|
||||
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
|
||||
|
||||
private val ASTNode.trailingCommaIsAllowed: Boolean
|
||||
get() = (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
|
||||
private val ASTNode.addTrailingComma: Boolean
|
||||
get() = (settings.kotlinCustomSettings.addTrailingCommaIsAllowedFor(this) ||
|
||||
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA) &&
|
||||
psi?.let(PsiElement::isMultiline) == true
|
||||
|
||||
@@ -821,7 +818,7 @@ abstract class KotlinCommonBlock(
|
||||
if (!filter(childElement)) return null
|
||||
val childElementType = childElement.elementType
|
||||
return createWrapAlwaysIf(
|
||||
(!checkTrailingComma || childElement.treeParent.trailingCommaIsAllowed) && (
|
||||
(!checkTrailingComma || childElement.treeParent.addTrailingComma) && (
|
||||
rightAnchor != null && rightAnchor === childElementType ||
|
||||
leftAnchor != null && leftAnchor === getSiblingWithoutWhitespaceAndComments(childElement)?.elementType ||
|
||||
additionalCheck(childElement)
|
||||
|
||||
@@ -6,11 +6,17 @@
|
||||
package org.jetbrains.kotlin.idea.util
|
||||
|
||||
import com.intellij.formatting.ASTBlock
|
||||
import com.intellij.lang.ASTNode
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.openapi.util.UserDataHolder
|
||||
import com.intellij.openapi.util.registry.Registry
|
||||
import com.intellij.openapi.util.text.StringUtil
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import com.intellij.psi.PsiElement
|
||||
import com.intellij.psi.codeStyle.CodeStyleSettings
|
||||
import com.intellij.psi.tree.TokenSet
|
||||
import com.intellij.psi.util.PsiUtilCore
|
||||
import org.jetbrains.kotlin.KtNodeTypes
|
||||
import org.jetbrains.kotlin.idea.core.formatter.KotlinCodeStyleSettings
|
||||
import org.jetbrains.kotlin.idea.formatter.kotlinCustomSettings
|
||||
import org.jetbrains.kotlin.psi.KtDestructuringDeclaration
|
||||
@@ -77,7 +83,9 @@ fun <T : PsiElement> T.needTrailingComma(
|
||||
globalStartOffset: T.() -> Int? = PsiElement::startOffset,
|
||||
globalEndOffset: T.() -> Int? = PsiElement::endOffset,
|
||||
): Boolean {
|
||||
if (trailingComma() == null && !settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA || !additionalCheck()) return false
|
||||
if (trailingComma() == null && !settings.kotlinCustomSettings.addTrailingCommaIsAllowedFor(this)) return false
|
||||
if (!additionalCheck()) return false
|
||||
|
||||
val startOffset = globalStartOffset() ?: return false
|
||||
val endOffset = globalEndOffset() ?: return false
|
||||
return containsLineBreakInThis(startOffset, endOffset)
|
||||
@@ -86,4 +94,35 @@ fun <T : PsiElement> T.needTrailingComma(
|
||||
fun PsiElement.containsLineBreakInThis(globalStartOffset: Int, globalEndOffset: Int): Boolean {
|
||||
val textRange = TextRange.create(globalStartOffset, globalEndOffset).shiftLeft(startOffset)
|
||||
return StringUtil.containsLineBreak(textRange.subSequence(text))
|
||||
}
|
||||
}
|
||||
|
||||
fun trailingCommaIsAllowedOnCallSite(): Boolean = Registry.`is`("kotlin.formatter.allowTrailingCommaOnCallSite")
|
||||
|
||||
private val TYPES_WITH_TRAILING_COMMA = TokenSet.create(
|
||||
KtNodeTypes.TYPE_PARAMETER_LIST,
|
||||
KtNodeTypes.DESTRUCTURING_DECLARATION,
|
||||
KtNodeTypes.WHEN_ENTRY,
|
||||
KtNodeTypes.FUNCTION_LITERAL,
|
||||
KtNodeTypes.VALUE_PARAMETER_LIST,
|
||||
)
|
||||
|
||||
private val TYPES_WITH_TRAILING_COMMA_ON_CALL_SITE = TokenSet.create(
|
||||
KtNodeTypes.COLLECTION_LITERAL_EXPRESSION,
|
||||
KtNodeTypes.TYPE_ARGUMENT_LIST,
|
||||
KtNodeTypes.INDICES,
|
||||
KtNodeTypes.VALUE_ARGUMENT_LIST,
|
||||
)
|
||||
|
||||
fun UserDataHolder.addTrailingCommaIsAllowedForThis(): Boolean {
|
||||
val type = when (this) {
|
||||
is ASTNode -> PsiUtilCore.getElementType(this)
|
||||
is PsiElement -> PsiUtilCore.getElementType(this)
|
||||
else -> return false
|
||||
}
|
||||
|
||||
return type in TYPES_WITH_TRAILING_COMMA || trailingCommaIsAllowedOnCallSite() && type in TYPES_WITH_TRAILING_COMMA_ON_CALL_SITE
|
||||
}
|
||||
|
||||
fun KotlinCodeStyleSettings.addTrailingCommaIsAllowedFor(element: UserDataHolder): Boolean =
|
||||
ALLOW_TRAILING_COMMA && element.addTrailingCommaIsAllowedForThis()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user