All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.jetbrains.kotlin.fir.analysis.FirSourceUtils.kt Maven / Gradle / Ivy
/*
* Copyright 2010-2021 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.
*/
package org.jetbrains.kotlin.fir.analysis
import com.intellij.lang.LighterASTNode
import com.intellij.psi.tree.IElementType
import com.intellij.psi.tree.TokenSet
import org.jetbrains.kotlin.*
fun KtSourceElement.getChild(type: IElementType, index: Int = 0, depth: Int = -1, reverse: Boolean = false): KtSourceElement? {
return getChild(setOf(type), index, depth, reverse)
}
fun KtSourceElement.getChild(types: TokenSet, index: Int = 0, depth: Int = -1, reverse: Boolean = false): KtSourceElement? {
return getChild(types.types.toSet(), index, depth, reverse)
}
fun KtSourceElement.getChild(types: Set, index: Int = 0, depth: Int = -1, reverse: Boolean = false): KtSourceElement? {
return when (this) {
is KtPsiSourceElement -> {
getChild(types, index, depth, reverse)
}
is KtLightSourceElement -> {
getChild(types, index, depth, reverse)
}
else -> null
}
}
private fun KtPsiSourceElement.getChild(types: Set, index: Int, depth: Int, reverse: Boolean): KtSourceElement? {
val visitor = PsiElementFinderByType(types, index, depth, reverse)
return visitor.find(psi)?.toKtPsiSourceElement()
}
private fun KtLightSourceElement.getChild(types: Set, index: Int, depth: Int, reverse: Boolean): KtSourceElement? {
val visitor = LighterTreeElementFinderByType(treeStructure, types, index, depth, reverse)
val childNode = visitor.find(lighterASTNode) ?: return null
return buildChildSourceElement(childNode)
}
/**
* Keeps 'padding' of parent node in child node
*/
internal fun KtLightSourceElement.buildChildSourceElement(childNode: LighterASTNode): KtLightSourceElement {
val offsetDelta = startOffset - lighterASTNode.startOffset
return childNode.toKtLightSourceElement(
treeStructure,
startOffset = childNode.startOffset + offsetDelta,
endOffset = childNode.endOffset + offsetDelta
)
}