-
Notifications
You must be signed in to change notification settings - Fork 395
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support synthetic KDoc for enum members
- Loading branch information
Showing
13 changed files
with
96 additions
and
23 deletions.
There are no files selected for viewing
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
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
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
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
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
68 changes: 68 additions & 0 deletions
68
...src/main/kotlin/org/jetbrains/dokka/analysis/kotlin/symbols/kdoc/SyntheticKDocProvider.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,68 @@ | ||
package org.jetbrains.dokka.analysis.kotlin.symbols.kdoc | ||
|
||
import org.jetbrains.dokka.analysis.kotlin.symbols.plugin.SymbolsAnalysisPlugin | ||
import org.jetbrains.dokka.analysis.kotlin.symbols.translators.getDRIFromSymbol | ||
import org.jetbrains.dokka.analysis.markdown.jb.MarkdownParser | ||
import org.jetbrains.dokka.model.doc.DocumentationNode | ||
import org.jetbrains.kotlin.analysis.api.KtAnalysisSession | ||
import org.jetbrains.kotlin.analysis.api.symbols.* | ||
import org.jetbrains.kotlin.analysis.api.symbols.markers.KtPossibleMemberSymbol | ||
import org.jetbrains.kotlin.builtins.StandardNames | ||
|
||
private const val ENUM_ENTRIES_TEMPLATE_PATH = "/dokka/docs/kdoc/EnumEntries.kt.template" | ||
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 fun KtAnalysisSession.hasGeneratedKDocDocumentation(symbol: KtSymbol): Boolean = | ||
getDocumentationTemplatePath(symbol) != null | ||
|
||
private fun KtAnalysisSession.getDocumentationTemplatePath(symbol: KtSymbol): String? = | ||
when (symbol) { | ||
is KtPropertySymbol -> if (isEnumEntriesProperty(symbol)) ENUM_ENTRIES_TEMPLATE_PATH else null | ||
is KtFunctionSymbol -> { | ||
when { | ||
isEnumValuesMethod(symbol) -> ENUM_VALUES_TEMPLATE_PATH | ||
isEnumValueOfMethod(symbol) -> ENUM_VALUEOF_TEMPLATE_PATH | ||
else -> null | ||
} | ||
} | ||
else -> null | ||
} | ||
|
||
private fun KtAnalysisSession.isEnumSpecialMember(symbol: KtPossibleMemberSymbol): Boolean = | ||
symbol.origin == KtSymbolOrigin.SOURCE_MEMBER_GENERATED | ||
&& (symbol.getContainingSymbol() as? KtClassOrObjectSymbol)?.classKind == KtClassKind.ENUM_CLASS | ||
|
||
private fun KtAnalysisSession.isEnumEntriesProperty(symbol: KtPropertySymbol): Boolean = | ||
symbol.name == StandardNames.ENUM_ENTRIES && isEnumSpecialMember(symbol) | ||
|
||
private fun KtAnalysisSession.isEnumValuesMethod(symbol: KtFunctionSymbol): Boolean = | ||
symbol.name == StandardNames.ENUM_VALUES && isEnumSpecialMember(symbol) | ||
|
||
private fun KtAnalysisSession.isEnumValueOfMethod(symbol: KtFunctionSymbol): Boolean = | ||
symbol.name == StandardNames.ENUM_VALUE_OF && isEnumSpecialMember(symbol) | ||
|
||
internal fun KtAnalysisSession.getGeneratedKDocDocumentationFrom(symbol: KtSymbol): DocumentationNode? { | ||
val templatePath = getDocumentationTemplatePath(symbol) ?: return null | ||
return loadTemplate(symbol, templatePath) | ||
} | ||
|
||
private fun KtAnalysisSession.loadTemplate(symbol: KtSymbol, filePath: String): DocumentationNode? { | ||
val kdoc = loadContent(filePath) ?: return null | ||
val externalDriProvider = { link: String -> | ||
try { | ||
val linkedSymbol = resolveKDocLink(link, symbol) | ||
if (linkedSymbol == null) null | ||
else getDRIFromSymbol(linkedSymbol) | ||
} catch (e1: IllegalArgumentException) { | ||
/// logger.warn("Couldn't resolve link for $link") | ||
null | ||
} | ||
} | ||
|
||
val parser = MarkdownParser(externalDriProvider, filePath) | ||
return parser.parse(kdoc) | ||
} | ||
|
||
private fun loadContent(filePath: String): String? = | ||
SymbolsAnalysisPlugin::class.java.getResource(filePath)?.readText() |
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