aboutsummaryrefslogtreecommitdiff
path: root/src/Analysis/CommentsAPI.kt
blob: b195107a6f66ee3254fd793e38bdb2b81291af37 (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
31
32
33
34
35
36
37
38
39
40
41
42
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.*

/**
 * Retrieves PSI elements representing documentation from the [DeclarationDescriptor]
 *
 * $$receiver: [DeclarationDescriptor] to get documentation nodes from
 */
fun DeclarationDescriptor.getDocumentationElements(): List<KDoc> {
    val psiElement = DescriptorToSourceUtils.descriptorToDeclaration(this)
    if (psiElement == null)
        return listOf()

    return psiElement.children() // visit children
            .takeWhile { it is KDoc } // all KDoc
            .map { it as KDoc } // cast
            .toList()
            .reverse() // make reversed list
}

/**
 * Extracts text from KDoc, removes comment symbols and trims whitespace
 */
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 {
        val comment = it.trim().dropWhile { it == '/' || it == '*' }
        (if (comment.endsWith("*/"))
            comment.substring(0, comment.length() - 2)
        else
            comment).trim()
    }.join("\n").trim("\n")
}