diff options
-rw-r--r-- | src/CommentsAPI.kt | 14 | ||||
-rw-r--r-- | src/PsiAPI.kt | 19 | ||||
-rw-r--r-- | src/main.kt | 5 | ||||
-rw-r--r-- | test/playground.kt | 3 |
4 files changed, 41 insertions, 0 deletions
diff --git a/src/CommentsAPI.kt b/src/CommentsAPI.kt new file mode 100644 index 00000000..4bfb19e6 --- /dev/null +++ b/src/CommentsAPI.kt @@ -0,0 +1,14 @@ +package com.jetbrains.dokka + +import org.jetbrains.jet.lang.descriptors.* +import org.jetbrains.jet.lexer.* +import org.jetbrains.jet.lang.resolve.* +import org.jetbrains.jet.kdoc.psi.api.* +import org.jetbrains.jet.lang.psi.JetDeclaration + +fun BindingContext.getComments(descriptor: DeclarationDescriptor): KDoc? { + val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(descriptor) + if (psiElement == null) throw IllegalArgumentException("$descriptor doesn't have connection to source code, is it synthetic?") + + return psiElement.previousSiblings().takeWhile { it !is JetDeclaration }.firstOrNull { it is KDoc } as KDoc? +} diff --git a/src/PsiAPI.kt b/src/PsiAPI.kt new file mode 100644 index 00000000..9f43730b --- /dev/null +++ b/src/PsiAPI.kt @@ -0,0 +1,19 @@ +package com.jetbrains.dokka + +import com.intellij.psi.PsiElement +import kotlin.support.AbstractIterator + +fun PsiElement.previousSiblings(): Stream<PsiElement> { + var element: PsiElement? = this + return object : Stream<PsiElement> { + override fun iterator(): Iterator<PsiElement> = object : AbstractIterator<PsiElement>() { + override fun computeNext() { + element = element?.getPrevSibling() + if (element == null) + done() + else + setNext(element!!) + } + } + } +} diff --git a/src/main.kt b/src/main.kt index 8b550999..3083ba90 100644 --- a/src/main.kt +++ b/src/main.kt @@ -52,5 +52,10 @@ fun BindingContext.analyseFile(file: JetFile) { println("Package: ${packageFragment}") for (descriptor in packageFragment.getMemberScope().getAllDescriptors()) { println("Member: ${descriptor}") + val doc = getComments(descriptor) + if (doc != null) { + println("Comment: ${doc.getText()}") + } + println() } } diff --git a/test/playground.kt b/test/playground.kt index 136995d1..80992fbd 100644 --- a/test/playground.kt +++ b/test/playground.kt @@ -14,6 +14,9 @@ var topLevelVariable : String set(value) { } +/** + * This is a class + */ class Class { fun memberFunction() { } |