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

org.jetbrains.dokka.analysis.DRIFactory.kt Maven / Gradle / Ivy

Go to download

Dokka is an API documentation engine for Kotlin and Java, performing the same function as Javadoc for Java

There is a newer version: 1.8.20
Show newest version
package org.jetbrains.dokka.analysis

import com.intellij.psi.*
import org.jetbrains.dokka.links.Callable
import org.jetbrains.dokka.links.DRI
import org.jetbrains.dokka.links.DriTarget
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf
import org.jetbrains.kotlin.resolve.descriptorUtil.parentsWithSelf
import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull

fun DRI.Companion.from(descriptor: DeclarationDescriptor) = descriptor.parentsWithSelf.run {
    val parameter = firstIsInstanceOrNull()
    val callable = parameter?.containingDeclaration ?: firstIsInstanceOrNull()

    DRI(
        firstIsInstanceOrNull()?.fqName?.asString() ?: "",
        (filterIsInstance() + filterIsInstance()).toList()
            .takeIf { it.isNotEmpty() }
            ?.asReversed()
            ?.joinToString(separator = ".") { it.name.asString() },
        callable?.let { Callable.from(it) },
        DriTarget.from(parameter ?: descriptor)
    )
}

fun DRI.Companion.from(psi: PsiElement) = psi.parentsWithSelf.run {
    val psiMethod = firstIsInstanceOrNull()
    val psiField = firstIsInstanceOrNull()
    val classes = filterIsInstance().filterNot { it is PsiTypeParameter }
        .toList() // We only want exact PsiClass types, not PsiTypeParameter subtype
    DRI(
        classes.lastOrNull()?.qualifiedName?.substringBeforeLast('.', "") ?: "",
        classes.toList().takeIf { it.isNotEmpty() }?.asReversed()?.mapNotNull { it.name }?.joinToString("."),
        psiMethod?.let { Callable.from(it) } ?: psiField?.let { Callable.from(it) },
        DriTarget.from(psi)
    )
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy