Search in sources :

Example 1 with PsiElementProcessor

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

the class XmlHighlightingTest method testEntityHighlighting.

// TODO: external validator should not be lauched due to error detected after general highlighting pass!
@HighlightingFlags(HighlightingFlag.SkipExternalValidation)
public void testEntityHighlighting() throws Exception {
    doTest();
    final XmlTag rootTag = ((XmlFile) myFile).getDocument().getRootTag();
    final List<XmlEntityRef> refs = new ArrayList<>();
    XmlUtil.processXmlElements(rootTag, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull final PsiElement element) {
            if (element instanceof XmlEntityRef) {
                refs.add((XmlEntityRef) element);
            }
            return true;
        }
    }, true);
    assertEquals("Should be 2 entity refs", 2, refs.size());
    PsiReference[] entityRefs = refs.get(0).getReferences();
    assertTrue(entityRefs.length == 1 && entityRefs[0].resolve() == null);
    entityRefs = refs.get(1).getReferences();
    assertTrue(entityRefs.length == 1 && entityRefs[0].resolve() != null);
    doTest(getFullRelativeTestName("2.xml"), false, false);
}
Also used : PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Example 2 with PsiElementProcessor

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

the class XmlHighlightingTest method testXsiSchemaLocation2.

public void testXsiSchemaLocation2() throws Exception {
    doTest(new VirtualFile[] { getVirtualFile(getFullRelativeTestName()), getVirtualFile(BASE_PATH + getTestName(false) + "_1.xsd"), getVirtualFile(BASE_PATH + getTestName(false) + "_2.xsd"), getVirtualFile(BASE_PATH + getTestName(false) + "_3.xsd") }, false, false);
    XmlTag rootTag = ((XmlFile) myFile).getDocument().getRootTag();
    checkOneTagForSchemaAttribute(rootTag, "xmlns:test2", getTestName(false) + "_2.xsd");
    configureByFiles(null, getVirtualFile(BASE_PATH + getTestName(false) + "_2.xsd"), getVirtualFile(BASE_PATH + getTestName(false) + "_3.xsd"));
    rootTag = ((XmlFile) myFile).getDocument().getRootTag();
    final List<XmlTag> tags = new ArrayList<>();
    XmlUtil.processXmlElements(rootTag, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull final PsiElement element) {
            if (element instanceof XmlTag && (((XmlTag) element).getName().equals("xs:element") || ((XmlTag) element).getName().equals("xs:attribute") || ((XmlTag) element).getName().equals("xs:restriction") || ((XmlTag) element).getName().equals("xs:group") || ((XmlTag) element).getName().equals("xs:attributeGroup")) && (((XmlTag) element).getAttributeValue("type") != null || ((XmlTag) element).getAttributeValue("ref") != null || ((XmlTag) element).getAttributeValue("base") != null)) {
                tags.add((XmlTag) element);
            }
            return true;
        }
    }, true);
    assertEquals("Should be adequate number of tags", 11, tags.size());
    final String resolveFileName = getTestName(false) + "_3.xsd";
    checkOneTagForSchemaAttribute(tags.get(1), "type", resolveFileName);
    checkOneTagForSchemaAttribute(tags.get(0), "ref", resolveFileName);
    checkOneTagForSchemaAttribute(tags.get(3), "type", resolveFileName);
    checkOneTagForSchemaAttribute(tags.get(4), "base", resolveFileName);
    final String schemaFile = "XMLSchema.xsd";
    checkOneTagForSchemaAttribute(tags.get(5), "base", schemaFile);
    checkOneTagForSchemaAttribute(rootTag, "xmlns:xs", schemaFile);
    checkOneTagForSchemaAttribute(tags.get(6), "ref", resolveFileName);
    checkOneTagForSchemaAttribute(tags.get(7), "ref", resolveFileName);
    checkOneTagForSchemaAttribute(rootTag, "xmlns:test2", getTestName(false) + "_2.xsd");
    checkOneTagForSchemaAttribute(tags.get(8), "type", schemaFile);
    checkOneTagForSchemaAttribute(tags.get(9), "ref", schemaFile);
    checkOneTagForSchemaAttribute(tags.get(10), "ref", resolveFileName);
}
Also used : PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Example 3 with PsiElementProcessor

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

