diff options
author | Ignat Beresnev <ignat.beresnev@jetbrains.com> | 2022-09-26 18:47:01 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-26 18:47:01 +0200 |
commit | 86f9559ebd40a07e996df49464fc9101dd21d3bc (patch) | |
tree | 88b91d93b49e3276b81143febb9cb381ab03b82d /plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt | |
parent | 9207f8f032fac8036c9aa5aa65633341a14efa62 (diff) | |
download | dokka-86f9559ebd40a07e996df49464fc9101dd21d3bc.tar.gz dokka-86f9559ebd40a07e996df49464fc9101dd21d3bc.tar.bz2 dokka-86f9559ebd40a07e996df49464fc9101dd21d3bc.zip |
Add documentation for synthetic Enum `values()` and `valueOf()` functions (#2650)
Diffstat (limited to 'plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt')
-rw-r--r-- | plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt b/plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt new file mode 100644 index 00000000..376c0940 --- /dev/null +++ b/plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt @@ -0,0 +1,39 @@ +package org.jetbrains.dokka.base.translators.psi + +import com.intellij.psi.* +import com.intellij.psi.javadoc.PsiDocComment +import org.jetbrains.dokka.analysis.DokkaResolutionFacade +import org.jetbrains.dokka.base.translators.psi.parsers.JavadocParser +import org.jetbrains.dokka.model.doc.DocumentationNode + +private const val ENUM_VALUEOF_TEMPLATE_PATH = "/dokka/docs/javadoc/EnumValueOf.java.template" +private const val ENUM_VALUES_TEMPLATE_PATH = "/dokka/docs/javadoc/EnumValues.java.template" + +internal class SyntheticElementDocumentationProvider( + private val javadocParser: JavadocParser, + private val resolutionFacade: DokkaResolutionFacade +) { + fun isDocumented(psiElement: PsiElement): Boolean = psiElement is PsiMethod + && (psiElement.isSyntheticEnumValuesMethod() || psiElement.isSyntheticEnumValueOfMethod()) + + fun getDocumentation(psiElement: PsiElement): DocumentationNode? { + val psiMethod = psiElement as? PsiMethod ?: return null + val templatePath = when { + psiMethod.isSyntheticEnumValuesMethod() -> ENUM_VALUES_TEMPLATE_PATH + psiMethod.isSyntheticEnumValueOfMethod() -> ENUM_VALUEOF_TEMPLATE_PATH + else -> return null + } + val docComment = loadSyntheticDoc(templatePath) ?: return null + return javadocParser.parseDocComment(docComment, psiElement) + } + + private fun loadSyntheticDoc(path: String): PsiDocComment? { + val text = javaClass.getResource(path)?.readText() ?: return null + return JavaPsiFacade.getElementFactory(resolutionFacade.project).createDocCommentFromText(text) + } +} + +private fun PsiMethod.isSyntheticEnumValuesMethod() = this.isSyntheticEnumFunction() && this.name == "values" +private fun PsiMethod.isSyntheticEnumValueOfMethod() = this.isSyntheticEnumFunction() && this.name == "valueOf" +private fun PsiMethod.isSyntheticEnumFunction() = this is SyntheticElement && this.containingClass?.isEnum == true + |