fun collectComments()

in gdscript/src/main/kotlin/gdscript/psi/utils/GdCommentUtil.kt [85:150]


    fun collectComments(element: PsiElement?): GdCommentModel {
        val comments = mutableListOf<String>()
        val model = GdCommentModel()

        if (element is GdClassNaming || element is PsiFile) {
            var file = element
            if (element is GdClassNaming) file = element.parent

            var child = file?.firstChild
            var isComment = false
            var newLined = false
            while (child != null) {
                if (child.elementType == GdTypes.COMMENT) {
                    if (child.text.startsWith("##")) {
                        isComment = true
                        comments.add(child.text.removePrefix("##").trim())
                        newLined = false
                    }
                } else if (child.elementType == TokenType.WHITE_SPACE) {
                    if (child.text == "\n") {
                        if (newLined) break
                        newLined = true
                    }
                } else if (isComment) {
                    break
                }
                child = child.nextSibling
            }
            comments.reverse()
        } else {
            var previous: PsiElement? = element
            while (true) {
                previous = previous?.prevCommentBlock()
                if (previous != null) {
                    val txt = previous.text
                    if (!txt.startsWith("##")) break
                    comments.add(txt.removePrefix("##").trim())
                } else break
            }
        }

        var brief = false
        comments.forEach {
            if (it.startsWith("@description")) {
                model.isDeprecated = true
            } else if (it.startsWith("@tutorial")) {
                val groups = TUTORIAL_REGEX.find(it)?.groups
                val tutorial = GdTutorial()
                if (groups?.get(2) != null) {
                    tutorial.url = groups[2]!!.value
                    tutorial.name = groups[1]?.value ?: groups[2]!!.value
                    model.tutorials.add(0, tutorial)
                }
            } else if (it.trim() == "" && model.description != "") {
                brief = true
            } else {
                if (brief) {
                    model.brief = "${it}\n${model.brief}".trim('\n')
                } else {
                    model.description = "${it}\n${model.description}".trim('\n')
                }
            }
        }

        return model
    }