diff options
Diffstat (limited to 'core/src/main/kotlin/links/DRI.kt')
-rw-r--r-- | core/src/main/kotlin/links/DRI.kt | 69 |
1 files changed, 54 insertions, 15 deletions
diff --git a/core/src/main/kotlin/links/DRI.kt b/core/src/main/kotlin/links/DRI.kt index 35eb6497..7ab25444 100644 --- a/core/src/main/kotlin/links/DRI.kt +++ b/core/src/main/kotlin/links/DRI.kt @@ -1,22 +1,28 @@ package org.jetbrains.dokka.links +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.types.KotlinType import java.text.ParseException /** * [DRI] stands for DokkaResourceIdentifier */ -data class DRI(val packageName: String? = null, - val classNames: String? = null, - val callable: Callable? = null, - val target: Int? = null, - val extra: String? = null) { - - constructor(packageName: String? = null, - classNames: String? = null, - callableName: String? = null, - signature: String? = null, - target: Int? = null, - extra: String? = null) : this(packageName, classNames, Callable.from(callableName, signature), target, extra) +data class DRI( + val packageName: String? = null, + val classNames: String? = null, + val callable: Callable? = null, + val target: Int? = null, + val extra: String? = null +) { + + constructor( + packageName: String? = null, + classNames: String? = null, + callableName: String? = null, + signature: String? = null, + target: Int? = null, + extra: String? = null + ) : this(packageName, classNames, Callable.from(callableName, signature), target, extra) override fun toString(): String = "${packageName.orEmpty()}/${classNames.orEmpty()}/${callable?.name.orEmpty()}/${callable?.signature().orEmpty()}/${target?.toString().orEmpty()}/${extra.orEmpty()}" @@ -29,7 +35,11 @@ data class DRI(val packageName: String? = null, DRI( packageName, classNames, - try { Callable.from(callableName, callableSignature) } catch(e: ParseException) { null }, + try { + Callable.from(callableName, callableSignature) + } catch (e: ParseException) { + null + }, target?.toInt(), ext ) @@ -37,11 +47,25 @@ data class DRI(val packageName: String? = null, } catch (e: Throwable) { throw ParseException(s, 0) } + + val topLevel = DRI() } } +fun DRI.withClass(name: String) = copy(classNames = classNames.orEmpty() + ".$name") + +val DRI.parent: DRI + get() = when { + extra != null -> copy(extra = null) + target != null -> copy(target = null) + callable != null -> copy(callable = null) + classNames != null -> copy(classNames = classNames.substringBeforeLast('.').takeIf { it.isNotBlank() }) + else -> DRI.topLevel + } + data class Callable(val name: String, val receiver: String, val returnType: String, val params: List<String>) { - fun signature() = "$receiver.$returnType.${params.joinToString(".")}" + fun signature() = "$receiver#$returnType#${params.joinToString("#")}" + companion object { fun from(name: String?, signature: String?): Callable = try { signature.toString() @@ -63,10 +87,25 @@ data class Callable(val name: String, val receiver: String, val returnType: Stri } catch (e: Throwable) { throw ParseException(s, 0) } + + fun from(descriptor: CallableDescriptor) = with(descriptor) { + Callable( + name.asString(), + extensionReceiverParameter?.value?.type?.constructorName.orEmpty(), + returnType?.constructorName.orEmpty(), + valueParameters.map { it.type.constructorName.orEmpty() } + ) + } } } -data class ClassReference(val dri: DRI, val subs: MutableList<ClassReference> = mutableListOf()) +data class ClassReference(val dri: DRI, val subs: MutableList<ClassReference> = mutableListOf()) { + private val subsText = subs.takeIf { it.isNotEmpty() }?.toString().orEmpty() + override fun toString() = "$dri$subsText" +} private operator fun <T> List<T>.component6(): T = get(5) +private val KotlinType.constructorName + get() = constructor.declarationDescriptor?.name?.asString() + |