diff options
author | Szymon Świstun <sswistun@virtuslab.com> | 2020-03-02 11:07:47 +0100 |
---|---|---|
committer | Kamil Doległo <kamilok1965@interia.pl> | 2020-03-04 13:19:17 +0100 |
commit | 9b7a59cdc93a6dcd9ab0623f6f476abf92d9427d (patch) | |
tree | 4c6c0f730c6480f824d6e92949c77940929241a1 | |
parent | ac590359174995a16a116a96dbb9df5dafa042f5 (diff) | |
download | dokka-9b7a59cdc93a6dcd9ab0623f6f476abf92d9427d.tar.gz dokka-9b7a59cdc93a6dcd9ab0623f6f476abf92d9427d.tar.bz2 dokka-9b7a59cdc93a6dcd9ab0623f6f476abf92d9427d.zip |
Add information about modifiers to Documentables' extra properties
5 files changed, 107 insertions, 21 deletions
diff --git a/core/src/main/kotlin/model/Documentable.kt b/core/src/main/kotlin/model/Documentable.kt index 3bc1690b..9de06ba7 100644 --- a/core/src/main/kotlin/model/Documentable.kt +++ b/core/src/main/kotlin/model/Documentable.kt @@ -3,8 +3,6 @@ package org.jetbrains.dokka.model import com.intellij.psi.PsiNamedElement import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.model.doc.DocumentationNode -import org.jetbrains.dokka.model.properties.ExtraProperty -import org.jetbrains.dokka.model.properties.MergeStrategy import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.model.properties.WithExtraProperties import org.jetbrains.dokka.pages.PlatformData @@ -335,6 +333,11 @@ sealed class Projection { data class Nullable(val inner: Projection) : Projection() } +enum class ExtraModifiers { + STATIC, INLINE, INFIX, SUSPEND, REIFIED, CROSSINLINE, NOINLINE, + OVERRIDE, DATA, CONST, DYNAMIC, EXTERNAL, INNER, LATEINIT, OPERATOR, TAILREC, VARARG +} + private fun String.shorten(maxLength: Int) = lineSequence().first().let { if (it.length != length || it.length > maxLength) it.take(maxLength - 3) + "..." else it } diff --git a/core/src/main/kotlin/model/aditionalExtras.kt b/core/src/main/kotlin/model/aditionalExtras.kt new file mode 100644 index 00000000..69e89a2a --- /dev/null +++ b/core/src/main/kotlin/model/aditionalExtras.kt @@ -0,0 +1,11 @@ +package org.jetbrains.dokka.model + +import org.jetbrains.dokka.model.properties.ExtraProperty + +class AdditionalModifiers(val content: List<ExtraModifiers>) : ExtraProperty<Documentable> { + object AdditionalKey : ExtraProperty.Key<Documentable, AdditionalModifiers> + + override fun equals(other: Any?): Boolean = if (other is AdditionalModifiers) other.content == content else false + override fun hashCode() = content.hashCode() + override val key: ExtraProperty.Key<Documentable, *> = AdditionalKey +}
\ No newline at end of file diff --git a/core/src/main/kotlin/model/properties/properties.kt b/core/src/main/kotlin/model/properties/properties.kt index 83f8d63d..7010d0df 100644 --- a/core/src/main/kotlin/model/properties/properties.kt +++ b/core/src/main/kotlin/model/properties/properties.kt @@ -1,21 +1,22 @@ package org.jetbrains.dokka.model.properties interface ExtraProperty<in C : Any> { - interface Key<in C: Any, T: Any> { + interface Key<in C : Any, T : Any> { fun mergeStrategyFor(left: T, right: T): MergeStrategy<C> = MergeStrategy.Fail { throw NotImplementedError("Property merging for $this is not implemented") } } + val key: Key<C, *> } -interface CalculatedProperty<in C: Any, T: Any>: ExtraProperty.Key<C, T> { +interface CalculatedProperty<in C : Any, T : Any> : ExtraProperty.Key<C, T> { fun calculate(subject: C): T } sealed class MergeStrategy<in C> { - class Replace<in C : Any>(val newProperty: ExtraProperty<C>): MergeStrategy<C>() - object Remove: MergeStrategy<Any>() - class Full<C: Any>(val merger: (preMerged: C, left: C, right: C) -> C): MergeStrategy<C>() - class Fail(val error: () -> Nothing): MergeStrategy<Any>() + class Replace<in C : Any>(val newProperty: ExtraProperty<C>) : MergeStrategy<C>() + object Remove : MergeStrategy<Any>() + class Full<C : Any>(val merger: (preMerged: C, left: C, right: C) -> C) : MergeStrategy<C>() + class Fail(val error: () -> Nothing) : MergeStrategy<Any>() } diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 531537a4..cea408d7 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -8,17 +8,23 @@ import org.jetbrains.dokka.model.* import org.jetbrains.dokka.model.Enum import org.jetbrains.dokka.model.Function import org.jetbrains.dokka.model.doc.DocumentationNode +import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.pages.PlatformData import org.jetbrains.dokka.parsers.MarkdownParser import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.transformers.descriptors.DescriptorToDocumentableTranslator +import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.idea.kdoc.findKDoc import org.jetbrains.kotlin.idea.refactoring.fqName.fqName +import org.jetbrains.kotlin.resolve.DescriptorUtils +import org.jetbrains.kotlin.resolve.calls.components.isVararg +import org.jetbrains.kotlin.resolve.calls.tasks.isDynamic import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameSafe import org.jetbrains.kotlin.resolve.descriptorUtil.getAllSuperclassesWithoutAny +import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperClassNotAny import org.jetbrains.kotlin.resolve.descriptorUtil.getSuperInterfaces import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter import org.jetbrains.kotlin.resolve.scopes.MemberScope @@ -102,7 +108,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = info.docs, generics = descriptor.typeConstructor.parameters.map { it.toTypeParameter() }, companion = descriptor.companion(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -121,7 +128,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private visibility = PlatformDependent(mapOf(platformData to descriptor.visibility)), supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -143,7 +151,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private supertypes = PlatformDependent.from(platformData, info.supertypes), documentation = info.docs, companion = descriptor.companion(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -158,7 +167,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private classlikes = scope.classlikes(driWithPlatform), functions = scope.functions(driWithPlatform), properties = scope.properties(driWithPlatform), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -188,7 +198,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private modifier = descriptor.modifier(), companion = descriptor.companion(driWithPlatform), supertypes = PlatformDependent.from(platformData, info.supertypes), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -213,7 +224,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), type = KotlinTypeWrapper(descriptor.returnType!!), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -237,7 +249,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private documentation = descriptor.resolveDescriptorData(platformData), modifier = descriptor.modifier(), type = KotlinTypeWrapper(descriptor.returnType!!), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -260,7 +273,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private type = KotlinTypeWrapper(descriptor.returnType), modifier = descriptor.modifier(), generics = descriptor.typeParameters.map { it.toTypeParameter() }, - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -289,7 +303,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private this.name.asString(), type = KotlinTypeWrapper(this.type), documentation = descriptor.resolveDescriptorData(platformData), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) val name = run { @@ -322,7 +337,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private ) }, sources = descriptor.createSources(), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) } @@ -332,7 +348,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private name = descriptor.name.asString(), type = KotlinTypeWrapper(descriptor.type), documentation = descriptor.resolveDescriptorData(platformData), - platformData = listOf(platformData) + platformData = listOf(platformData), + extra = descriptor.additionalExtras() ) private fun MemberScope.functions(parent: DRIWithPlatformInfo): List<Function> = @@ -378,7 +395,8 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private fqNameSafe.asString(), PlatformDependent.from(platformData, getDocumentation()), upperBounds.map { it.toProjection() }, - listOf(platformData) + listOf(platformData), + extra = additionalExtras() ) private fun KotlinType.toProjection(): Projection = when (constructor.declarationDescriptor) { @@ -419,5 +437,56 @@ open class DokkaDescriptorVisitor( // TODO: close this class and make it private PlatformDependent(mapOf(platformData to DescriptorDocumentableSource(this))) } + inline fun <reified D : Documentable> FunctionDescriptor.additionalExtras(): PropertyContainer<D> = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.INFIX.takeIf { isInfix }, + ExtraModifiers.INLINE.takeIf { isInline }, + ExtraModifiers.SUSPEND.takeIf { isSuspend }, + ExtraModifiers.OPERATOR.takeIf { isOperator }, + ExtraModifiers.STATIC.takeIf { isJvmStaticInObjectOrClassOrInterface() }, + ExtraModifiers.TAILREC.takeIf { isTailrec }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.OVERRIDE.takeIf { DescriptorUtils.isOverride(this) } + ).toContainer() + + inline fun <reified D : Documentable> ClassDescriptor.additionalExtras(): PropertyContainer<D> = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.INLINE.takeIf { isInline }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.INNER.takeIf { isInner }, + ExtraModifiers.DATA.takeIf { isData }, + ExtraModifiers.OVERRIDE.takeIf { getSuperInterfaces().isNotEmpty() || getSuperClassNotAny() != null } + ).toContainer() + + fun ValueParameterDescriptor.additionalExtras(): PropertyContainer<Parameter> = + listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.NOINLINE.takeIf { isNoinline }, + ExtraModifiers.CROSSINLINE.takeIf { isCrossinline }, + ExtraModifiers.CONST.takeIf { isConst }, + ExtraModifiers.LATEINIT.takeIf { isLateInit }, + ExtraModifiers.VARARG.takeIf { isVararg } + ).toContainer() + + fun TypeParameterDescriptor.additionalExtras(): PropertyContainer<TypeParameter> = + listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.REIFIED.takeIf { isReified } + ).toContainer() + + fun PropertyDescriptor.additionalExtras(): PropertyContainer<Property> = listOfNotNull( + ExtraModifiers.DYNAMIC.takeIf { isDynamic() }, + ExtraModifiers.CONST.takeIf { isConst }, + ExtraModifiers.LATEINIT.takeIf { isLateInit }, + ExtraModifiers.STATIC.takeIf { isJvmStaticInObjectOrClassOrInterface() }, + ExtraModifiers.EXTERNAL.takeIf { isExternal }, + ExtraModifiers.OVERRIDE.takeIf { DescriptorUtils.isOverride(this) } + ).toContainer() + + inline fun <reified D : Documentable> List<ExtraModifiers>.toContainer( + container: PropertyContainer<D> = PropertyContainer.empty() + ): PropertyContainer<D> = + container + AdditionalModifiers(this) + data class ClassInfo(val supertypes: List<DRI>, val docs: PlatformDependent<DocumentationNode>) } diff --git a/plugins/base/src/test/kotlin/model/ClassesTest.kt b/plugins/base/src/test/kotlin/model/ClassesTest.kt index 2332df61..18288442 100644 --- a/plugins/base/src/test/kotlin/model/ClassesTest.kt +++ b/plugins/base/src/test/kotlin/model/ClassesTest.kt @@ -151,7 +151,9 @@ class ClassesTest : AbstractModelTest("/src/main/kotlin/classes/Test.kt", "class with((this / "classes" / "Klass").cast<Class>()) { name equals "Klass" visibility.values allEquals Visibilities.PUBLIC - // TODO data modifier + with(extra[AdditionalModifiers.AdditionalKey].assertNotNull("Extras")) { + content.find{it == ExtraModifiers.DATA}.assertNotNull("data modifier") + } } } } |