Refactoring: use more specific block type in signatures

This commit is contained in:
Dmitry Jemerov
2017-11-30 14:38:31 +01:00
parent 3233d650c1
commit 1a93d10697
3 changed files with 15 additions and 22 deletions
@@ -58,14 +58,14 @@ abstract class KotlinCommonBlock(
private val settings: CodeStyleSettings,
private val spacingBuilder: KotlinSpacingBuilder,
private val alignmentStrategy: CommonAlignmentStrategy) {
private @Volatile var mySubBlocks: List<Block>? = null
private @Volatile var mySubBlocks: List<ASTBlock>? = 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<Block> {
private fun buildSubBlocks(): List<ASTBlock> {
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<Block>, tokenSet: TokenSet): Int {
return blocks.indexOfFirst { block ->
if (block !is ASTBlock) return@indexOfFirst false
val node = block.node
node != null && node.elementType in tokenSet
}
}
private fun findNodeBlockIndex(blocks: List<ASTBlock>, tokenSet: TokenSet): Int {
return blocks.indexOfFirst { block -> block.node?.elementType in tokenSet }
}
@@ -22,7 +22,7 @@ import com.intellij.openapi.util.TextRange
class SyntheticKotlinBlock(
private val node: ASTNode,
private val subBlocks: List<Block>,
private val subBlocks: List<ASTBlock>,
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()
@@ -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<Block> = 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,