Refactoring: work with ASTNodes in functional style
This commit is contained in:
@@ -17,6 +17,7 @@
|
|||||||
package org.jetbrains.kotlin.psi.psiUtil
|
package org.jetbrains.kotlin.psi.psiUtil
|
||||||
|
|
||||||
import com.intellij.injected.editor.VirtualFileWindow
|
import com.intellij.injected.editor.VirtualFileWindow
|
||||||
|
import com.intellij.lang.ASTNode
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.search.PsiSearchScopeUtil
|
import com.intellij.psi.search.PsiSearchScopeUtil
|
||||||
@@ -389,3 +390,12 @@ fun KtModifierList.hasExpectModifier() = hasModifier(KtTokens.HEADER_KEYWORD) ||
|
|||||||
fun KtModifierListOwner.hasActualModifier() = hasModifier(KtTokens.IMPL_KEYWORD) || hasModifier(KtTokens.ACTUAL_KEYWORD)
|
fun KtModifierListOwner.hasActualModifier() = hasModifier(KtTokens.IMPL_KEYWORD) || hasModifier(KtTokens.ACTUAL_KEYWORD)
|
||||||
fun KtModifierList.hasActualModifier() = hasModifier(KtTokens.IMPL_KEYWORD) || hasModifier(KtTokens.ACTUAL_KEYWORD)
|
fun KtModifierList.hasActualModifier() = hasModifier(KtTokens.IMPL_KEYWORD) || hasModifier(KtTokens.ACTUAL_KEYWORD)
|
||||||
|
|
||||||
|
fun ASTNode.children() = generateSequence(firstChildNode) { node -> node.treeNext }
|
||||||
|
|
||||||
|
fun ASTNode.siblings(forward: Boolean = true): Sequence<ASTNode> {
|
||||||
|
if (forward) {
|
||||||
|
return generateSequence(treeNext) { it.treeNext }
|
||||||
|
} else {
|
||||||
|
return generateSequence(treePrev) { it.treePrev }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -33,7 +33,8 @@ import org.jetbrains.kotlin.kdoc.parser.KDocElementTypes
|
|||||||
import org.jetbrains.kotlin.lexer.KtTokens
|
import org.jetbrains.kotlin.lexer.KtTokens
|
||||||
import org.jetbrains.kotlin.lexer.KtTokens.*
|
import org.jetbrains.kotlin.lexer.KtTokens.*
|
||||||
import org.jetbrains.kotlin.psi.*
|
import org.jetbrains.kotlin.psi.*
|
||||||
import java.util.*
|
import org.jetbrains.kotlin.psi.psiUtil.children
|
||||||
|
import org.jetbrains.kotlin.psi.psiUtil.siblings
|
||||||
|
|
||||||
private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS)
|
private val QUALIFIED_OPERATION = TokenSet.create(DOT, SAFE_ACCESS)
|
||||||
private val KDOC_COMMENT_INDENT = 1
|
private val KDOC_COMMENT_INDENT = 1
|
||||||
@@ -104,8 +105,7 @@ abstract class KotlinCommonBlock(
|
|||||||
nodeSubBlocks.subList(operationBlockIndex, nodeSubBlocks.size),
|
nodeSubBlocks.subList(operationBlockIndex, nodeSubBlocks.size),
|
||||||
null, indent, null, spacingBuilder) { createSyntheticSpacingNodeBlock(it) }
|
null, indent, null, spacingBuilder) { createSyntheticSpacingNodeBlock(it) }
|
||||||
|
|
||||||
nodeSubBlocks = ArrayList<Block>(nodeSubBlocks.subList(0, operationBlockIndex))
|
nodeSubBlocks = nodeSubBlocks.subList(0, operationBlockIndex) + operationSyntheticBlock
|
||||||
nodeSubBlocks.add(operationSyntheticBlock)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -295,31 +295,14 @@ abstract class KotlinCommonBlock(
|
|||||||
return createBlock(child, alignmentStrategy, createChildIndent(child), childWrap, settings, spacingBuilder)
|
return createBlock(child, alignmentStrategy, createChildIndent(child), childWrap, settings, spacingBuilder)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun buildSubBlocks(): ArrayList<Block> {
|
private fun buildSubBlocks(): List<Block> {
|
||||||
val blocks = ArrayList<Block>()
|
|
||||||
|
|
||||||
val childrenAlignmentStrategy = getChildrenAlignmentStrategy()
|
val childrenAlignmentStrategy = getChildrenAlignmentStrategy()
|
||||||
val wrappingStrategy = getWrappingStrategy()
|
val wrappingStrategy = getWrappingStrategy()
|
||||||
|
|
||||||
var child: ASTNode? = node.firstChildNode
|
return node.children()
|
||||||
while (child != null) {
|
.filter { it.textRange.length > 0 && it.elementType != TokenType.WHITE_SPACE }
|
||||||
val childType = child.elementType
|
.map { buildSubBlock(it, childrenAlignmentStrategy, wrappingStrategy ) }
|
||||||
|
.toMutableList()
|
||||||
if (child.textRange.length == 0) {
|
|
||||||
child = child.treeNext
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
if (childType === TokenType.WHITE_SPACE) {
|
|
||||||
child = child.treeNext
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
blocks.add(buildSubBlock(child, childrenAlignmentStrategy, wrappingStrategy))
|
|
||||||
child = child.treeNext
|
|
||||||
}
|
|
||||||
|
|
||||||
return blocks
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getWrappingStrategy(): WrappingStrategy {
|
private fun getWrappingStrategy(): WrappingStrategy {
|
||||||
@@ -605,24 +588,14 @@ private fun getAlignmentForChildInParenthesis(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPrevWithoutWhitespace(pNode: ASTNode?): ASTNode? {
|
private fun getPrevWithoutWhitespace(pNode: ASTNode): ASTNode? {
|
||||||
var node = pNode
|
return pNode.siblings(forward = false).firstOrNull { it.elementType != TokenType.WHITE_SPACE }
|
||||||
node = node!!.treePrev
|
|
||||||
while (node != null && node.elementType === TokenType.WHITE_SPACE) {
|
|
||||||
node = node.treePrev
|
|
||||||
}
|
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getPrevWithoutWhitespaceAndComments(pNode: ASTNode?): ASTNode? {
|
private fun getPrevWithoutWhitespaceAndComments(pNode: ASTNode): ASTNode? {
|
||||||
var node = pNode
|
return pNode.siblings(forward = false).firstOrNull {
|
||||||
node = node!!.treePrev
|
it.elementType != TokenType.WHITE_SPACE && it.elementType !in COMMENTS
|
||||||
while (node != null && (node.elementType === TokenType.WHITE_SPACE || COMMENTS.contains(node.elementType))) {
|
|
||||||
node = node.treePrev
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return node
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun getWrappingStrategyForItemList(wrapType: Int, itemType: IElementType, wrapFirstElement: Boolean = false): WrappingStrategy {
|
private fun getWrappingStrategyForItemList(wrapType: Int, itemType: IElementType, wrapFirstElement: Boolean = false): WrappingStrategy {
|
||||||
|
|||||||
@@ -16,19 +16,13 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.idea.formatter
|
package org.jetbrains.kotlin.idea.formatter
|
||||||
|
|
||||||
import com.intellij.formatting.Block
|
import com.intellij.formatting.*
|
||||||
import com.intellij.formatting.Alignment
|
|
||||||
import com.intellij.formatting.Indent
|
|
||||||
import com.intellij.formatting.Wrap
|
|
||||||
import com.intellij.openapi.util.TextRange
|
|
||||||
import com.intellij.formatting.ChildAttributes
|
|
||||||
import com.intellij.formatting.Spacing
|
|
||||||
import com.intellij.lang.ASTNode
|
import com.intellij.lang.ASTNode
|
||||||
import com.intellij.formatting.ASTBlock
|
import com.intellij.openapi.util.TextRange
|
||||||
|
|
||||||
class SyntheticKotlinBlock(
|
class SyntheticKotlinBlock(
|
||||||
private val node: ASTNode,
|
private val node: ASTNode,
|
||||||
private val subBlocks: MutableList<Block>,
|
private val subBlocks: List<Block>,
|
||||||
private val alignment: Alignment?,
|
private val alignment: Alignment?,
|
||||||
private val indent: Indent?,
|
private val indent: Indent?,
|
||||||
private val wrap: Wrap?,
|
private val wrap: Wrap?,
|
||||||
|
|||||||
Reference in New Issue
Block a user