-
Notifications
You must be signed in to change notification settings - Fork 411
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add documentation for synthetic Enum
values()
and valueOf()
funct…
…ions (#2650)
- Loading branch information
1 parent
9207f8f
commit 86f9559
Showing
15 changed files
with
461 additions
and
60 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
...e/src/integrationTest/kotlin/org/jetbrains/dokka/it/StdLibDocumentationIntegrationTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package org.jetbrains.dokka.it | ||
|
||
import java.net.URL | ||
import kotlin.test.Test | ||
|
||
class StdLibDocumentationIntegrationTest { | ||
|
||
/** | ||
* Documentation for Enum's synthetic values() and valueOf() functions is only present in source code, | ||
* but not present in the descriptors. However, Dokka needs to generate documentation for these functions, | ||
* so it ships with hardcoded kdoc templates. | ||
* | ||
* This test exists to make sure documentation for these hardcoded synthetic functions does not change, | ||
* and fails if it does, indicating that it needs to be updated. | ||
*/ | ||
@Test | ||
fun shouldAssertEnumDocumentationHasNotChanged() { | ||
val sourcesLink = "https://raw.githubusercontent.com/JetBrains/kotlin/master/core/builtins/native/kotlin/Enum.kt" | ||
val sources = URL(sourcesLink).readText() | ||
|
||
val expectedValuesDoc = | ||
" /**\n" + | ||
" * Returns an array containing the constants of this enum type, in the order they're declared.\n" + | ||
" * This method may be used to iterate over the constants.\n" + | ||
" * @values\n" + | ||
" */" | ||
check(sources.contains(expectedValuesDoc)) | ||
|
||
val expectedValueOfDoc = | ||
" /**\n" + | ||
" * Returns the enum constant of this type with the specified name. The string must match exactly " + | ||
"an identifier used to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.)\n" + | ||
" * @throws IllegalArgumentException if this enum type has no constant with the specified name\n" + | ||
" * @valueOf\n" + | ||
" */" | ||
check(sources.contains(expectedValueOfDoc)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
.../base/src/main/kotlin/translators/descriptors/SyntheticDescriptorDocumentationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.jetbrains.dokka.base.translators.descriptors | ||
|
||
import org.jetbrains.dokka.analysis.DokkaResolutionFacade | ||
import org.jetbrains.dokka.analysis.from | ||
import org.jetbrains.dokka.base.parsers.MarkdownParser | ||
import org.jetbrains.dokka.links.DRI | ||
import org.jetbrains.dokka.model.doc.DocumentationNode | ||
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor | ||
import org.jetbrains.kotlin.descriptors.FunctionDescriptor | ||
import org.jetbrains.kotlin.idea.kdoc.resolveKDocLink | ||
import org.jetbrains.kotlin.resolve.DescriptorFactory | ||
|
||
private const val ENUM_VALUEOF_TEMPLATE_PATH = "/dokka/docs/kdoc/EnumValueOf.kt.template" | ||
private const val ENUM_VALUES_TEMPLATE_PATH = "/dokka/docs/kdoc/EnumValues.kt.template" | ||
|
||
internal class SyntheticDescriptorDocumentationProvider( | ||
private val resolutionFacade: DokkaResolutionFacade | ||
) { | ||
fun isDocumented(descriptor: DeclarationDescriptor): Boolean = descriptor is FunctionDescriptor | ||
&& (DescriptorFactory.isEnumValuesMethod(descriptor) || DescriptorFactory.isEnumValueOfMethod(descriptor)) | ||
|
||
fun getDocumentation(descriptor: DeclarationDescriptor): DocumentationNode? { | ||
val function = descriptor as? FunctionDescriptor ?: return null | ||
return when { | ||
DescriptorFactory.isEnumValuesMethod(function) -> loadTemplate(descriptor, ENUM_VALUES_TEMPLATE_PATH) | ||
DescriptorFactory.isEnumValueOfMethod(function) -> loadTemplate(descriptor, ENUM_VALUEOF_TEMPLATE_PATH) | ||
else -> null | ||
} | ||
} | ||
|
||
private fun loadTemplate(descriptor: DeclarationDescriptor, filePath: String): DocumentationNode? { | ||
val kdoc = loadContent(filePath) ?: return null | ||
val parser = MarkdownParser({ link -> resolveLink(descriptor, link)}, filePath) | ||
return parser.parse(kdoc) | ||
} | ||
|
||
private fun loadContent(filePath: String): String? = javaClass.getResource(filePath)?.readText() | ||
|
||
private fun resolveLink(descriptor: DeclarationDescriptor, link: String): DRI? = | ||
resolveKDocLink( | ||
resolutionFacade.resolveSession.bindingContext, | ||
resolutionFacade, | ||
descriptor, | ||
null, | ||
link.split('.') | ||
).firstOrNull()?.let { DRI.from(it) } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
plugins/base/src/main/kotlin/translators/psi/SynheticElementDocumentationProvider.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
plugins/base/src/main/resources/dokka/docs/javadoc/EnumValueOf.java.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/** | ||
* Returns the enum constant of this type with the specified | ||
* name. | ||
* The string must match exactly an identifier used to declare | ||
* an enum constant in this type. (Extraneous whitespace | ||
* characters are not permitted.) | ||
* | ||
* @return the enum constant with the specified name | ||
* @throws IllegalArgumentException if this enum type has no | ||
* constant with the specified name | ||
*/ | ||
|
8 changes: 8 additions & 0 deletions
8
plugins/base/src/main/resources/dokka/docs/javadoc/EnumValues.java.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** | ||
* Returns an array containing the constants of this enum | ||
* type, in the order they're declared. This method may be | ||
* used to iterate over the constants. | ||
* | ||
* @return an array containing the constants of this enum | ||
* type, in the order they're declared | ||
*/ |
4 changes: 4 additions & 0 deletions
4
plugins/base/src/main/resources/dokka/docs/kdoc/EnumValueOf.kt.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
Returns the enum constant of this type with the specified name. The string must match exactly an identifier used | ||
to declare an enum constant in this type. (Extraneous whitespace characters are not permitted.) | ||
|
||
@throws kotlin.IllegalArgumentException if this enum type has no constant with the specified name |
3 changes: 3 additions & 0 deletions
3
plugins/base/src/main/resources/dokka/docs/kdoc/EnumValues.kt.template
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Returns an array containing the constants of this enum type, in the order they're declared. | ||
|
||
This method may be used to iterate over the constants. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.