Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sealed interfaces rendering #3429

Merged
merged 2 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ internal class DokkaPsiParser(
JavaClassKindTypes.INTERFACE
)
}).toSourceSetDependent()
val modifiers = getModifier().toSourceSetDependent()
val implementedInterfacesExtra =
ImplementedInterfaces(ancestry.allImplementedInterfaces().toSourceSetDependent())

Expand Down Expand Up @@ -326,6 +325,7 @@ internal class DokkaPsiParser(
companion = null,
generics = mapTypeParameters(dri),
supertypes = ancestors,
modifier = getModifier().toSourceSetDependent(),
sourceSets = setOf(sourceSetData),
isExpectActual = false,
extra = PropertyContainer.withAll(
Expand All @@ -349,7 +349,7 @@ internal class DokkaPsiParser(
supertypes = ancestors,
documentation = documentation,
expectPresentInSet = null,
modifier = modifiers,
modifier = getModifier().toSourceSetDependent(),
sourceSets = setOf(sourceSetData),
isExpectActual = false,
extra = PropertyContainer.withAll(
Expand Down Expand Up @@ -598,10 +598,17 @@ internal class DokkaPsiParser(
else -> getBound(type)
}

private fun PsiModifierListOwner.getModifier() = when {
hasModifier(JvmModifier.ABSTRACT) -> JavaModifier.Abstract
hasModifier(JvmModifier.FINAL) -> JavaModifier.Final
else -> JavaModifier.Empty
private fun PsiModifierListOwner.getModifier(): JavaModifier {
val isInterface = this is PsiClass && this.isInterface

return if (isInterface) {
// Java interface can't have modality modifiers except for "sealed", which is not supported yet in Dokka
JavaModifier.Empty
} else when {
hasModifier(JvmModifier.ABSTRACT) -> JavaModifier.Abstract
hasModifier(JvmModifier.FINAL) -> JavaModifier.Final
else -> JavaModifier.Empty
}
}

private fun PsiTypeParameterListOwner.mapTypeParameters(dri: DRI): List<DTypeParameter> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package org.jetbrains.dokka.analysis.test.documentable

import org.jetbrains.dokka.analysis.test.api.kotlinJvmTestProject
import org.jetbrains.dokka.analysis.test.api.parse
import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.DInterface
import org.jetbrains.dokka.model.KotlinModifier
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class KotlinModalityTest {

@Test
fun `should parse kotlin sealed class`() {
val project = kotlinJvmTestProject {
ktFile("Sealed.kt") {
+"sealed class Sealed"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Sealed", classlike.name)
assertTrue(classlike is DClass)
assertEquals(KotlinModifier.Sealed, classlike.modifier.values.single())
}

@Test
fun `should parse kotlin abstract class`() {
val project = kotlinJvmTestProject {
ktFile("Abstract.kt") {
+"abstract class Abstract"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Abstract", classlike.name)
assertTrue(classlike is DClass)
assertEquals(KotlinModifier.Abstract, classlike.modifier.values.single())
}

@Test
fun `should parse kotlin open class`() {
val project = kotlinJvmTestProject {
ktFile("Open.kt") {
+"open class Open"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Open", classlike.name)
assertTrue(classlike is DClass)
assertEquals(KotlinModifier.Open, classlike.modifier.values.single())
}

@Test
fun `should parse kotlin class`() {
val project = kotlinJvmTestProject {
ktFile("Class.kt") {
+"class Class"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Class", classlike.name)
assertTrue(classlike is DClass)
assertEquals(KotlinModifier.Final, classlike.modifier.values.single())
}

@Test
fun `should parse kotlin sealed interface`() {
val project = kotlinJvmTestProject {
ktFile("Sealed.kt") {
+"sealed interface Sealed"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Sealed", classlike.name)
assertTrue(classlike is DInterface)
assertEquals(KotlinModifier.Sealed, classlike.modifier.values.single())
}

@Test
fun `should parse ordinary kotlin interface`() {
val project = kotlinJvmTestProject {
ktFile("Interface.kt") {
+"interface Interface"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Interface", classlike.name)
assertTrue(classlike is DInterface)
assertEquals(KotlinModifier.Empty, classlike.modifier.values.single())
}

@Test
fun `should parse abstract kotlin interface`() {
val project = kotlinJvmTestProject {
ktFile("Interface.kt") {
+"abstract interface Interface"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Interface", classlike.name)
assertTrue(classlike is DInterface)
assertEquals(KotlinModifier.Empty, classlike.modifier.values.single())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2014-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license.
*/

package org.jetbrains.dokka.analysis.test.jvm.java

import org.jetbrains.dokka.analysis.test.api.javaTestProject
import org.jetbrains.dokka.analysis.test.api.parse
import org.jetbrains.dokka.model.DClass
import org.jetbrains.dokka.model.DInterface
import org.jetbrains.dokka.model.JavaModifier
import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertTrue

class JavaModalityTest {

@Test
fun `should parse java class`() {
val project = javaTestProject {
javaFile("Class.java") {
+"class Class {}"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Class", classlike.name)
assertTrue(classlike is DClass)
assertEquals(JavaModifier.Empty, classlike.modifier.values.single())
}

@Test
fun `should parse java abstract class`() {
val project = javaTestProject {
javaFile("Abstract.java") {
+"abstract class Abstract {}"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Abstract", classlike.name)
assertTrue(classlike is DClass)
assertEquals(JavaModifier.Abstract, classlike.modifier.values.single())
}

@Test
fun `should parse java final class`() {
val project = javaTestProject {
javaFile("Final.java") {
+"final class Final {}"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Final", classlike.name)
assertTrue(classlike is DClass)
assertEquals(JavaModifier.Final, classlike.modifier.values.single())
}

@Test
fun `should parse java interface`() {
val project = javaTestProject {
javaFile("Interface.java") {
+"interface Interface {}"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Interface", classlike.name)
assertTrue(classlike is DInterface)
assertEquals(JavaModifier.Empty, classlike.modifier.values.single())
}

@Test
fun `should parse abstract java interface`() {
val project = javaTestProject {
javaFile("Interface.java") {
+"abstract interface Interface {}"
}
}

val module = project.parse()
val pkg = module.packages.single()
val classlike = pkg.classlikes.single()

assertEquals("Interface", classlike.name)
assertTrue(classlike is DInterface)
assertEquals(JavaModifier.Empty, classlike.modifier.values.single())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ private class DokkaDescriptorVisitor(
documentation = info.docs,
generics = generics.await(),
companion = descriptor.companion(driWithPlatform),
modifier = descriptor.modifier().toSourceSetDependent(),
sourceSets = setOf(sourceSet),
isExpectActual = (isExpect || isActual),
extra = PropertyContainer.withAll(
Expand Down Expand Up @@ -1111,12 +1112,24 @@ private class DokkaDescriptorVisitor(
objectDescriptor(it, dri)
}

private fun MemberDescriptor.modifier() = when (modality) {
Modality.FINAL -> KotlinModifier.Final
Modality.SEALED -> KotlinModifier.Sealed
Modality.OPEN -> KotlinModifier.Open
Modality.ABSTRACT -> KotlinModifier.Abstract
else -> KotlinModifier.Empty
private fun MemberDescriptor.modifier(): KotlinModifier {
val isInterface = this is ClassDescriptor && this.kind == ClassKind.INTERFACE
return if (isInterface) {
// only two modalities are possible for interfaces:
// - `SEALED` - when it's declared as `sealed interface`
// - `ABSTRACT` - when it's declared as `interface` or `abstract interface` (`abstract` is redundant but possible here)
when (modality) {
Modality.SEALED -> KotlinModifier.Sealed
else -> KotlinModifier.Empty
}
} else {
when (modality) {
Modality.FINAL -> KotlinModifier.Final
Modality.SEALED -> KotlinModifier.Sealed
Modality.OPEN -> KotlinModifier.Open
Modality.ABSTRACT -> KotlinModifier.Abstract
}
}
}

private fun MemberDescriptor.createSources(): SourceSetDependent<DocumentableSource> =
Expand Down
Loading
Loading