aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTreehugger Robot <treehugger-gerrit@google.com>2019-11-20 23:16:08 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2019-11-20 23:16:08 +0000
commitff08b02f8822611ba57ebbe4a4be1660d5ea2f52 (patch)
tree28c43e81169e7d6335889a93c994ef6d6d871cb5
parentc5e0f886ce5134265cafc3267f508abdd2143763 (diff)
parent9c104fa1514ad002d9afb44d96bdcab273361c61 (diff)
downloaddokka-ff08b02f8822611ba57ebbe4a4be1660d5ea2f52.tar.gz
Merge "@sample improvements"
-rw-r--r--core/src/main/kotlin/Formats/DacHtmlFormat.kt2
-rw-r--r--core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt11
-rw-r--r--core/src/main/kotlin/Samples/DevsiteSampleProcessingService.kt52
-rw-r--r--core/src/test/kotlin/model/JavaTest.kt2
4 files changed, 65 insertions, 2 deletions
diff --git a/core/src/main/kotlin/Formats/DacHtmlFormat.kt b/core/src/main/kotlin/Formats/DacHtmlFormat.kt
index ba59957db..4e7fe3849 100644
--- a/core/src/main/kotlin/Formats/DacHtmlFormat.kt
+++ b/core/src/main/kotlin/Formats/DacHtmlFormat.kt
@@ -4,6 +4,7 @@ import com.google.inject.Inject
import com.google.inject.name.Named
import kotlinx.html.*
import org.jetbrains.dokka.*
+import org.jetbrains.dokka.Samples.DevsiteSampleProcessingService
import org.jetbrains.dokka.Utilities.firstSentence
import org.w3c.dom.html.HTMLElement
import java.lang.Math.max
@@ -900,6 +901,7 @@ class DacFormatDescriptor : JavaLayoutHtmlFormatDescriptorBase(), DefaultAnalysi
override val languageServiceClass = KotlinLanguageService::class
override val packageListServiceClass: KClass<out PackageListService> = JavaLayoutHtmlPackageListService::class
override val outputBuilderFactoryClass: KClass<out JavaLayoutHtmlFormatOutputBuilderFactory> = DevsiteLayoutHtmlFormatOutputBuilderFactoryImpl::class
+ override val sampleProcessingService = DevsiteSampleProcessingService::class
}
diff --git a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt
index 8762f29ef..38dc7dda9 100644
--- a/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt
+++ b/core/src/main/kotlin/Kotlin/DescriptorDocumentationParser.kt
@@ -71,6 +71,8 @@ class DescriptorDocumentationParser @Inject constructor(
?.resolveToDescriptorIfAny()
?: descriptor
+ // This will build the initial node for all content above the tags, however we also sometimes have @Sample
+ // tags between content, so we handle that case below
var kdocText = kdoc.getContent()
// workaround for code fence parsing problem in IJ markdown parser
if (kdocText.endsWith("```") || kdocText.endsWith("~~~")) {
@@ -83,8 +85,15 @@ class DescriptorDocumentationParser @Inject constructor(
val tags = kdoc.getTags()
tags.forEach {
when (it.knownTag) {
- KDocKnownTag.SAMPLE ->
+ KDocKnownTag.SAMPLE -> {
content.append(sampleService.resolveSample(contextDescriptor, it.getSubjectName(), it))
+ // If the sample tag has text below it, it will be considered as the child of the tag, so add it
+ val tagSubContent = it.getContent()
+ if (tagSubContent.isNotBlank()) {
+ val markdownNode = parseMarkdown(tagSubContent)
+ buildInlineContentTo(markdownNode, content, LinkResolver(linkMap, { href -> linkResolver.resolveContentLink(contextDescriptor, href) }))
+ }
+ }
KDocKnownTag.SEE ->
content.addTagToSeeAlso(contextDescriptor, it)
else -> {
diff --git a/core/src/main/kotlin/Samples/DevsiteSampleProcessingService.kt b/core/src/main/kotlin/Samples/DevsiteSampleProcessingService.kt
new file mode 100644
index 000000000..33d6cfeba
--- /dev/null
+++ b/core/src/main/kotlin/Samples/DevsiteSampleProcessingService.kt
@@ -0,0 +1,52 @@
+package org.jetbrains.dokka.Samples
+
+import com.google.inject.Inject
+import com.intellij.psi.PsiElement
+import org.jetbrains.dokka.*
+import org.jetbrains.kotlin.name.Name
+import org.jetbrains.kotlin.psi.*
+import org.jetbrains.kotlin.utils.addIfNotNull
+
+open class DevsiteSampleProcessingService
+@Inject constructor(
+ options: DocumentationOptions,
+ logger: DokkaLogger,
+ resolutionFacade: DokkaResolutionFacade
+) : DefaultSampleProcessingService(options, logger, resolutionFacade) {
+
+ override fun processImports(psiElement: PsiElement): ContentBlockCode {
+ // List of expression calls inside this sample, so we can trim the imports to only show relevant expressions
+ val sampleExpressionCalls = mutableSetOf<String>()
+ val psiFile = psiElement.containingFile
+ (psiElement as KtDeclarationWithBody).bodyExpression!!.accept(object : KtTreeVisitorVoid() {
+ override fun visitCallExpression(expression: KtCallExpression) {
+ sampleExpressionCalls.addIfNotNull(expression.calleeExpression?.text)
+ super.visitCallExpression(expression)
+ }
+ })
+ val androidxPackage = Name.identifier("androidx")
+ if (psiFile is KtFile) {
+ val filteredImports = psiFile.importList?.imports?.filter { element ->
+ val fqImportName = element.importPath?.fqName ?: return@filter false
+
+ val shortName = fqImportName.shortName().identifier
+ // Hide all non-androidx imports
+ if (!fqImportName.startsWith(androidxPackage)) return@filter false
+
+ sampleExpressionCalls.any { call ->
+ call == shortName
+ }
+ }
+
+ return ContentBlockCode("kotlin").apply {
+ filteredImports?.forEach { import ->
+ if (import != filteredImports.first()) {
+ append(ContentText("\n"))
+ }
+ append(ContentText(import.text))
+ }
+ }
+ }
+ return super.processImports(psiElement)
+ }
+}
diff --git a/core/src/test/kotlin/model/JavaTest.kt b/core/src/test/kotlin/model/JavaTest.kt
index 267d6119f..c00d8dc37 100644
--- a/core/src/test/kotlin/model/JavaTest.kt
+++ b/core/src/test/kotlin/model/JavaTest.kt
@@ -23,7 +23,7 @@ public class JavaTest {
with(content.sections[1]) {
assertEquals("Parameters", tag)
assertEquals("value", subjectName)
- assertEquals("render(Type:String,SUMMARY): is int parameter", toTestString())
+ assertEquals("render(Type:Int,SUMMARY): is int parameter", toTestString())
}
with(content.sections[2]) {
assertEquals("Author", tag)