KotlinCommonBlock: refactoring
#KT-34744
This commit is contained in:
@@ -588,7 +588,7 @@ 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.withTrailingComma && (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
|
||||||
@@ -596,7 +596,7 @@ abstract class KotlinCommonBlock(
|
|||||||
return getWrappingStrategyForItemList(
|
return getWrappingStrategyForItemList(
|
||||||
wrapSetting,
|
wrapSetting,
|
||||||
VALUE_ARGUMENT,
|
VALUE_ARGUMENT,
|
||||||
node.withTrailingComma,
|
node.trailingCommaIsAllowed,
|
||||||
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR)
|
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -606,7 +606,7 @@ abstract class KotlinCommonBlock(
|
|||||||
FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> return getWrappingStrategyForItemList(
|
FUN, PRIMARY_CONSTRUCTOR, SECONDARY_CONSTRUCTOR -> return getWrappingStrategyForItemList(
|
||||||
commonSettings.METHOD_PARAMETERS_WRAP,
|
commonSettings.METHOD_PARAMETERS_WRAP,
|
||||||
VALUE_PARAMETER,
|
VALUE_PARAMETER,
|
||||||
node.withTrailingComma,
|
node.trailingCommaIsAllowed,
|
||||||
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR)
|
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR)
|
||||||
)
|
)
|
||||||
FUNCTION_TYPE -> return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
|
FUNCTION_TYPE -> return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
|
||||||
@@ -623,21 +623,15 @@ abstract class KotlinCommonBlock(
|
|||||||
|
|
||||||
elementType === FUNCTION_LITERAL -> {
|
elementType === FUNCTION_LITERAL -> {
|
||||||
if (nodePsi.cast<KtFunctionLiteral>().needTrailingComma(settings))
|
if (nodePsi.cast<KtFunctionLiteral>().needTrailingComma(settings))
|
||||||
return { childElement ->
|
return trailingCommaWrappingStrategy(leftAnchor = LBRACE, rightAnchor = ARROW)
|
||||||
createWrapAlwaysIf(childElement.elementType === ARROW || getSiblingWithoutWhitespaceAndComments(childElement)?.elementType === LBRACE)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
elementType === WHEN_ENTRY -> {
|
elementType === WHEN_ENTRY -> {
|
||||||
// with argument
|
// with argument
|
||||||
if (nodePsi.cast<KtWhenEntry>().needTrailingComma(settings)) {
|
if (nodePsi.cast<KtWhenEntry>().needTrailingComma(settings)) {
|
||||||
val check = thisOrPrevIsMultiLineElement(COMMA, LBRACE /* not necessary */, ARROW /* not necessary */)
|
val check = thisOrPrevIsMultiLineElement(COMMA, LBRACE /* not necessary */, ARROW /* not necessary */)
|
||||||
return { childElement ->
|
return trailingCommaWrappingStrategy(rightAnchor = ARROW) {
|
||||||
createWrapAlwaysIf(
|
getSiblingWithoutWhitespaceAndComments(it, true) != null && check(it)
|
||||||
childElement.elementType === ARROW ||
|
|
||||||
getSiblingWithoutWhitespaceAndComments(childElement, true) != null &&
|
|
||||||
check(childElement)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -647,14 +641,8 @@ abstract class KotlinCommonBlock(
|
|||||||
if (nodePsi.valOrVarKeyword == null) return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
|
if (nodePsi.valOrVarKeyword == null) return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
|
||||||
else if (nodePsi.needTrailingComma(settings)) {
|
else if (nodePsi.needTrailingComma(settings)) {
|
||||||
val check = thisOrPrevIsMultiLineElement(COMMA, LPAR, RPAR)
|
val check = thisOrPrevIsMultiLineElement(COMMA, LPAR, RPAR)
|
||||||
return block@{ childElement ->
|
return trailingCommaWrappingStrategy(leftAnchor = LPAR, rightAnchor = RPAR, filter = { it.elementType !== EQ }) {
|
||||||
val childElementType = childElement.elementType
|
getSiblingWithoutWhitespaceAndComments(it, true) != null && check(it)
|
||||||
if (childElementType === EQ) return@block null
|
|
||||||
createWrapAlwaysIf(
|
|
||||||
childElementType === RPAR ||
|
|
||||||
getSiblingWithoutWhitespaceAndComments(childElement)?.elementType === LPAR ||
|
|
||||||
getSiblingWithoutWhitespaceAndComments(childElement, true) != null && check(childElement)
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -763,7 +751,7 @@ abstract class KotlinCommonBlock(
|
|||||||
private fun defaultTrailingCommaWrappingStrategy(leftAnchor: IElementType, rightAnchor: IElementType): WrappingStrategy =
|
private fun defaultTrailingCommaWrappingStrategy(leftAnchor: IElementType, rightAnchor: IElementType): WrappingStrategy =
|
||||||
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
|
fun(childElement: ASTNode): Wrap? = trailingCommaWrappingStrategyWithMultiLineCheck(leftAnchor, rightAnchor)(childElement)
|
||||||
|
|
||||||
private val ASTNode.withTrailingComma: Boolean
|
private val ASTNode.trailingCommaIsAllowed: Boolean
|
||||||
get() = if (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
|
get() = if (settings.kotlinCustomSettings.ALLOW_TRAILING_COMMA ||
|
||||||
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA
|
lastChildNode?.let { getSiblingWithoutWhitespaceAndComments(it) }?.elementType === COMMA
|
||||||
)
|
)
|
||||||
@@ -774,10 +762,10 @@ abstract class KotlinCommonBlock(
|
|||||||
private fun ASTNode.notDelimiterSiblingNodeInSequence(
|
private fun ASTNode.notDelimiterSiblingNodeInSequence(
|
||||||
forward: Boolean,
|
forward: Boolean,
|
||||||
delimiterType: IElementType,
|
delimiterType: IElementType,
|
||||||
barrier: IElementType
|
typeOfLastElement: IElementType
|
||||||
): ASTNode? {
|
): ASTNode? {
|
||||||
var sibling: ASTNode? = null
|
var sibling: ASTNode? = null
|
||||||
for (element in siblings(forward).filter { it.elementType != WHITE_SPACE }.takeWhile { it.elementType != barrier }) {
|
for (element in siblings(forward).filter { it.elementType != WHITE_SPACE }.takeWhile { it.elementType != typeOfLastElement }) {
|
||||||
val elementType = element.elementType
|
val elementType = element.elementType
|
||||||
if (!forward) {
|
if (!forward) {
|
||||||
sibling = element
|
sibling = element
|
||||||
@@ -793,12 +781,12 @@ abstract class KotlinCommonBlock(
|
|||||||
|
|
||||||
private fun thisOrPrevIsMultiLineElement(
|
private fun thisOrPrevIsMultiLineElement(
|
||||||
delimiterType: IElementType,
|
delimiterType: IElementType,
|
||||||
leftBarrier: IElementType,
|
typeOfFirstElement: IElementType,
|
||||||
rightBarrier: IElementType
|
typeOfLastElement: IElementType
|
||||||
) = fun(childElement: ASTNode): Boolean {
|
) = fun(childElement: ASTNode): Boolean {
|
||||||
when (childElement.elementType) {
|
when (childElement.elementType) {
|
||||||
leftBarrier,
|
typeOfFirstElement,
|
||||||
rightBarrier,
|
typeOfLastElement,
|
||||||
delimiterType,
|
delimiterType,
|
||||||
in WHITE_SPACE_OR_COMMENT_BIT_SET
|
in WHITE_SPACE_OR_COMMENT_BIT_SET
|
||||||
-> return false
|
-> return false
|
||||||
@@ -807,26 +795,37 @@ abstract class KotlinCommonBlock(
|
|||||||
val psi = childElement.psi ?: return false
|
val psi = childElement.psi ?: return false
|
||||||
if (psi.isMultiline()) return true
|
if (psi.isMultiline()) return true
|
||||||
|
|
||||||
val startOffset = childElement.notDelimiterSiblingNodeInSequence(false, delimiterType, leftBarrier)?.startOffset ?: psi.startOffset
|
val startOffset = childElement.notDelimiterSiblingNodeInSequence(false, delimiterType, typeOfFirstElement)?.startOffset
|
||||||
val endOffset = childElement.notDelimiterSiblingNodeInSequence(true, delimiterType, rightBarrier)?.psi?.endOffset ?: psi.endOffset
|
?: psi.startOffset
|
||||||
|
val endOffset = childElement.notDelimiterSiblingNodeInSequence(true, delimiterType, typeOfLastElement)?.psi?.endOffset
|
||||||
|
?: psi.endOffset
|
||||||
return psi.parent.containsLineBreakInThis(startOffset, endOffset)
|
return psi.parent.containsLineBreakInThis(startOffset, endOffset)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun trailingCommaWrappingStrategyWithMultiLineCheck(
|
private fun trailingCommaWrappingStrategyWithMultiLineCheck(
|
||||||
leftAnchor: IElementType,
|
leftAnchor: IElementType,
|
||||||
rightAnchor: IElementType
|
rightAnchor: IElementType
|
||||||
) = trailingCommaWrappingStrategy(leftAnchor, rightAnchor, thisOrPrevIsMultiLineElement(COMMA, leftAnchor, rightAnchor))
|
) = trailingCommaWrappingStrategy(
|
||||||
|
leftAnchor = leftAnchor,
|
||||||
|
rightAnchor = rightAnchor,
|
||||||
|
checkTrailingComma = true,
|
||||||
|
additionalCheck = thisOrPrevIsMultiLineElement(COMMA, leftAnchor, rightAnchor)
|
||||||
|
)
|
||||||
|
|
||||||
private fun trailingCommaWrappingStrategy(
|
private fun trailingCommaWrappingStrategy(
|
||||||
leftAnchor: IElementType,
|
leftAnchor: IElementType? = null,
|
||||||
rightAnchor: IElementType,
|
rightAnchor: IElementType? = null,
|
||||||
additionalCheck: (ASTNode) -> Boolean
|
checkTrailingComma: Boolean = false,
|
||||||
): WrappingStrategy = { childElement ->
|
filter: (ASTNode) -> Boolean = { true },
|
||||||
|
additionalCheck: (ASTNode) -> Boolean = { false }
|
||||||
|
): WrappingStrategy = fun(childElement: ASTNode): Wrap? {
|
||||||
|
if (!filter(childElement)) return null
|
||||||
val childElementType = childElement.elementType
|
val childElementType = childElement.elementType
|
||||||
createWrapAlwaysIf(
|
return createWrapAlwaysIf(
|
||||||
childElement.treeParent.withTrailingComma && (childElementType === rightAnchor ||
|
(!checkTrailingComma || childElement.treeParent.trailingCommaIsAllowed) && (
|
||||||
getSiblingWithoutWhitespaceAndComments(childElement)?.elementType === leftAnchor ||
|
rightAnchor != null && rightAnchor === childElementType ||
|
||||||
additionalCheck(childElement)
|
leftAnchor != null && leftAnchor === getSiblingWithoutWhitespaceAndComments(childElement)?.elementType ||
|
||||||
|
additionalCheck(childElement)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user