diff options
author | Marcin Aman <marcin.aman@gmail.com> | 2021-08-25 20:55:41 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-08-25 20:55:41 +0200 |
commit | f2adc0a50462a63f7e1901db2e58077001acd622 (patch) | |
tree | a0a21d3c99b2d8cbe77ebf95d865aabd36d6598f /plugins/base/src/main/kotlin/translators | |
parent | 939cbcd4d867961516da71f7c55d037ec88cb7f3 (diff) | |
parent | efc9f92b0ac083c73ac494c95b530305ac04115a (diff) | |
download | dokka-f2adc0a50462a63f7e1901db2e58077001acd622.tar.gz dokka-f2adc0a50462a63f7e1901db2e58077001acd622.tar.bz2 dokka-f2adc0a50462a63f7e1901db2e58077001acd622.zip |
Merge pull request #2066 from Kotlin/webhelp-like-frontend
Webhelp like frontend
Diffstat (limited to 'plugins/base/src/main/kotlin/translators')
-rw-r--r-- | plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt | 31 | ||||
-rw-r--r-- | plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt | 18 |
2 files changed, 34 insertions, 15 deletions
diff --git a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt index 3ff8ffc3..d986056c 100644 --- a/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/descriptors/DefaultDescriptorToDocumentableTranslator.kt @@ -1,6 +1,7 @@ package org.jetbrains.dokka.base.translators.descriptors import com.intellij.psi.PsiNamedElement +import com.intellij.psi.util.PsiLiteralUtil.* import kotlinx.coroutines.async import kotlinx.coroutines.coroutineScope import kotlinx.coroutines.runBlocking @@ -29,6 +30,8 @@ import org.jetbrains.dokka.transformers.sources.AsyncSourceToDocumentableTransla import org.jetbrains.dokka.utilities.DokkaLogger import org.jetbrains.dokka.utilities.parallelMap import org.jetbrains.dokka.utilities.parallelMapNotNull +import org.jetbrains.kotlin.KtNodeTypes +import org.jetbrains.dokka.model.BooleanConstant import org.jetbrains.kotlin.builtins.functions.FunctionClassDescriptor import org.jetbrains.kotlin.builtins.isBuiltinExtensionFunctionalType import org.jetbrains.kotlin.builtins.isExtensionFunctionType @@ -38,7 +41,6 @@ import org.jetbrains.kotlin.descriptors.* import org.jetbrains.kotlin.descriptors.ClassKind import org.jetbrains.kotlin.descriptors.annotations.Annotated import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor -import org.jetbrains.kotlin.idea.core.getDirectlyOverriddenDeclarations import org.jetbrains.kotlin.idea.kdoc.findKDoc import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink import org.jetbrains.kotlin.js.resolve.diagnostics.findPsi @@ -53,7 +55,6 @@ import org.jetbrains.kotlin.resolve.constants.KClassValue.Value.LocalClass import org.jetbrains.kotlin.resolve.constants.KClassValue.Value.NormalClass import org.jetbrains.kotlin.resolve.descriptorUtil.annotationClass import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameOrNull -import org.jetbrains.kotlin.resolve.descriptorUtil.overriddenTreeUniqueAsSequence import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.source.KotlinSourceElement import org.jetbrains.kotlin.resolve.source.PsiSourceElement @@ -1011,25 +1012,33 @@ private class DokkaDescriptorVisitor( if (kind != CallableMemberDescriptor.Kind.FAKE_OVERRIDE) this else overriddenDescriptors.first().getConcreteDescriptor() as T - private fun ValueParameterDescriptor.getDefaultValue(): String? = - (source as? KotlinSourceElement)?.psi?.children?.find { it is KtExpression }?.text + private fun ValueParameterDescriptor.getDefaultValue(): Expression? = + ((source as? KotlinSourceElement)?.psi as? KtParameter)?.defaultValue?.toDefaultValueExpression() - private suspend fun PropertyDescriptor.getDefaultValue(): String? = - (source as? KotlinSourceElement)?.psi?.children?.find { it is KtConstantExpression }?.text + private suspend fun PropertyDescriptor.getDefaultValue(): Expression? = + (source as? KotlinSourceElement)?.psi?.children?.filterIsInstance<KtConstantExpression>()?.firstOrNull() + ?.toDefaultValueExpression() private suspend fun ClassDescriptor.getAppliedConstructorParameters() = (source as PsiSourceElement).psi?.children?.flatMap { - it.safeAs<KtInitializerList>()?.initializersAsText().orEmpty() + it.safeAs<KtInitializerList>()?.initializersAsExpression().orEmpty() }.orEmpty() - private suspend fun KtInitializerList.initializersAsText() = + private suspend fun KtInitializerList.initializersAsExpression() = initializers.firstIsInstanceOrNull<KtCallElement>() ?.getValueArgumentsInParentheses() - ?.flatMap { it.childrenAsText() } + ?.map { it.getArgumentExpression()?.toDefaultValueExpression() ?: ComplexExpression("") } .orEmpty() - private fun ValueArgument.childrenAsText() = - this.safeAs<KtValueArgument>()?.children?.map { it.text }.orEmpty() + private fun KtExpression.toDefaultValueExpression(): Expression? = when (node?.elementType) { + KtNodeTypes.INTEGER_CONSTANT -> parseLong(node?.text)?.let { IntegerConstant(it) } + KtNodeTypes.FLOAT_CONSTANT -> if (node?.text?.toLowerCase()?.endsWith('f') == true) + parseFloat(node?.text)?.let { FloatConstant(it) } + else parseDouble(node?.text)?.let { DoubleConstant(it) } + KtNodeTypes.BOOLEAN_CONSTANT -> BooleanConstant(node?.text == "true") + KtNodeTypes.STRING_TEMPLATE -> StringConstant(node.findChildByType(KtNodeTypes.LITERAL_STRING_TEMPLATE_ENTRY)?.text.orEmpty()) + else -> node?.text?.let { ComplexExpression(it) } + } private data class ClassInfo(val ancestry: List<AncestryLevel>, val docs: SourceSetDependent<DocumentationNode>) { val supertypes: List<TypeConstructorWithKind> diff --git a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt index 2db55d45..f9bc7e26 100644 --- a/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt +++ b/plugins/base/src/main/kotlin/translators/documentables/PageContentBuilder.kt @@ -122,6 +122,13 @@ open class PageContentBuilder( header(1, text, sourceSets = sourceSets, styles = styles, extra = extra, block = block) } + fun constant(text: String) = text(text, styles = mainStyles + TokenStyle.Constant) + fun keyword(text: String) = text(text, styles = mainStyles + TokenStyle.Keyword) + fun stringLiteral(text: String) = text(text, styles = mainStyles + TokenStyle.String) + fun booleanLiteral(value: Boolean) = text(value.toString(), styles = mainStyles + TokenStyle.Boolean) + fun punctuation(text: String) = text(text, styles = mainStyles + TokenStyle.Punctuation) + fun operator(text: String) = text(text, styles = mainStyles + TokenStyle.Operator) + fun text( text: String, kind: Kind = ContentKind.Main, @@ -194,16 +201,18 @@ open class PageContentBuilder( suffix: String = "", separator: String = ", ", sourceSets: Set<DokkaSourceSet> = mainSourcesetData, // TODO: children should be aware of this platform data + surroundingCharactersStyle: Set<Style> = mainStyles, + separatorStyles: Set<Style> = mainStyles, operation: DocumentableContentBuilder.(T) -> Unit ) { if (elements.isNotEmpty()) { - if (prefix.isNotEmpty()) text(prefix, sourceSets = sourceSets) + if (prefix.isNotEmpty()) text(prefix, sourceSets = sourceSets, styles = surroundingCharactersStyle) elements.dropLast(1).forEach { operation(it) - text(separator, sourceSets = sourceSets) + text(separator, sourceSets = sourceSets, styles = separatorStyles) } operation(elements.last()) - if (suffix.isNotEmpty()) text(suffix, sourceSets = sourceSets) + if (suffix.isNotEmpty()) text(suffix, sourceSets = sourceSets, styles = surroundingCharactersStyle) } } @@ -404,11 +413,12 @@ open class PageContentBuilder( fun <T> sourceSetDependentText( value: SourceSetDependent<T>, sourceSets: Set<DokkaSourceSet> = value.keys, + styles: Set<Style> = mainStyles, transform: (T) -> String ) = value.entries.filter { it.key in sourceSets }.mapNotNull { (p, v) -> transform(v).takeIf { it.isNotBlank() }?.let { it to p } }.groupBy({ it.first }) { it.second }.forEach { - text(it.key, sourceSets = it.value.toSet()) + text(it.key, sourceSets = it.value.toSet(), styles = styles) } } |