the class XsltCodeInsightUtil method findLastWithParam.

public static XmlTag findLastWithParam(XmlTag templateTag) {
    final XmlTag[] lastParam = new XmlTag[1];
    templateTag.processElements(new PsiElementProcessor() {

        public boolean execute(@NotNull PsiElement element) {
            if (element instanceof XmlTag) {
                if ("with-param".equals(((XmlTag) element).getLocalName())) {
                    lastParam[0] = (XmlTag) element;
                } else {
                    return false;
                }
            }
            return true;
        }
    }, templateTag);
    return lastParam[0];
}
Also used : PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 4 with PsiElementProcessor

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

the class ImportToggleAliasIntention method doInvoke.

@Override
public void doInvoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    // sanity check: isAvailable must have set it.
    final IntentionState state = IntentionState.fromContext(editor, file);
    //
    // we set in in the source
    final String target_name;
    // we replace it in the source
    final String remove_name;
    PyReferenceExpression reference = sure(state.myImportElement.getImportReferenceExpression());
    // search for references to us with the right name
    try {
        String imported_name = PyPsiUtils.toPath(reference);
        if (state.myAlias != null) {
            // have to remove alias, rename everything to original
            target_name = imported_name;
            remove_name = state.myAlias;
        } else {
            // ask for and add alias
            Application application = ApplicationManager.getApplication();
            if (application != null && !application.isUnitTestMode()) {
                String alias = Messages.showInputDialog(project, PyBundle.message("INTN.alias.for.$0.dialog.title", imported_name), "Add Alias", Messages.getQuestionIcon(), "", new InputValidator() {

                    @Override
                    public boolean checkInput(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }

                    @Override
                    public boolean canClose(String inputString) {
                        return PyNames.isIdentifier(inputString);
                    }
                });
                if (alias == null) {
                    return;
                }
                target_name = alias;
            } else {
                // test mode
                target_name = "alias";
            }
            remove_name = imported_name;
        }
        final PsiElement referee = reference.getReference().resolve();
        if (referee != null && imported_name != null) {
            final Collection<PsiReference> references = new ArrayList<>();
            final ScopeOwner scope = PsiTreeUtil.getParentOfType(state.myImportElement, ScopeOwner.class);
            PsiTreeUtil.processElements(scope, new PsiElementProcessor() {

                public boolean execute(@NotNull PsiElement element) {
                    getReferences(element);
                    if (element instanceof PyStringLiteralExpression) {
                        final PsiLanguageInjectionHost host = (PsiLanguageInjectionHost) element;
                        final List<Pair<PsiElement, TextRange>> files = InjectedLanguageManager.getInstance(project).getInjectedPsiFiles(host);
                        if (files != null) {
                            for (Pair<PsiElement, TextRange> pair : files) {
                                final PsiElement first = pair.getFirst();
                                if (first instanceof ScopeOwner) {
                                    final ScopeOwner scopeOwner = (ScopeOwner) first;
                                    PsiTreeUtil.processElements(scopeOwner, new PsiElementProcessor() {

                                        public boolean execute(@NotNull PsiElement element) {
                                            getReferences(element);
                                            return true;
                                        }
                                    });
                                }
                            }
                        }
                    }
                    return true;
                }

                private void getReferences(PsiElement element) {
                    if (element instanceof PyReferenceExpression && PsiTreeUtil.getParentOfType(element, PyImportElement.class) == null) {
                        PyReferenceExpression ref = (PyReferenceExpression) element;
                        if (remove_name.equals(PyPsiUtils.toPath(ref))) {
                            // filter out other names that might resolve to our target
                            PsiElement resolved = ref.getReference().resolve();
                            if (resolved == referee)
                                references.add(ref.getReference());
                        }
                    }
                }
            });
            // no references here is OK by us.
            if (showConflicts(project, findDefinitions(target_name, references, Collections.<PsiElement>emptySet()), target_name, null)) {
                // got conflicts
                return;
            }
            // alter the import element
            PyElementGenerator generator = PyElementGenerator.getInstance(project);
            final LanguageLevel languageLevel = LanguageLevel.forElement(state.myImportElement);
            if (state.myAlias != null) {
                // remove alias
                ASTNode node = sure(state.myImportElement.getNode());
                ASTNode parent = sure(node.getTreeParent());
                // this is the reference
                node = sure(node.getFirstChildNode());
                // things past the reference: space, 'as', and alias
                node = sure(node.getTreeNext());
                parent.removeRange(node, null);
            } else {
                // add alias
                ASTNode my_ielt_node = sure(state.myImportElement.getNode());
                PyImportElement fountain = generator.createFromText(languageLevel, PyImportElement.class, "import foo as " + target_name, new int[] { 0, 2 });
                // at import elt
                ASTNode graft_node = sure(fountain.getNode());
                // at ref
                graft_node = sure(graft_node.getFirstChildNode());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // 'as'
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // space
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
                // alias
                graft_node = sure(graft_node.getTreeNext());
                my_ielt_node.addChild((ASTNode) graft_node.clone());
            }
            // alter references
            for (PsiReference ref : references) {
                ASTNode ref_name_node = sure(sure(ref.getElement()).getNode());
                ASTNode parent = sure(ref_name_node.getTreeParent());
                ASTNode new_name_node = generator.createExpressionFromText(languageLevel, target_name).getNode();
                assert new_name_node != null;
                parent.replaceChild(ref_name_node, new_name_node);
            }
        }
    } catch (IncorrectOperationException ignored) {
        PyUtil.showBalloon(project, PyBundle.message("QFIX.action.failed"), MessageType.WARNING);
    }
}
Also used : ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) TextRange(com.intellij.openapi.util.TextRange) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) InputValidator(com.intellij.openapi.ui.InputValidator) PsiLanguageInjectionHost(com.intellij.psi.PsiLanguageInjectionHost) ASTNode(com.intellij.lang.ASTNode) ArrayList(java.util.ArrayList) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Application(com.intellij.openapi.application.Application) PsiElement(com.intellij.psi.PsiElement) Pair(com.intellij.openapi.util.Pair)

