Skip to content

Commit 012ac6d

Browse files
authored
Introduce isProperty to DRI and fix merging property and function with the same name (#4347)
1 parent 441bda8 commit 012ac6d

File tree

15 files changed

+238
-56
lines changed

15 files changed

+238
-56
lines changed

dokka-subprojects/analysis-java-psi/src/main/kotlin/org/jetbrains/dokka/analysis/java/util/PsiUtil.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ internal fun Callable.Companion.from(psi: PsiField): Callable {
5555
return Callable(
5656
name = psi.name,
5757
receiver = null,
58-
params = emptyList()
58+
params = emptyList(),
59+
isProperty = true
5960
)
6061
}
6162

dokka-subprojects/analysis-kotlin-descriptors-compiler/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/descriptors/compiler/configuration/CallableFactory.kt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ import org.jetbrains.dokka.links.Callable
1010
import org.jetbrains.dokka.links.JavaClassReference
1111
import org.jetbrains.dokka.links.TypeReference
1212
import org.jetbrains.kotlin.descriptors.CallableDescriptor
13+
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
1314

1415
internal fun Callable.Companion.from(descriptor: CallableDescriptor, name: String? = null) = with(descriptor) {
1516
Callable(
1617
name ?: descriptor.name.asString(),
1718
extensionReceiverParameter?.let { TypeReference.from(it) },
18-
valueParameters.mapNotNull { TypeReference.from(it) }
19+
valueParameters.mapNotNull { TypeReference.from(it) },
20+
isProperty = descriptor is PropertyDescriptor
1921
)
2022
}
2123

@@ -30,6 +32,7 @@ internal fun Callable.Companion.from(psi: PsiField): Callable {
3032
return Callable(
3133
name = psi.name,
3234
receiver = null,
33-
params = emptyList()
35+
params = emptyList(),
36+
isProperty = true
3437
)
3538
}

dokka-subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DRIFactory.kt

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,15 @@ internal fun ClassId.createDRI(): DRI = DRI(
1818
packageName = this.packageFqName.asString(), classNames = this.relativeClassName.asString()
1919
)
2020

21-
private fun CallableId.createDRI(receiver: TypeReference?, params: List<TypeReference>, contextParams: List<TypeReference>): DRI = DRI(
21+
private fun CallableId.createDRI(receiver: TypeReference?, params: List<TypeReference>, contextParams: List<TypeReference>, isProperty: Boolean): DRI = DRI(
2222
packageName = this.packageName.asString(),
2323
classNames = this.className?.asString(),
2424
callable = Callable(
2525
this.callableName.asString(),
2626
params = params,
2727
receiver = receiver,
28-
contextParameters = contextParams
28+
contextParameters = contextParams,
29+
isProperty = isProperty
2930
)
3031
)
3132

@@ -66,7 +67,7 @@ internal fun KaSession.getDRIFromVariable(symbol: KaVariableSymbol): DRI {
6667
val callableId = symbol.callableId ?: throw IllegalStateException("Can not get callable Id due to it is local")
6768
val receiver = symbol.receiverType?.let(::getTypeReferenceFrom)
6869
val contextParams = @OptIn(KaExperimentalApi::class) symbol.contextParameters.map { getTypeReferenceFrom(it.returnType) }
69-
return callableId.createDRI(receiver, emptyList(), contextParams)
70+
return callableId.createDRI(receiver, emptyList(), contextParams, true)
7071
}
7172

7273

@@ -76,7 +77,7 @@ internal fun KaSession.getDRIFromFunction(symbol: KaFunctionSymbol): DRI {
7677
val receiver = symbol.receiverType?.let {
7778
getTypeReferenceFrom(it)
7879
}
79-
return symbol.callableId?.createDRI(receiver, params, contextParams) ?: getDRIFromLocalFunction(symbol)
80+
return symbol.callableId?.createDRI(receiver, params, contextParams, false) ?: getDRIFromLocalFunction(symbol)
8081
}
8182

8283
internal fun getDRIFromClassLike(symbol: KaClassLikeSymbol): DRI =

dokka-subprojects/analysis-kotlin-symbols/src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/translators/DefaultSymbolToDocumentableTranslator.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,9 @@ internal class DokkaSymbolVisitor(
593593
propertyDRI.copy(
594594
callable = propertyDRI.callable?.copy(
595595
name = name,
596-
params = propertyAccessorSymbol.valueParameters.map { getTypeReferenceFrom(it.returnType) })
596+
params = propertyAccessorSymbol.valueParameters.map { getTypeReferenceFrom(it.returnType) },
597+
isProperty = false
598+
)
597599
)
598600

599601
val inheritedFrom = if(propertyAccessorSymbol.origin == KaSymbolOrigin.JAVA_SYNTHETIC_PROPERTY) dri.copy(callable = null) else dri.getInheritedFromDRI(propertyParentDRI)

dokka-subprojects/core/api/dokka-core.api

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -473,22 +473,28 @@ public final class org/jetbrains/dokka/links/Callable {
473473
public static final field Companion Lorg/jetbrains/dokka/links/Callable$Companion;
474474
public synthetic fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;)V
475475
public synthetic fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
476-
public fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;)V
476+
public synthetic fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;)V
477477
public synthetic fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
478+
public fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;Z)V
479+
public synthetic fun <init> (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;ZILkotlin/jvm/internal/DefaultConstructorMarker;)V
478480
public final fun component1 ()Ljava/lang/String;
479481
public final fun component2 ()Lorg/jetbrains/dokka/links/TypeReference;
480482
public final fun component3 ()Ljava/util/List;
481483
public final fun component4 ()Ljava/util/List;
484+
public final fun component5 ()Z
482485
public final synthetic fun copy (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;)Lorg/jetbrains/dokka/links/Callable;
483-
public final fun copy (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;)Lorg/jetbrains/dokka/links/Callable;
486+
public final synthetic fun copy (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;)Lorg/jetbrains/dokka/links/Callable;
487+
public final fun copy (Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;Z)Lorg/jetbrains/dokka/links/Callable;
484488
public static synthetic fun copy$default (Lorg/jetbrains/dokka/links/Callable;Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;ILjava/lang/Object;)Lorg/jetbrains/dokka/links/Callable;
485489
public static synthetic fun copy$default (Lorg/jetbrains/dokka/links/Callable;Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;ILjava/lang/Object;)Lorg/jetbrains/dokka/links/Callable;
490+
public static synthetic fun copy$default (Lorg/jetbrains/dokka/links/Callable;Ljava/lang/String;Lorg/jetbrains/dokka/links/TypeReference;Ljava/util/List;Ljava/util/List;ZILjava/lang/Object;)Lorg/jetbrains/dokka/links/Callable;
486491
public fun equals (Ljava/lang/Object;)Z
487492
public final fun getContextParameters ()Ljava/util/List;
488493
public final fun getName ()Ljava/lang/String;
489494
public final fun getParams ()Ljava/util/List;
490495
public final fun getReceiver ()Lorg/jetbrains/dokka/links/TypeReference;
491496
public fun hashCode ()I
497+
public final fun isProperty ()Z
492498
public final fun signature ()Ljava/lang/String;
493499
public fun toString ()Ljava/lang/String;
494500
}

