aboutsummaryrefslogtreecommitdiff
path: root/src/CommentsAPI.kt
blob: 2e574d23d9b28a7afeb0ed52f8603fe656049c52 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package org.jetbrains.dokka

import org.jetbrains.jet.lang.descriptors.*
import org.jetbrains.jet.lang.resolve.*
import org.jetbrains.jet.kdoc.psi.api.*
import org.jetbrains.jet.lang.psi.JetDeclaration

fun BindingContext.getDocumentation(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?
}

fun KDoc?.extractText(): String {
    if (this == null)
        return ""
    val text = getText()
    if (text == null)
        return ""
    val lines = text.replace("\r", "").split("\n")
    return lines.map {
        it.dropWhile { java.lang.Character.isWhitespace(it) }
                .dropWhile { it == '/' }
                .dropWhile { it == '*' }
                .dropWhile { it == '/' }
                .dropWhile { java.lang.Character.isWhitespace(it) }
    }.filter { it.any() }.join("\n")
}