All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lightningkite.khrysalis.util.KotlinPsiExtensions.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.khrysalis.util

import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.com.intellij.psi.PsiWhiteSpace
import org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.LeafPsiElement
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.lexer.KtTokens
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi.KtExpression
import org.jetbrains.kotlin.psi.KtQualifiedExpression
import org.jetbrains.kotlin.psi.psiUtil.allChildren
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassOrAny
import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces
import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.supertypes

fun ClassDescriptor.recursiveChildren(filter: DescriptorKindFilter = DescriptorKindFilter.ALL): Sequence =
    this.unsubstitutedMemberScope.getContributedDescriptors(filter) { true }.asSequence() +
            (this.getSuperClassOrAny().takeUnless { it == this }?.recursiveChildren(filter) ?: sequenceOf()) +
            this.getSuperInterfaces().asSequence().flatMap { it.recursiveChildren(filter) }

fun PropertyDescriptor.allOverridden(): Sequence =
    this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }
fun FunctionDescriptor.allOverridden(): Sequence =
    this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }
fun CallableMemberDescriptor.allOverridden(): Sequence =
    this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }

fun PropertyDescriptor.allSuperVersions(): Sequence =
    sequenceOf(this) + this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }
fun FunctionDescriptor.allSuperVersions(): Sequence =
    sequenceOf(this) + this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }
fun CallableMemberDescriptor.allSuperVersions(): Sequence =
    sequenceOf(this) + this.overriddenDescriptors.asSequence().flatMap { it.allSuperVersions() }


inline fun  PsiElement.parentIfType(): T? = parent as? T
inline fun  PsiElement.parentOfType(): T? = parentOfType(T::class.java)
fun  PsiElement.parentOfType(type: Class): T? =
    if (type.isInstance(this.parent)) type.cast(this.parent) else this.parent?.parentOfType(type)


fun MemberScope.functionsNamed(name: String) = getContributedFunctions(Name.identifier(name), NoLookupLocation.FROM_BACKEND)


fun KtExpression.hasNewlineBefore(typedRule: KtQualifiedExpression): Boolean {
    return typedRule.prevSibling
        ?.let { it as? PsiWhiteSpace }
        ?.textContains('\n') == true
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy