diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt index 7efcb927745..0f9512458aa 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/KotlinCommonBlock.kt @@ -58,14 +58,14 @@ abstract class KotlinCommonBlock( private val settings: CodeStyleSettings, private val spacingBuilder: KotlinSpacingBuilder, private val alignmentStrategy: CommonAlignmentStrategy) { - private @Volatile var mySubBlocks: List? = null + private @Volatile var mySubBlocks: List? = null abstract protected fun createBlock(node: ASTNode, alignmentStrategy: CommonAlignmentStrategy, indent: Indent?, wrap: Wrap?, settings: CodeStyleSettings, - spacingBuilder: KotlinSpacingBuilder): Block + spacingBuilder: KotlinSpacingBuilder): ASTBlock abstract protected fun createSyntheticSpacingNodeBlock(node: ASTNode): ASTBlock @@ -104,7 +104,7 @@ abstract class KotlinCommonBlock( else Indent.getNormalIndent() val operationSyntheticBlock = SyntheticKotlinBlock( - (operationBlock as ASTBlock).node, + operationBlock.node, nodeSubBlocks.subList(operationBlockIndex, nodeSubBlocks.size), null, indent, null, spacingBuilder) { createSyntheticSpacingNodeBlock(it) } @@ -172,7 +172,7 @@ abstract class KotlinCommonBlock( if (type == KtNodeTypes.IF) { val elseBlock = mySubBlocks?.getOrNull(newChildIndex) - if (elseBlock is ASTBlock && elseBlock.node.elementType == KtTokens.ELSE_KEYWORD) { + if (elseBlock != null && elseBlock.node.elementType == KtTokens.ELSE_KEYWORD) { return ChildAttributes.DELEGATE_TO_NEXT_CHILD } } @@ -278,7 +278,7 @@ abstract class KotlinCommonBlock( } - private fun buildSubBlock(child: ASTNode, alignmentStrategy: CommonAlignmentStrategy, wrappingStrategy: WrappingStrategy): Block { + private fun buildSubBlock(child: ASTNode, alignmentStrategy: CommonAlignmentStrategy, wrappingStrategy: WrappingStrategy): ASTBlock { val childWrap = wrappingStrategy.getWrap(child) // Skip one sub-level for operators, so type of block node is an element type of operator @@ -298,14 +298,14 @@ abstract class KotlinCommonBlock( return createBlock(child, alignmentStrategy, createChildIndent(child), childWrap, settings, spacingBuilder) } - private fun buildSubBlocks(): List { + private fun buildSubBlocks(): List { val childrenAlignmentStrategy = getChildrenAlignmentStrategy() val wrappingStrategy = getWrappingStrategy() return node.children() .filter { it.textRange.length > 0 && it.elementType != TokenType.WHITE_SPACE } .map { buildSubBlock(it, childrenAlignmentStrategy, wrappingStrategy ) } - .toMutableList() + .toList() } private fun getWrappingStrategy(): WrappingStrategy { @@ -619,11 +619,6 @@ private fun getWrappingStrategyForItemList(wrapType: Int, itemTypes: TokenSet, w } } -private fun findNodeBlockIndex(blocks: List, tokenSet: TokenSet): Int { - return blocks.indexOfFirst { block -> - if (block !is ASTBlock) return@indexOfFirst false - - val node = block.node - node != null && node.elementType in tokenSet - } -} \ No newline at end of file +private fun findNodeBlockIndex(blocks: List, tokenSet: TokenSet): Int { + return blocks.indexOfFirst { block -> block.node?.elementType in tokenSet } +} diff --git a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt index 6aad10489b3..1c056dd53ef 100644 --- a/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt +++ b/idea/formatter/src/org/jetbrains/kotlin/idea/formatter/SyntheticKotlinBlock.kt @@ -22,7 +22,7 @@ import com.intellij.openapi.util.TextRange class SyntheticKotlinBlock( private val node: ASTNode, - private val subBlocks: List, + private val subBlocks: List, private val alignment: Alignment?, private val indent: Indent?, private val wrap: Wrap?, @@ -56,9 +56,7 @@ class SyntheticKotlinBlock( while (treeNode == null) when (child) { is SyntheticKotlinBlock -> child = child.getSubBlocks().first() - is ASTBlock -> treeNode = child.node - - else -> break@loop + else -> treeNode = child.node } val textRange = getTextRange() diff --git a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinBlock.kt b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinBlock.kt index 063119979a5..93cbe682e71 100644 --- a/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinBlock.kt +++ b/idea/src/org/jetbrains/kotlin/idea/formatter/KotlinBlock.kt @@ -24,8 +24,8 @@ import com.intellij.psi.codeStyle.CodeStyleSettings import com.intellij.psi.formatter.FormatterUtil import com.intellij.psi.formatter.common.AbstractBlock import com.intellij.psi.tree.IElementType -import org.jetbrains.kotlin.KtNodeTypes.* -import org.jetbrains.kotlin.lexer.KtTokens.* +import org.jetbrains.kotlin.KtNodeTypes.WHEN_ENTRY +import org.jetbrains.kotlin.lexer.KtTokens.ARROW /** * @see Block for good JavaDoc documentation @@ -63,7 +63,7 @@ class KotlinBlock( override fun getSubBlocks(): List = subBlocks - override fun createBlock(node: ASTNode, alignmentStrategy: CommonAlignmentStrategy, indent: Indent?, wrap: Wrap?, settings: CodeStyleSettings, spacingBuilder: KotlinSpacingBuilder): Block { + override fun createBlock(node: ASTNode, alignmentStrategy: CommonAlignmentStrategy, indent: Indent?, wrap: Wrap?, settings: CodeStyleSettings, spacingBuilder: KotlinSpacingBuilder): ASTBlock { return KotlinBlock( node, alignmentStrategy,