Search in sources :

Example 1 with PsiRecursiveElementVisitor

use of com.intellij.psi.PsiRecursiveElementVisitor in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoRefactoringUtil method getOccurrences.

@NotNull
public static List<PsiElement> getOccurrences(@NotNull PsiElement pattern, @Nullable PsiElement context) {
    if (context == null)
        return Collections.emptyList();
    List<PsiElement> occurrences = ContainerUtil.newArrayList();
    PsiRecursiveElementVisitor visitor = new PsiRecursiveElementVisitor() {

        @Override
        public void visitElement(@NotNull PsiElement element) {
            if (PsiEquivalenceUtil.areElementsEquivalent(element, pattern)) {
                occurrences.add(element);
                return;
            }
            super.visitElement(element);
        }
    };
    context.acceptChildren(visitor);
    return occurrences;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) NotNull(org.jetbrains.annotations.NotNull) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with PsiRecursiveElementVisitor

use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.

the class XsltCodeInsightUtil method findVariableInsertionPoint.

public static XmlTag findVariableInsertionPoint(final XmlTag currentUsageTag, PsiElement usageBlock, final String referenceName, XmlTag... moreUsages) {
    // sort tags by document order
    final Set<XmlTag> usages = new TreeSet<>(POSITION_COMPARATOR);
    usages.add(currentUsageTag);
    ContainerUtil.addAll(usages, moreUsages);
    // collect all other possible unresolved references with the same name in the current template
    usageBlock.accept(new PsiRecursiveElementVisitor() {

        public void visitElement(PsiElement element) {
            if (element instanceof XPathVariableReference) {
                visitXPathVariableReference(((XPathVariableReference) element));
            } else {
                super.visitElement(element);
            }
        }

        private void visitXPathVariableReference(XPathVariableReference reference) {
            if (referenceName.equals(reference.getReferencedName())) {
                if (reference.resolve() == null) {
                    usages.add(PsiTreeUtil.getContextOfType(reference, XmlTag.class, true));
                }
            }
        }

        public void visitXmlAttribute(XmlAttribute attribute) {
            if (XsltSupport.isXPathAttribute(attribute)) {
                final PsiFile[] xpathFiles = XsltSupport.getFiles(attribute);
                for (PsiFile xpathFile : xpathFiles) {
                    xpathFile.accept(this);
                }
            }
        }
    });
    final Iterator<XmlTag> it = usages.iterator();
    final XmlTag firstUsage = it.next();
    // find broadest scope to create the variable in
    XmlTag tag = firstUsage;
    while (it.hasNext()) {
        XmlTag xmlTag = it.next();
        final PsiElement t = PsiTreeUtil.findCommonParent(tag, xmlTag);
        if (t instanceof XmlTag) {
            tag = (XmlTag) t;
        } else {
            break;
        }
    }
    // find the actual tag to create the variable before
    final XmlTag[] subTags = tag.getSubTags();
    for (XmlTag xmlTag : subTags) {
        if (xmlTag.getTextOffset() > firstUsage.getTextOffset())
            break;
        tag = xmlTag;
    }
    final XmlTag parentTag = tag.getParentTag();
    if (parentTag == null)
        return tag;
    final String parentName = parentTag.getLocalName();
    if ("apply-templates".equals(parentName) || "call-template".equals(parentName) || "when".equals(parentName) || "choose".equals(parentName)) {
        if ("when".equals(parentName))
            tag = tag.getParentTag();
        assert tag != null;
        tag = tag.getParentTag();
    }
    assert tag != null;
    return tag;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 3 with PsiRecursiveElementVisitor

use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.

the class AdvancedDtdOptions method prepareNamespaceMap.

public static Map<String, ?> prepareNamespaceMap(Project project, VirtualFile firstFile) {
    final PsiFile file = PsiManager.getInstance(project).findFile(firstFile);
    if (file == null) {
        return Collections.emptyMap();
    }
    final HashMap<String, Object> map = new LinkedHashMap<>();
    file.accept(new PsiRecursiveElementVisitor() {

        @Override
        public void visitElement(PsiElement element) {
            if (element instanceof XmlElementDecl) {
                final String s = ((XmlElementDecl) element).getName();
                if (s != null) {
                    final String[] parts = s.split(":");
                    if (parts.length > 1) {
                        map.put(XMLNS + ":" + parts[0], null);
                    }
                }
            } else if (element instanceof XmlAttributeDecl) {
                final String s = ((XmlAttributeDecl) element).getName();
                if (s != null) {
                    final String[] parts = s.split(":");
                    if (parts.length > 1) {
                        map.put(XMLNS + ":" + parts[0], null);
                    }
                }
            }
            super.visitElement(element);
        }
    });
    return map;
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) XmlAttributeDecl(com.intellij.psi.xml.XmlAttributeDecl) XmlElementDecl(com.intellij.psi.xml.XmlElementDecl) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiRecursiveElementVisitor

use of com.intellij.psi.PsiRecursiveElementVisitor in project intellij-community by JetBrains.

the class GroovyFormattingModelBuilder method createModel.

@Override
@NotNull
public FormattingModel createModel(final PsiElement element, final CodeStyleSettings settings) {
    ASTNode node = element.getNode();
    assert node != null;
    PsiFile containingFile = element.getContainingFile().getViewProvider().getPsi(GroovyLanguage.INSTANCE);
    assert containingFile != null : element.getContainingFile();
    ASTNode astNode = containingFile.getNode();
    assert astNode != null;
    CommonCodeStyleSettings groovySettings = settings.getCommonSettings(GroovyLanguage.INSTANCE);
    GroovyCodeStyleSettings customSettings = settings.getCustomSettings(GroovyCodeStyleSettings.class);
    final AlignmentProvider alignments = new AlignmentProvider();
    if (customSettings.USE_FLYING_GEESE_BRACES) {
        element.accept(new PsiRecursiveElementVisitor() {

            @Override
            public void visitElement(PsiElement element) {
                if (GeeseUtil.isClosureRBrace(element)) {
                    GeeseUtil.calculateRBraceAlignment(element, alignments);
                } else {
                    super.visitElement(element);
                }
            }
        });
    }
    final GroovyBlock block = new GroovyBlock(astNode, Indent.getAbsoluteNoneIndent(), null, new FormattingContext(groovySettings, alignments, customSettings, false));
    return new GroovyFormattingModel(containingFile, block, FormattingDocumentModelImpl.createOn(containingFile));
}
Also used : PsiRecursiveElementVisitor(com.intellij.psi.PsiRecursiveElementVisitor) GroovyBlock(org.jetbrains.plugins.groovy.formatter.blocks.GroovyBlock) GroovyCodeStyleSettings(org.jetbrains.plugins.groovy.codeStyle.GroovyCodeStyleSettings) ASTNode(com.intellij.lang.ASTNode) CommonCodeStyleSettings(com.intellij.psi.codeStyle.CommonCodeStyleSettings) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 5 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