dokka-subprojects/core/src/main/kotlin/org/jetbrains/dokka/links/DRI.kt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.fasterxml.jackson.annotation.JsonTypeInfo
99
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.CLASS
1010
import com.fasterxml.jackson.databind.ObjectMapper
1111
import com.fasterxml.jackson.module.kotlin.readValue
12+
import kotlin.collections.List
1213

1314
/**
1415
* [DRI] stands for DokkaResourceIdentifier
@@ -87,8 +88,16 @@ public data class Callable(
8788
val receiver: TypeReference? = null,
8889
val params: List<TypeReference>,
8990
@property:ExperimentalDokkaApi
90-
val contextParameters: List<TypeReference> = emptyList<TypeReference>()
91+
val contextParameters: List<TypeReference> = emptyList(),
92+
val isProperty: Boolean = false
9193
) {
94+
init {
95+
if (isProperty) require(
96+
params.isEmpty(),
97+
{ "Callable $name is property, but has the following parameters: $params" })
98+
99+
}
100+
92101
@Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
93102
public constructor(
94103
name: String,
@@ -103,6 +112,22 @@ public data class Callable(
103112
params: List<TypeReference> = this.params
104113
): Callable = Callable(name, receiver, params)
105114

115+
@Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
116+
public constructor(
117+
name: String,
118+
receiver: TypeReference? = null,
119+
params: List<TypeReference>,
120+
contextParameters: List<TypeReference>,
121+
) : this(name, receiver, params, contextParameters)
122+
123+
@Deprecated("Binary compatibility", level = DeprecationLevel.HIDDEN)
124+
public fun copy(
125+
name: String = this.name,
126+
receiver: TypeReference? = this.receiver,
127+
params: List<TypeReference> = this.params,
128+
contextParameters: List<TypeReference>
129+
): Callable = Callable(name, receiver, params, contextParameters)
130+
106131
public fun signature(): String {
107132
/**
108133
* Compatibility with [signature] without context parameters must be preserved,

dokka-subprojects/plugin-base/src/main/kotlin/org/jetbrains/dokka/base/translators/documentables/DefaultPageCreator.kt

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ public open class DefaultPageCreator(
118118
props.mergeClashingDocumentable().map(::pageForProperties)
119119
else
120120
classlikes.renameClashingDocumentable().map(::pageForClasslike) +
121-
(functions + props).renameClashingDocumentable().mapNotNull(::pageForMember)
121+
functions.renameClashingDocumentable().map(::pageForFunction)
122+
props.renameClashingDocumentable().mapNotNull(::pageForProperty)
122123

123124
return ClasslikePageNode(
124125
documentables.first().nameAfterClash(), contentForClasslikesAndEntries(documentables), dri, documentables,
@@ -159,7 +160,8 @@ public open class DefaultPageCreator(
159160
entries.mergeClashingDocumentable().map(::pageForEnumEntries)
160161
else
161162
(nestedClasslikes + typealiases).renameClashingDocumentable().map(::pageForClasslike) +
162-
(functions + props).renameClashingDocumentable().mapNotNull(::pageForMember) +
163+
functions.renameClashingDocumentable().map(::pageForFunction) +
164+
props.renameClashingDocumentable().mapNotNull(::pageForProperty) +
163165
entries.renameClashingDocumentable().map(::pageForEnumEntry)
164166

165167

@@ -207,12 +209,6 @@ public open class DefaultPageCreator(
207209
return MemberPageNode(fs.first().nameAfterClash(), contentForMembers(fs), dri, fs)
208210
}
209211

210-
private fun pageForMember(d: Documentable): MemberPageNode? = when (d) {
211-
is DProperty -> pageForProperty(d)
212-
is DFunction -> pageForFunction(d)
213-
else -> throw IllegalStateException()
214-
}
215-
216212
public open fun pageForProperty(p: DProperty): MemberPageNode? =
217213
MemberPageNode(p.nameAfterClash(), contentForProperty(p), setOf(p.dri), listOf(p))
218214

dokka-subprojects/plugin-base/src/test/kotlin/basic/DRITest.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,4 +525,29 @@ class DRITest : BaseAbstractTest() {
525525
}
526526
}
527527
}
528+
529+
@Test
530+
fun `DRI for a property`() {
531+
testInline(
532+
"""
533+
|/src/main/kotlin/Test.kt
534+
|package test
535+
|val prop: Int = 0
536+
""".trimMargin(),
537+
defaultConfiguration
538+
) {
539+
documentablesMergingStage = { module ->
540+
val propDRI = module.dfs { it.name == "prop" }?.dri
541+
542+
assertEquals(
543+
DRI("test", null, Callable("prop", null, emptyList(), isProperty = true)),
544+
propDRI
545+
)
546+
assertEquals(
547+
"test//prop/#/PointingToDeclaration/",
548+
propDRI.toString()
549+
)
550+
}
551+
}
552+
}
528553
}

dokka-subprojects/plugin-base/src/test/kotlin/content/inheritors/ContentForInheritorsTest.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,12 @@ class ContentForInheritorsTest : BaseAbstractTest() {
578578
val targetDRI = DRI(
579579
"test",
580580
"NamedDomainObjectContainerDelegate",
581-
org.jetbrains.dokka.links.Callable("namer", null, emptyList())
581+
org.jetbrains.dokka.links.Callable("namer", null, emptyList(), isProperty = true)
582582
)
583583
val fakeDRI = DRI(
584584
"test",
585585
"NamedDomainObjectContainerScope",
586-
org.jetbrains.dokka.links.Callable("namer", null, emptyList())
586+
org.jetbrains.dokka.links.Callable("namer", null, emptyList(), isProperty = true)
587587
)
588588
assertNull(
589589
module.dfs { it.name == "namer" && (it as? MemberPageNode)?.dri?.singleOrNull() == fakeDRI },

0 commit comments

Comments
 (0)