Search in sources :

Example 6 with PsiRecursiveElementVisitor

use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-elixir by KronicDeth.

the class ModuleAttribute method annotate.

/*
     * Public Instance Methods
     */
/**
     * Annotates the specified PSI element.
     * It is guaranteed to be executed in non-reentrant fashion.
     * I.e there will be no call of this method for this instance before previous call get completed.
     * Multiple instances of the annotator might exist simultaneously, though.
     *
     * @param element to annotate.
     * @param holder  the container which receives annotations created by the plugin.
     */
@Override
public void annotate(@NotNull final PsiElement element, @NotNull final AnnotationHolder holder) {
    element.accept(new PsiRecursiveElementVisitor() {

        /*
                     *
                     * Instance Methods
                     *
                     */
        /*
                     * Public Instance Methods
                     */
        @Override
        public void visitElement(@NotNull final PsiElement element) {
            if (element instanceof AtNonNumericOperation) {
                visitMaybeUsage((AtNonNumericOperation) element);
            } else if (element instanceof AtUnqualifiedNoParenthesesCall) {
                visitDeclaration((AtUnqualifiedNoParenthesesCall) element);
            }
        }

        /*
                     * Private Instance Methods
                     */
        private void visitDeclaration(@NotNull final AtUnqualifiedNoParenthesesCall atUnqualifiedNoParenthesesCall) {
            ElixirAtIdentifier atIdentifier = atUnqualifiedNoParenthesesCall.getAtIdentifier();
            TextRange textRange = atIdentifier.getTextRange();
            String identifier = identifierName(atIdentifier);
            if (isCallbackName(identifier)) {
                highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
                highlightCallback(atUnqualifiedNoParenthesesCall, holder);
            } else if (isDocumentationName(identifier)) {
                highlight(textRange, holder, ElixirSyntaxHighlighter.DOCUMENTATION_MODULE_ATTRIBUTE);
                highlightDocumentationText(atUnqualifiedNoParenthesesCall, holder);
            } else if (isTypeName(identifier)) {
                highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
                highlightType(atUnqualifiedNoParenthesesCall, holder);
            } else if (isSpecificationName(identifier)) {
                highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
                highlightSpecification(atUnqualifiedNoParenthesesCall, holder);
            } else {
                highlight(textRange, holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
            }
        }

        private void visitMaybeUsage(@NotNull final AtNonNumericOperation element) {
            PsiElement operand = element.operand();
            if (operand != null && !(operand instanceof ElixirAccessExpression)) {
                visitUsage(element);
            }
        }

        private void visitUsage(@NotNull final AtNonNumericOperation atNonNumericOperation) {
            highlight(atNonNumericOperation.getTextRange(), holder, ElixirSyntaxHighlighter.MODULE_ATTRIBUTE);
            if (!isNonReferencing(atNonNumericOperation) && atNonNumericOperation.getReference().resolve() == null) {
                holder.createErrorAnnotation(atNonNumericOperation, "Unresolved module attribute");
            }
        }
    });
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PsiElement (com.intellij.psi.PsiElement)6 PsiRecursiveElementVisitor (com.intellij.psi.PsiRecursiveElementVisitor)6 PsiFile (com.intellij.psi.PsiFile)3 NotNull (org.jetbrains.annotations.NotNull)2 LineRange (com.intellij.codeInsight.editorActions.moveUpDown.LineRange)1 ASTNode (com.intellij.lang.ASTNode)1 TextRange (com.intellij.openapi.util.TextRange)1 CommonCodeStyleSettings (com.intellij.psi.codeStyle.CommonCodeStyleSettings)1 XmlAttributeDecl (com.intellij.psi.xml.XmlAttributeDecl)1 XmlElementDecl (com.intellij.psi.xml.XmlElementDecl)1 ArrayList (java.util.ArrayList)1 GroovyCodeStyleSettings (org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings)1 GroovyBlock (org.jetbrains.plugins.groovy.formatter.blocks.GroovyBlock)1 GroovyFileBase (org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GrVariableDeclaration (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration)1 GrCodeBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrCodeBlock)1 GrCaseLabel (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseLabel)1 GrCaseSection (org.jetbrains.plugins.groovy.lang.psi.api.statements.clauses.GrCaseSection)1 GrTypeDefinition (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition)1