aboutsummaryrefslogtreecommitdiff
path: root/plugins/base
diff options
context:
space:
mode:
authorAndrzej Ratajczak <andrzej.ratajczak98@gmail.com>2020-10-01 16:15:14 +0200
committerKamil Doległo <9080183+kamildoleglo@users.noreply.github.com>2020-10-01 17:01:21 +0200
commit71b03f6d311c6ebfdf67c593e97a7483a64844f4 (patch)
tree6409cea41bdcde2a56159de86e9610e94e72375e /plugins/base
parentfbee5a49ee4c21bf0f31362bf1f9af46adba7a44 (diff)
downloaddokka-71b03f6d311c6ebfdf67c593e97a7483a64844f4.tar.gz
dokka-71b03f6d311c6ebfdf67c593e97a7483a64844f4.tar.bz2
dokka-71b03f6d311c6ebfdf67c593e97a7483a64844f4.zip
Add parsing of package-info in java sources
Diffstat (limited to 'plugins/base')
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/DefaultPsiToDocumentableTranslator.kt39
-rw-r--r--plugins/base/src/main/kotlin/translators/psi/JavadocParser.kt3
-rw-r--r--plugins/base/src/test/kotlin/translators/DefaultPsiToDocumentableTranslatorTest.kt71
3 files changed, 86 insertions, 27 deletions
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<PsiJavaFile>() }.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> T.toSourceSetDependent() = mapOf(sourceSetData to this)
- fun parseClasslike(psi: PsiClass, parent: DRI): DClasslike = with(psi) {
+ fun parsePackage(packageName: String, psiFiles: List<PsiJavaFile>): 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<AncestryLevel>()
val superMethodsKeys = hashSetOf<Int>()
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<PsiDocComment>()
}
/**
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<Text>().body
+ assertEquals(
+ "Here comes description from package-info", documentationOfPackage
+ )
+ }
+ }
+ }
}