KotlinCommonBlock: cleanup code

This commit is contained in:
Dmitry Gridin
2020-01-23 01:06:49 +07:00
parent 63ee723928
commit b045513642
@@ -1,5 +1,5 @@
/* /*
* Copyright 2000-2019 JetBrains s.r.o. and Kotlin Programming Language contributors. * Copyright 2000-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/ */
@@ -55,7 +55,7 @@ abstract class KotlinCommonBlock(
private val settings: CodeStyleSettings, private val settings: CodeStyleSettings,
private val spacingBuilder: KotlinSpacingBuilder, private val spacingBuilder: KotlinSpacingBuilder,
private val alignmentStrategy: CommonAlignmentStrategy, private val alignmentStrategy: CommonAlignmentStrategy,
private val overrideChildren: Sequence<ASTNode>? = null private val overrideChildren: Sequence<ASTNode>? = null,
) { ) {
@Volatile @Volatile
private var mySubBlocks: List<ASTBlock>? = null private var mySubBlocks: List<ASTBlock>? = null
@@ -74,7 +74,7 @@ abstract class KotlinCommonBlock(
wrap: Wrap?, wrap: Wrap?,
settings: CodeStyleSettings, settings: CodeStyleSettings,
spacingBuilder: KotlinSpacingBuilder, spacingBuilder: KotlinSpacingBuilder,
overrideChildren: Sequence<ASTNode>? = null overrideChildren: Sequence<ASTNode>? = null,
): ASTBlock ): ASTBlock
protected abstract fun createSyntheticSpacingNodeBlock(node: ASTNode): ASTBlock protected abstract fun createSyntheticSpacingNodeBlock(node: ASTNode): ASTBlock
@@ -195,7 +195,7 @@ abstract class KotlinCommonBlock(
private fun createWrapForQualifiedExpression(node: ASTNode): Wrap? = if (node.wrapForFirstCallInChainIsAllowed) private fun createWrapForQualifiedExpression(node: ASTNode): Wrap? = if (node.wrapForFirstCallInChainIsAllowed)
Wrap.createWrap( Wrap.createWrap(
settings.kotlinCommonSettings.METHOD_CALL_CHAIN_WRAP, settings.kotlinCommonSettings.METHOD_CALL_CHAIN_WRAP,
settings.kotlinCommonSettings.WRAP_FIRST_METHOD_IN_CALL_CHAIN settings.kotlinCommonSettings.WRAP_FIRST_METHOD_IN_CALL_CHAIN,
) )
else else
null null
@@ -213,13 +213,13 @@ abstract class KotlinCommonBlock(
return Indent.getIndent( return Indent.getIndent(
indentType, false, indentType, false,
enforceIndentToChildren enforceIndentToChildren,
) )
} }
private fun List<ASTBlock>.wrapToBlock( private fun List<ASTBlock>.wrapToBlock(
anchor: ASTNode?, anchor: ASTNode?,
parentBlock: ASTBlock? parentBlock: ASTBlock?,
): ASTBlock = splitAtIndex(0, null, null, anchor, parentBlock).single() ): ASTBlock = splitAtIndex(0, null, null, anchor, parentBlock).single()
private fun List<ASTBlock>.splitAtIndex( private fun List<ASTBlock>.splitAtIndex(
@@ -227,7 +227,7 @@ abstract class KotlinCommonBlock(
indent: Indent?, indent: Indent?,
wrap: Wrap?, wrap: Wrap?,
anchor: ASTNode? = null, anchor: ASTNode? = null,
parentBlock: ASTBlock? = null parentBlock: ASTBlock? = null,
): List<ASTBlock> { ): List<ASTBlock> {
val operationBlock = this[index] val operationBlock = this[index]
val createParentSyntheticSpacingBlock: (ASTNode) -> ASTBlock = if (parentBlock != null) { val createParentSyntheticSpacingBlock: (ASTNode) -> ASTBlock = if (parentBlock != null) {
@@ -246,7 +246,7 @@ abstract class KotlinCommonBlock(
val operationSyntheticBlock = SyntheticKotlinBlock( val operationSyntheticBlock = SyntheticKotlinBlock(
anchor ?: operationBlock.requireNode(), anchor ?: operationBlock.requireNode(),
subList(index, size), subList(index, size),
null, indent, wrap, spacingBuilder, createParentSyntheticSpacingBlock null, indent, wrap, spacingBuilder, createParentSyntheticSpacingBlock,
) )
return subList(0, index) + operationSyntheticBlock return subList(0, index) + operationSyntheticBlock
@@ -287,7 +287,7 @@ abstract class KotlinCommonBlock(
return nodeSubBlocks.splitAtIndex( return nodeSubBlocks.splitAtIndex(
elvisIndex, elvisIndex,
indent, indent,
null null,
) )
} }
return nodeSubBlocks return nodeSubBlocks
@@ -378,7 +378,7 @@ abstract class KotlinCommonBlock(
return when (type) { return when (type) {
in CODE_BLOCKS, WHEN, IF, FOR, WHILE, DO_WHILE, WHEN_ENTRY -> ChildAttributes( in CODE_BLOCKS, WHEN, IF, FOR, WHILE, DO_WHILE, WHEN_ENTRY -> ChildAttributes(
Indent.getNormalIndent(), Indent.getNormalIndent(),
null null,
) )
TRY -> ChildAttributes(Indent.getNoneIndent(), null) TRY -> ChildAttributes(Indent.getNoneIndent(), null)
@@ -441,13 +441,13 @@ abstract class KotlinCommonBlock(
parentType === VALUE_PARAMETER_LIST -> parentType === VALUE_PARAMETER_LIST ->
getAlignmentForChildInParenthesis( getAlignmentForChildInParenthesis(
kotlinCommonSettings.ALIGN_MULTILINE_PARAMETERS, VALUE_PARAMETER, COMMA, kotlinCommonSettings.ALIGN_MULTILINE_PARAMETERS, VALUE_PARAMETER, COMMA,
kotlinCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR kotlinCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR,
) )
parentType === VALUE_ARGUMENT_LIST -> parentType === VALUE_ARGUMENT_LIST ->
getAlignmentForChildInParenthesis( getAlignmentForChildInParenthesis(
kotlinCommonSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS, VALUE_ARGUMENT, COMMA, kotlinCommonSettings.ALIGN_MULTILINE_PARAMETERS_IN_CALLS, VALUE_ARGUMENT, COMMA,
kotlinCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR kotlinCommonSettings.ALIGN_MULTILINE_METHOD_BRACKETS, LPAR, RPAR,
) )
parentType === WHEN -> parentType === WHEN ->
@@ -496,7 +496,7 @@ abstract class KotlinCommonBlock(
child: ASTNode, child: ASTNode,
alignmentStrategy: CommonAlignmentStrategy, alignmentStrategy: CommonAlignmentStrategy,
wrappingStrategy: WrappingStrategy, wrappingStrategy: WrappingStrategy,
overrideChildren: Sequence<ASTNode>? = null overrideChildren: Sequence<ASTNode>? = null,
): ASTBlock { ): ASTBlock {
val childWrap = wrappingStrategy(child) val childWrap = wrappingStrategy(child)
@@ -511,7 +511,7 @@ abstract class KotlinCommonBlock(
childWrap, childWrap,
settings, settings,
spacingBuilder, spacingBuilder,
overrideChildren overrideChildren,
) )
} }
} }
@@ -547,7 +547,7 @@ abstract class KotlinCommonBlock(
private fun buildSubBlocksForChildNode( private fun buildSubBlocksForChildNode(
node: ASTNode, node: ASTNode,
childrenAlignmentStrategy: CommonAlignmentStrategy, childrenAlignmentStrategy: CommonAlignmentStrategy,
wrappingStrategy: WrappingStrategy wrappingStrategy: WrappingStrategy,
): Sequence<ASTBlock> { ): Sequence<ASTBlock> {
if (node.elementType == FUN && false /* TODO fix tests and restore */) { if (node.elementType == FUN && false /* TODO fix tests and restore */) {
val filteredChildren = node.children().filter { val filteredChildren = node.children().filter {
@@ -597,7 +597,7 @@ abstract class KotlinCommonBlock(
wrapSetting, wrapSetting,
VALUE_ARGUMENT, VALUE_ARGUMENT,
node.trailingCommaIsAllowed, node.trailingCommaIsAllowed,
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR) additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR),
) )
} }
@@ -607,7 +607,7 @@ abstract class KotlinCommonBlock(
commonSettings.METHOD_PARAMETERS_WRAP, commonSettings.METHOD_PARAMETERS_WRAP,
VALUE_PARAMETER, VALUE_PARAMETER,
node.trailingCommaIsAllowed, node.trailingCommaIsAllowed,
additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR) additionalWrap = trailingCommaWrappingStrategyWithMultiLineCheck(LPAR, RPAR),
) )
FUNCTION_TYPE -> return defaultTrailingCommaWrappingStrategy(LPAR, RPAR) FUNCTION_TYPE -> return defaultTrailingCommaWrappingStrategy(LPAR, RPAR)
FUNCTION_LITERAL -> { FUNCTION_LITERAL -> {
@@ -668,18 +668,18 @@ abstract class KotlinCommonBlock(
return getWrappingStrategyForItemList( return getWrappingStrategyForItemList(
commonSettings.PARAMETER_ANNOTATION_WRAP, commonSettings.PARAMETER_ANNOTATION_WRAP,
ANNOTATIONS, ANNOTATIONS,
!node.treeParent.isFirstParameter() !node.treeParent.isFirstParameter(),
) )
is KtClassOrObject, is KtTypeAlias -> is KtClassOrObject, is KtTypeAlias ->
return getWrappingStrategyForItemList( return getWrappingStrategyForItemList(
commonSettings.CLASS_ANNOTATION_WRAP, commonSettings.CLASS_ANNOTATION_WRAP,
ANNOTATIONS ANNOTATIONS,
) )
is KtNamedFunction, is KtSecondaryConstructor -> is KtNamedFunction, is KtSecondaryConstructor ->
return getWrappingStrategyForItemList( return getWrappingStrategyForItemList(
commonSettings.METHOD_ANNOTATION_WRAP, commonSettings.METHOD_ANNOTATION_WRAP,
ANNOTATIONS ANNOTATIONS,
) )
is KtProperty -> is KtProperty ->
@@ -688,7 +688,7 @@ abstract class KotlinCommonBlock(
commonSettings.VARIABLE_ANNOTATION_WRAP commonSettings.VARIABLE_ANNOTATION_WRAP
else else
commonSettings.FIELD_ANNOTATION_WRAP, commonSettings.FIELD_ANNOTATION_WRAP,
ANNOTATIONS ANNOTATIONS,
) )
} }
} }
@@ -762,7 +762,7 @@ abstract class KotlinCommonBlock(
private fun ASTNode.notDelimiterSiblingNodeInSequence( private fun ASTNode.notDelimiterSiblingNodeInSequence(
forward: Boolean, forward: Boolean,
delimiterType: IElementType, delimiterType: IElementType,
typeOfLastElement: 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 != typeOfLastElement }) { for (element in siblings(forward).filter { it.elementType != WHITE_SPACE }.takeWhile { it.elementType != typeOfLastElement }) {
@@ -782,13 +782,13 @@ abstract class KotlinCommonBlock(
private fun thisOrPrevIsMultiLineElement( private fun thisOrPrevIsMultiLineElement(
delimiterType: IElementType, delimiterType: IElementType,
typeOfFirstElement: IElementType, typeOfFirstElement: IElementType,
typeOfLastElement: IElementType typeOfLastElement: IElementType,
) = fun(childElement: ASTNode): Boolean { ) = fun(childElement: ASTNode): Boolean {
when (childElement.elementType) { when (childElement.elementType) {
typeOfFirstElement, typeOfFirstElement,
typeOfLastElement, typeOfLastElement,
delimiterType, delimiterType,
in WHITE_SPACE_OR_COMMENT_BIT_SET in WHITE_SPACE_OR_COMMENT_BIT_SET,
-> return false -> return false
} }
@@ -804,12 +804,12 @@ abstract class KotlinCommonBlock(
private fun trailingCommaWrappingStrategyWithMultiLineCheck( private fun trailingCommaWrappingStrategyWithMultiLineCheck(
leftAnchor: IElementType, leftAnchor: IElementType,
rightAnchor: IElementType rightAnchor: IElementType,
) = trailingCommaWrappingStrategy( ) = trailingCommaWrappingStrategy(
leftAnchor = leftAnchor, leftAnchor = leftAnchor,
rightAnchor = rightAnchor, rightAnchor = rightAnchor,
checkTrailingComma = true, checkTrailingComma = true,
additionalCheck = thisOrPrevIsMultiLineElement(COMMA, leftAnchor, rightAnchor) additionalCheck = thisOrPrevIsMultiLineElement(COMMA, leftAnchor, rightAnchor),
) )
private fun trailingCommaWrappingStrategy( private fun trailingCommaWrappingStrategy(
@@ -817,7 +817,7 @@ abstract class KotlinCommonBlock(
rightAnchor: IElementType? = null, rightAnchor: IElementType? = null,
checkTrailingComma: Boolean = false, checkTrailingComma: Boolean = false,
filter: (ASTNode) -> Boolean = { true }, filter: (ASTNode) -> Boolean = { true },
additionalCheck: (ASTNode) -> Boolean = { false } additionalCheck: (ASTNode) -> Boolean = { false },
): WrappingStrategy = fun(childElement: ASTNode): Wrap? { ): WrappingStrategy = fun(childElement: ASTNode): Wrap? {
if (!filter(childElement)) return null if (!filter(childElement)) return null
val childElementType = childElement.elementType val childElementType = childElement.elementType
@@ -826,7 +826,7 @@ abstract class KotlinCommonBlock(
rightAnchor != null && rightAnchor === childElementType || rightAnchor != null && rightAnchor === childElementType ||
leftAnchor != null && leftAnchor === getSiblingWithoutWhitespaceAndComments(childElement)?.elementType || leftAnchor != null && leftAnchor === getSiblingWithoutWhitespaceAndComments(childElement)?.elementType ||
additionalCheck(childElement) additionalCheck(childElement)
) ),
) )
} }
} }
@@ -871,7 +871,7 @@ private fun hasDoubleLineBreakBefore(node: ASTNode): Boolean {
fun NodeIndentStrategy.PositionStrategy.continuationIf( fun NodeIndentStrategy.PositionStrategy.continuationIf(
option: (KotlinCodeStyleSettings) -> Boolean, option: (KotlinCodeStyleSettings) -> Boolean,
indentFirst: Boolean = false indentFirst: Boolean = false,
): NodeIndentStrategy { ): NodeIndentStrategy {
return set { settings -> return set { settings ->
if (option(settings.kotlinCustomSettings)) { if (option(settings.kotlinCustomSettings)) {
@@ -981,7 +981,7 @@ private val INDENT_RULES = arrayOf(
.within(PROPERTY, FUN, DESTRUCTURING_DECLARATION, SECONDARY_CONSTRUCTOR) .within(PROPERTY, FUN, DESTRUCTURING_DECLARATION, SECONDARY_CONSTRUCTOR)
.notForType( .notForType(
BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD, CONSTRUCTOR_KEYWORD, RPAR, BLOCK, FUN_KEYWORD, VAL_KEYWORD, VAR_KEYWORD, CONSTRUCTOR_KEYWORD, RPAR,
EOL_COMMENT EOL_COMMENT,
) )
.set(Indent.getContinuationWithoutFirstIndent()), .set(Indent.getContinuationWithoutFirstIndent()),
@@ -1046,7 +1046,7 @@ private val INDENT_RULES = arrayOf(
WHEN_CONDITION_IN_RANGE, WHEN_CONDITION_IN_RANGE,
WHEN_CONDITION_IS_PATTERN, WHEN_CONDITION_IS_PATTERN,
ELSE_KEYWORD, ELSE_KEYWORD,
ARROW ARROW,
) )
.set(Indent.getNormalIndent()), .set(Indent.getNormalIndent()),
@@ -1069,14 +1069,14 @@ private val INDENT_RULES = arrayOf(
.within(TYPEALIAS) .within(TYPEALIAS)
.notForType( .notForType(
TYPE_ALIAS_KEYWORD, EOL_COMMENT, MODIFIER_LIST, BLOCK_COMMENT, TYPE_ALIAS_KEYWORD, EOL_COMMENT, MODIFIER_LIST, BLOCK_COMMENT,
DOC_COMMENT DOC_COMMENT,
) )
.set(Indent.getContinuationIndent()), .set(Indent.getContinuationIndent()),
strategy("Default parameter values") strategy("Default parameter values")
.within(VALUE_PARAMETER) .within(VALUE_PARAMETER)
.forElement { node -> node.psi != null && node.psi == (node.psi.parent as? KtParameter)?.defaultValue } .forElement { node -> node.psi != null && node.psi == (node.psi.parent as? KtParameter)?.defaultValue }
.continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true) .continuationIf(KotlinCodeStyleSettings::CONTINUATION_INDENT_FOR_EXPRESSION_BODIES, indentFirst = true),
) )
@@ -1104,7 +1104,7 @@ private fun ASTNode.suppressBinaryExpressionIndent(): Boolean {
private fun getAlignmentForChildInParenthesis( private fun getAlignmentForChildInParenthesis(
shouldAlignChild: Boolean, parameter: IElementType, delimiter: IElementType, shouldAlignChild: Boolean, parameter: IElementType, delimiter: IElementType,
shouldAlignParenthesis: Boolean, openBracket: IElementType, closeBracket: IElementType shouldAlignParenthesis: Boolean, openBracket: IElementType, closeBracket: IElementType,
): CommonAlignmentStrategy { ): CommonAlignmentStrategy {
val parameterAlignment = if (shouldAlignChild) Alignment.createAlignment() else null val parameterAlignment = if (shouldAlignChild) Alignment.createAlignment() else null
val bracketsAlignment = if (shouldAlignParenthesis) Alignment.createAlignment() else null val bracketsAlignment = if (shouldAlignParenthesis) Alignment.createAlignment() else null
@@ -1148,7 +1148,7 @@ private fun getWrappingStrategyForItemList(
wrapType: Int, wrapType: Int,
itemType: IElementType, itemType: IElementType,
wrapFirstElement: Boolean = false, wrapFirstElement: Boolean = false,
additionalWrap: WrappingStrategy? = null additionalWrap: WrappingStrategy? = null,
): WrappingStrategy { ): WrappingStrategy {
val itemWrap = Wrap.createWrap(wrapType, wrapFirstElement) val itemWrap = Wrap.createWrap(wrapType, wrapFirstElement)
return { childElement -> return { childElement ->