From 71b03f6d311c6ebfdf67c593e97a7483a64844f4 Mon Sep 17 00:00:00 2001 From: Andrzej Ratajczak Date: Thu, 1 Oct 2020 16:15:14 +0200 Subject: Add parsing of package-info in java sources --- .../psi/DefaultPsiToDocumentableTranslator.kt | 39 +++++++----- .../main/kotlin/translators/psi/JavadocParser.kt | 3 +- .../DefaultPsiToDocumentableTranslatorTest.kt | 71 ++++++++++++++++++---- 3 files changed, 86 insertions(+), 27 deletions(-) (limited to 'plugins') diff --git a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt index d67bd9f5..39cb3c89 100644 --- a/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt +++ b/plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt @@ -7,6 +7,7 @@ import com.intellij.openapi.vfs.VirtualFileManager import com.intellij.psi.* import com.intellij.psi.impl.source.PsiClassReferenceType import com.intellij.psi.impl.source.PsiImmediateClassType +import com.intellij.psi.javadoc.PsiDocComment import org.jetbrains.dokka.DokkaConfiguration.DokkaSourceSet import org.jetbrains.dokka.analysis.KotlinAnalysis import org.jetbrains.dokka.analysis.PsiDocumentableSource @@ -35,6 +36,7 @@ import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.psi.psiUtil.getChildOfType import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments +import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstance import org.jetbrains.kotlin.utils.addToStdlib.safeAs import java.io.File @@ -72,19 +74,7 @@ class DefaultPsiToDocumentableTranslator( return DModule( context.configuration.moduleName, psiFiles.mapNotNull { it.safeAs() }.groupBy { it.packageName }.map { (packageName, psiFiles) -> - val dri = DRI(packageName = packageName) - DPackage( - dri, - emptyList(), - emptyList(), - psiFiles.flatMap { psiFile -> - psiFile.classes.map { docParser.parseClasslike(it, dri) } - }, - emptyList(), - emptyMap(), - null, - setOf(sourceSet) - ) + docParser.parsePackage(packageName, psiFiles) }, emptyMap(), null, @@ -127,7 +117,28 @@ class DefaultPsiToDocumentableTranslator( private fun T.toSourceSetDependent() = mapOf(sourceSetData to this) - fun parseClasslike(psi: PsiClass, parent: DRI): DClasslike = with(psi) { + fun parsePackage(packageName: String, psiFiles: List): DPackage { + val dri = DRI(packageName = packageName) + val documentation = psiFiles.firstOrNull { it.name == "package-info.java" }?.let { + javadocParser.parseDocumentation(it).toSourceSetDependent() + } ?: emptyMap() + + return DPackage( + dri, + emptyList(), + emptyList(), + psiFiles.flatMap { psiFile -> + psiFile.classes.map { parseClasslike(it, dri) } + }, + emptyList(), + documentation, + null, + setOf(sourceSetData) + ) + } + + + private fun parseClasslike(psi: PsiClass, parent: DRI): DClasslike = with(psi) { val dri = parent.withClass(name.toString()) val ancestryTree = mutableListOf() val superMethodsKeys = hashSetOf() diff --git a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt index 5baaf35a..b4663b20 100644 --- a/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt +++ b/plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt @@ -88,8 +88,7 @@ class JavadocParser( return if (indexOfSuperClass >= 0) superMethodDocumentation[indexOfSuperClass] else superMethodDocumentation.first() } - - return null + return element.children.firstIsInstanceOrNull() } /** diff --git a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt index eb682b14..5d3f7398 100644 --- a/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt +++ b/plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt @@ -1,9 +1,9 @@ package translators -import org.jetbrains.dokka.model.DModule -import org.jetbrains.dokka.model.doc.Description import org.jetbrains.dokka.model.doc.Text +import org.jetbrains.dokka.model.firstMemberOfType import org.jetbrains.dokka.testApi.testRunner.AbstractCoreTest +import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertTrue import org.junit.jupiter.api.Test @@ -22,21 +22,21 @@ class DefaultPsiToDocumentableTranslatorTest : AbstractCoreTest() { testInline( """ |/src/main/java/sample/BaseClass1.java - |package sample + |package sample; |public class BaseClass1 { | /** B1 */ | void x() { } |} | |/src/main/java/sample/BaseClass2.java - |package sample + |package sample; |public class BaseClass2 extends BaseClass1 { | /** B2 */ | void x() { } |} | |/src/main/java/sample/X.java - |package sample + |package sample; |public class X extends BaseClass2 { | void x() { } |} @@ -67,21 +67,21 @@ class DefaultPsiToDocumentableTranslatorTest : AbstractCoreTest() { testInline( """ |/src/main/java/sample/BaseClass1.java - |package sample + |package sample; |public class BaseClass1 { | /** B1 */ | void x() { } |} | |/src/main/java/sample/Interface1.java - |package sample + |package sample; |public interface Interface1 { | /** I1 */ | void x() {} |} | |/src/main/java/sample/X.java - |package sample + |package sample; |public class X extends BaseClass1 implements Interface1 { | void x() { } |} @@ -112,20 +112,20 @@ class DefaultPsiToDocumentableTranslatorTest : AbstractCoreTest() { testInline( """ |/src/main/java/sample/BaseClass1.java - |package sample + |package sample; |public class BaseClass1 { | /** B1 */ | void x() { } |} | |/src/main/java/sample/BaseClass2.java - |package sample + |package sample; |public class BaseClass2 extends BaseClass1 { | void x() {} |} | |/src/main/java/sample/X.java - |package sample + |package sample; |public class X extends BaseClass2 { | void x() { } |} @@ -141,4 +141,53 @@ class DefaultPsiToDocumentableTranslatorTest : AbstractCoreTest() { } } } + + @Test + fun `java package-info package description`() { + val configuration = dokkaConfiguration { + sourceSets { + sourceSet { + sourceRoots = listOf("src/main/java") + } + } + } + + testInline( + """ + |/src/main/java/sample/BaseClass1.java + |package sample; + |public class BaseClass1 { + | /** B1 */ + | void x() { } + |} + | + |/src/main/java/sample/BaseClass2.java + |package sample; + |public class BaseClass2 extends BaseClass1 { + | void x() {} + |} + | + |/src/main/java/sample/X.java + |package sample; + |public class X extends BaseClass2 { + | void x() { } + |} + | + |/src/main/java/sample/package-info.java + |/** + | * Here comes description from package-info + | */ + |package sample; + """.trimMargin(), + configuration + ) { + documentablesMergingStage = { module -> + val documentationOfPackage = module.packages.single().documentation.values.single().children.single() + .firstMemberOfType().body + assertEquals( + "Here comes description from package-info", documentationOfPackage + ) + } + } + } } -- cgit