override fun annotate()

in gdscript/src/main/kotlin/gdscript/annotator/GdMatchStmtAnnotator.kt [21:72]


    override fun annotate(element: PsiElement, holder: AnnotationHolder) {
        if (element !is GdExpr || element.parent !is GdMatchSt) return

        val id = PsiTreeUtil.getDeepestLast(element).parent
        if (id !is GdRefIdRef) return

        val rootDecl = GdClassMemberReference(id).resolveDeclaration() ?: return
        val typeHint = PsiTreeUtil.findChildrenOfType(rootDecl, GdTypeHintRef::class.java).lastOrNull() ?: return
        val enumNmi = typeHint.resolveRef() ?: return
        // val enumNmi = GdTypeHintNmReference(typeHint).resolve() ?: return
        if (enumNmi !is GdEnumDeclNmi) return

        val match: GdMatchSt = element.parent as GdMatchSt
        val usedKeys = PsiTreeUtil.findChildrenOfAnyType(match, GdPattern::class.java).map {
            if (it.text == "_") return
            it.text
        }

        val enum = PsiTreeUtil.getStubOrPsiParent(enumNmi) as GdEnumDeclTl
        val allKeys = enum.values.toMutableMap()

        var prefix = ""
        val owningClassId = GdClassUtil.getOwningClassName(enum)

        // Is it my own enum?
        if (!GdInheritanceUtil.isExtending(element, owningClassId)) {
            val myId = GdClassUtil.getFullClassId(element)
            // InnerClass enum
            if ("$myId.$owningClassId" == GdClassUtil.getFullClassId(enum)) {
                prefix = "$owningClassId."
            } else {
                // Enum of outside class
                val enumClass = GdClassUtil.getOwningClassElement(enum)
                prefix = "${GdClassUtil.getFullClassId(enumClass)}."
            }
        }

        prefix += "${enum.name}."

        usedKeys.forEach {
            if (it.startsWith(prefix)) {
                allKeys.remove(it.removePrefix(prefix))
            }
        }

        if (allKeys.isEmpty()) return
        holder
            .newAnnotationGd(element.project, HighlightSeverity.WEAK_WARNING, GdScriptBundle.message("annotator.missing.enum.options"))
            .range(element.textRange)
            .withFix(GdAddMatchBranchesFix(match, allKeys.keys.map { "$prefix$it" }.toTypedArray()))
            .create()
    }