diff options
author | Marcin Aman <marcin.aman@gmail.com> | 2020-12-29 14:38:19 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-29 14:38:19 +0100 |
commit | f5e7cffbebb66b989c64bdda61e1f7809ddc6068 (patch) | |
tree | ea0ef4b550f9a55681a915d5cb4d9f1beca22041 /plugins/base/src/main/kotlin/translators | |
parent | e55f3b015faec3f62e829a2aa5984e4bd6d5037e (diff) | |
download | dokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.tar.gz dokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.tar.bz2 dokka-f5e7cffbebb66b989c64bdda61e1f7809ddc6068.zip |
Parsing of JvmName (#1675)
* Parsing of JvmName
* Make JvmName processor run after KaJ
Diffstat (limited to 'plugins/base/src/main/kotlin/translators')
3 files changed, 40 insertions, 19 deletions
diff --git a/plugins/base/src/main/kotlin/translators/annotationsValue.kt b/plugins/base/src/main/kotlin/translators/annotationsValue.kt new file mode 100644 index 00000000..a840816a --- /dev/null +++ b/plugins/base/src/main/kotlin/translators/annotationsValue.kt @@ -0,0 +1,3 @@ +package org.jetbrains.dokka.base.translators + +internal fun unquotedValue(value: String): String = value.removeSurrounding("\"")
\ No newline at end of file diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 5185dc3e..00c9a8e1 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -10,6 +10,7 @@ import org.jetbrains.dokka.analysis.from import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.parsers.MarkdownParser import org.jetbrains.dokka.base.translators.isDirectlyAnException +import org.jetbrains.dokka.base.translators.unquotedValue import org.jetbrains.dokka.links.* import org.jetbrains.dokka.links.Callable import org.jetbrains.dokka.model.* @@ -19,10 +20,8 @@ import org.jetbrains.dokka.model.doc.* import org.jetbrains.dokka.model.properties.PropertyContainer import org.jetbrains.dokka.plugability.DokkaContext import org.jetbrains.dokka.plugability.plugin -import org.jetbrains.dokka.plugability.query import org.jetbrains.dokka.plugability.querySingle import org.jetbrains.dokka.transformers.sources.AsyncSourceToDocumentableTranslator -import org.jetbrains.dokka.transformers.sources.SourceToDocumentableTranslator import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor import org.jetbrains.kotlin.builtins.isBuiltinExtensionFunctionalType @@ -33,7 +32,6 @@ import org.jetbrains.kotlin.builtins.isSuspendFunctionTypeOrSubtype import org.jetbrains.kotlin.codegen.isJvmStaticInObjectOrClassOrInterface import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind -import org.jetbrains.kotlin.descriptors.Visibility import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor import org.jetbrains.kotlin.idea.kdoc.findKDoc @@ -51,8 +49,8 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.resolve.source.PsiSourceElement +import org.jetbrains.kotlin.resolve.source.PsiSourceFile import org.jetbrains.kotlin.types.* -import org.jetbrains.kotlin.types.model.typeConstructor import org.jetbrains.kotlin.types.typeUtil.immediateSupertypes import org.jetbrains.kotlin.types.typeUtil.isAnyOrNullableAny import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull @@ -117,7 +115,7 @@ private class DokkaDescriptorVisitor( } } - private fun <T> T.toSourceSetDependent() = mapOf(sourceSet to this) + private fun <T> T.toSourceSetDependent() = if(this != null) mapOf(sourceSet to this) else emptyMap() suspend fun visitPackageFragmentDescriptor( descriptor: PackageFragmentDescriptor, @@ -436,13 +434,16 @@ private class DokkaDescriptorVisitor( sourceSets = setOf(sourceSet), generics = generics.await(), isExpectActual = (isExpect || isActual), - extra = PropertyContainer.withAll(listOfNotNull( - (descriptor.additionalExtras() + descriptor.getAnnotationsWithBackingField() - .toAdditionalExtras()).toSet().toSourceSetDependent().toAdditionalModifiers(), - descriptor.getAnnotationsWithBackingField().toSourceSetDependent().toAnnotations(), - descriptor.getDefaultValue()?.let { DefaultValue(it) }, - InheritedMember(inheritedFrom.toSourceSetDependent()), - )) + extra = PropertyContainer.withAll( + listOfNotNull( + (descriptor.additionalExtras() + descriptor.getAnnotationsWithBackingField() + .toAdditionalExtras()).toSet().toSourceSetDependent().toAdditionalModifiers(), + (descriptor.getAnnotationsWithBackingField() + descriptor.fileLevelAnnotations()).toSourceSetDependent() + .toAnnotations(), + descriptor.getDefaultValue()?.let { DefaultValue(it) }, + InheritedMember(inheritedFrom.toSourceSetDependent()), + ) + ) ) } } @@ -489,7 +490,7 @@ private class DokkaDescriptorVisitor( extra = PropertyContainer.withAll( InheritedMember(inheritedFrom.toSourceSetDependent()), descriptor.additionalExtras().toSourceSetDependent().toAdditionalModifiers(), - descriptor.getAnnotations().toSourceSetDependent().toAnnotations() + (descriptor.getAnnotations() + descriptor.fileLevelAnnotations()).toSourceSetDependent().toAnnotations(), ) ) } @@ -588,7 +589,7 @@ private class DokkaDescriptorVisitor( val name = run { val modifier = if (isGetter) "get" else "set" val rawName = propertyDescriptor.name.asString() - "$modifier${rawName[0].toUpperCase()}${rawName.drop(1)}" + "$modifier${rawName.capitalize()}" } val parameters = @@ -782,8 +783,7 @@ private class DokkaDescriptorVisitor( private suspend fun org.jetbrains.kotlin.descriptors.annotations.Annotations.getPresentableName(): String? = map { it.toAnnotation() }.singleOrNull { it.dri.classNames == "ParameterName" }?.params?.get("name") - .safeAs<StringValue>()?.value?.drop(1) - ?.dropLast(1) // Dropping enclosing doublequotes because we don't want to have it in our custom signature serializer + .safeAs<StringValue>()?.value?.let { unquotedValue(it) } private suspend fun KotlinType.toBound(): Bound = when (this) { @@ -928,19 +928,25 @@ private class DokkaDescriptorVisitor( ) } } - else -> StringValue(toString()) + else -> StringValue(unquotedValue(toString())) } - private suspend fun AnnotationDescriptor.toAnnotation(): Annotations.Annotation { + private suspend fun AnnotationDescriptor.toAnnotation(scope: Annotations.AnnotationScope = Annotations.AnnotationScope.DIRECT): Annotations.Annotation { + val dri = DRI.from(annotationClass as DeclarationDescriptor) return Annotations.Annotation( DRI.from(annotationClass as DeclarationDescriptor), allValueArguments.map { it.key.asString() to it.value.toValue() }.filter { it.second != null }.toMap() as Map<String, AnnotationParameterValue>, - annotationClass!!.annotations.hasAnnotation(FqName("kotlin.annotation.MustBeDocumented")) + mustBeDocumented(dri), + scope ) } + private fun AnnotationDescriptor.mustBeDocumented(dri: DRI): Boolean = + if (dri.isJvmName()) false + else annotationClass!!.annotations.hasAnnotation(FqName("kotlin.annotation.MustBeDocumented")) + private suspend fun PropertyDescriptor.getAnnotationsWithBackingField(): List<Annotations.Annotation> = getAnnotations() + (backingField?.getAnnotations() ?: emptyList()) @@ -1004,6 +1010,14 @@ private class DokkaDescriptorVisitor( private fun ConstantsEnumValue.fullEnumEntryName() = "${this.enumClassId.relativeClassName.asString()}.${this.enumEntryName.identifier}" + + private fun DeclarationDescriptorWithSource.ktFile(): KtFile? = (source.containingFile as? PsiSourceFile)?.psiFile as? KtFile + + private suspend fun DeclarationDescriptorWithSource.fileLevelAnnotations() = ktFile() + ?.let { file -> resolutionFacade.resolveSession.getFileAnnotations(file) } + ?.toList() + .orEmpty() + .parallelMap { it.toAnnotation(scope = Annotations.AnnotationScope.FILE) } } private data class AncestryLevel( diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index 9a1b05d5..cd51e9dd 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -17,6 +17,7 @@ import org.jetbrains.dokka.base.DokkaBase import org.jetbrains.dokka.base.translators.isDirectlyAnException import org.jetbrains.dokka.base.translators.psi.parsers.JavaDocumentationParser import org.jetbrains.dokka.base.translators.psi.parsers.JavadocParser +import org.jetbrains.dokka.base.translators.unquotedValue import org.jetbrains.dokka.links.DRI import org.jetbrains.dokka.links.nextTarget import org.jetbrains.dokka.links.withClass @@ -530,6 +531,9 @@ class DefaultPsiToDocumentableTranslator( private fun JvmAnnotationAttribute.toValue(): AnnotationParameterValue = when (this) { is PsiNameValuePair -> value?.toValue() ?: StringValue("") else -> StringValue(this.attributeName) + }.let { annotationValue -> + if (annotationValue is StringValue) annotationValue.copy(unquotedValue(annotationValue.value)) + else annotationValue } private fun PsiAnnotationMemberValue.toValue(): AnnotationParameterValue? = when (this) { |