Example 5 with PsiElementProcessor

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

the class AbstractXmlBlock method collectSubTags.

private static XmlTag[] collectSubTags(final XmlElement node) {
    final List<XmlTag> result = new ArrayList<>();
    node.processElements(new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull final PsiElement element) {
            if (element instanceof XmlTag) {
                result.add((XmlTag) element);
            }
            return true;
        }
    }, node);
    return result.toArray(new XmlTag[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Aggregations

PsiElementProcessor (com.intellij.psi.search.PsiElementProcessor)37 PsiElement (com.intellij.psi.PsiElement)20 NotNull (org.jetbrains.annotations.NotNull)12 ArrayList (java.util.ArrayList)8 ASTNode (com.intellij.lang.ASTNode)4 PsiFile (com.intellij.psi.PsiFile)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)4 PsiClassListCellRenderer (com.intellij.ide.util.PsiClassListCellRenderer)3 FileType (com.intellij.openapi.fileTypes.FileType)3 TextRange (com.intellij.openapi.util.TextRange)3 VirtualFile (com.intellij.openapi.vfs.VirtualFile)3 PsiReference (com.intellij.psi.PsiReference)3 XmlFile (com.intellij.psi.xml.XmlFile)3 List (java.util.List)3 Nullable (org.jetbrains.annotations.Nullable)3 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 Project (com.intellij.openapi.project.Project)2 Ref (com.intellij.openapi.util.Ref)2 CompositePsiElement (com.intellij.psi.impl.source.tree.CompositePsiElement)2 CachedValueProvider (com.intellij.psi.util.CachedValueProvider)2