Search in sources :

Example 11 with PsiElementProcessor

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

the class PropertiesFileRenameReferenceTest method testRenamePropertiesFile.

public void testRenamePropertiesFile() {
    final PsiFile[] files = myFixture.configureByFiles("i18n.properties", "MyClass.java");
    final PsiFile propertiesFile = files[0];
    final PsiFile javaSourceFile = files[1];
    myFixture.renameElement(propertiesFile, "i19n.properties");
    boolean[] found = { false };
    PsiTreeUtil.processElements(javaSourceFile, new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull PsiElement element) {
            if (PlatformPatterns.psiElement(PsiField.class).withName("BUNDLE_NAME").accepts(element)) {
                assertEquals("i19n", ((PsiLiteralExpression) ((PsiField) element).getInitializer()).getValue());
                found[0] = true;
            }
            return true;
        }
    });
    assertTrue(found[0]);
}
Also used : PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor)

Example 12 with PsiElementProcessor

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

the class XmlElementImpl method getNameFromEntityRef.

@Nullable
protected static String getNameFromEntityRef(final CompositeElement compositeElement, final IElementType xmlEntityDeclStart) {
    final ASTNode node = compositeElement.findChildByType(xmlEntityDeclStart);
    if (node == null)
        return null;
    ASTNode name = node.getTreeNext();
    if (name != null && name.getElementType() == TokenType.WHITE_SPACE) {
        name = name.getTreeNext();
    }
    if (name != null && name.getElementType() == XmlElementType.XML_ENTITY_REF) {
        final StringBuilder builder = new StringBuilder();
        ((XmlElement) name.getPsi()).processElements(new PsiElementProcessor() {

            @Override
            public boolean execute(@NotNull final PsiElement element) {
                builder.append(element.getText());
                return true;
            }
        }, name.getPsi());
        if (builder.length() > 0)
            return builder.toString();
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) XmlElement(com.intellij.psi.xml.XmlElement) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement) CompositePsiElement(com.intellij.psi.impl.source.tree.CompositePsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with PsiElementProcessor

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

the class XmlCompletionContributor method addEntityRefCompletions.

private static void addEntityRefCompletions(PsiElement context, CompletionResultSet resultSet) {
    XmlFile containingFile = null;
    XmlFile descriptorFile = null;
    final XmlTag tag = PsiTreeUtil.getParentOfType(context, XmlTag.class);
    if (tag != null) {
        containingFile = (XmlFile) tag.getContainingFile();
        descriptorFile = findDescriptorFile(tag, containingFile);
    }
    if (HtmlUtil.isHtml5Context(tag)) {
        descriptorFile = XmlUtil.findXmlFile(containingFile, Html5SchemaProvider.getCharsDtdLocation());
    } else if (tag == null) {
        final XmlDocument document = PsiTreeUtil.getParentOfType(context, XmlDocument.class);
        if (document != null) {
            containingFile = (XmlFile) document.getContainingFile();
            final FileType ft = containingFile.getFileType();
            if (HtmlUtil.isHtml5Document(document)) {
                descriptorFile = XmlUtil.findXmlFile(containingFile, Html5SchemaProvider.getCharsDtdLocation());
            } else if (ft != StdFileTypes.XML) {
                final String namespace = ft == StdFileTypes.XHTML || ft == StdFileTypes.JSPX ? XmlUtil.XHTML_URI : XmlUtil.HTML_URI;
                final XmlNSDescriptor nsDescriptor = document.getDefaultNSDescriptor(namespace, true);
                if (nsDescriptor != null) {
                    descriptorFile = nsDescriptor.getDescriptorFile();
                }
            }
        }
    }
    if (descriptorFile != null && containingFile != null) {
        final boolean acceptSystemEntities = containingFile.getFileType() == StdFileTypes.XML;
        final PsiElementProcessor processor = new PsiElementProcessor() {

            @Override
            public boolean execute(@NotNull final PsiElement element) {
                if (element instanceof XmlEntityDecl) {
                    final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element;
                    if (xmlEntityDecl.isInternalReference() || acceptSystemEntities) {
                        final LookupElementBuilder _item = buildEntityLookupItem(xmlEntityDecl);
                        if (_item != null) {
                            resultSet.addElement(_item);
                            resultSet.stopHere();
                        }
                    }
                }
                return true;
            }
        };
        XmlUtil.processXmlElements(descriptorFile, processor, true);
        if (descriptorFile != containingFile && acceptSystemEntities) {
            final XmlProlog element = containingFile.getDocument().getProlog();
            if (element != null)
                XmlUtil.processXmlElements(element, processor, true);
        }
    }
}
Also used : NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) XmlNSDescriptor(com.intellij.xml.XmlNSDescriptor) FileType(com.intellij.openapi.fileTypes.FileType) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 14 with PsiElementProcessor

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

the class DtdCompletionContributor method addEntityCompletions.

private static void addEntityCompletions(@NotNull final CompletionResultSet result, PsiElement position) {
    final PsiElementProcessor processor = new PsiElementProcessor() {

        @Override
        public boolean execute(@NotNull final PsiElement element) {
            if (element instanceof XmlEntityDecl) {
                final XmlEntityDecl xmlEntityDecl = (XmlEntityDecl) element;
                String name = xmlEntityDecl.getName();
                if (name != null && xmlEntityDecl.isInternalReference()) {
                    result.addElement(LookupElementBuilder.create(name).withInsertHandler(XmlCompletionContributor.ENTITY_INSERT_HANDLER));
                }
            }
            return true;
        }
    };
    XmlUtil.processXmlElements((XmlFile) position.getContainingFile().getOriginalFile(), processor, true);
}
Also used : XmlEntityDecl(com.intellij.psi.xml.XmlEntityDecl) NotNull(org.jetbrains.annotations.NotNull) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) PsiElement(com.intellij.psi.PsiElement)

Example 15 with PsiElementProcessor

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

the class ScriptSupportUtil method processDeclarations.

public static boolean processDeclarations(final XmlFile element, PsiScopeProcessor processor, ResolveState state, PsiElement lastParent, PsiElement place) {
    CachedValue<XmlTag[]> myCachedScriptTags = element.getUserData(CachedScriptTagsKey);
    if (myCachedScriptTags == null) {
        myCachedScriptTags = CachedValuesManager.getManager(element.getProject()).createCachedValue(() -> {
            final List<XmlTag> scriptTags = new ArrayList<>();
            final XmlDocument document = HtmlPsiUtil.getRealXmlDocument(element.getDocument());
            if (document != null) {
                PsiElementProcessor psiElementProcessor = new PsiElementProcessor() {

                    @Override
                    public boolean execute(@NotNull final PsiElement element1) {
                        if (element1 instanceof XmlTag) {
                            final XmlTag tag = (XmlTag) element1;
                            if (HtmlUtil.SCRIPT_TAG_NAME.equalsIgnoreCase(tag.getName())) {
                                final XmlElementDescriptor descriptor = tag.getDescriptor();
                                if (descriptor != null && HtmlUtil.SCRIPT_TAG_NAME.equals(descriptor.getName())) {
                                    scriptTags.add(tag);
                                }
                            }
                        }
                        return true;
                    }
                };
                XmlPsiUtil.processXmlElements(document, psiElementProcessor, true);
            }
            return new CachedValueProvider.Result<>(scriptTags.toArray(new XmlTag[scriptTags.size()]), element);
        }, false);
        element.putUserData(CachedScriptTagsKey, myCachedScriptTags);
    }
    if (ProcessingDeclarationsFlag.get() != null)
        return true;
    try {
        ProcessingDeclarationsFlag.set("");
        for (XmlTag tag : myCachedScriptTags.getValue()) {
            final XmlTagChild[] children = tag.getValue().getChildren();
            for (XmlTagChild child : children) {
                if (!child.processDeclarations(processor, state, null, place))
                    return false;
            }
            if (tag.getAttributeValue("src") != null) {
                final XmlAttribute attribute = tag.getAttribute("src", null);
                if (attribute != null) {
                    final PsiFile psiFile = FileReferenceUtil.findFile(attribute.getValueElement());
                    if (psiFile != null && psiFile.isValid()) {
                        if (!psiFile.processDeclarations(processor, state, null, place)) {
                            return false;
                        }
                    }
                }
            }
        }
    } finally {
        ProcessingDeclarationsFlag.set(null);
    }
    return true;
}
Also used : CachedValueProvider(com.intellij.psi.util.CachedValueProvider) PsiElementProcessor(com.intellij.psi.search.PsiElementProcessor) ArrayList(java.util.ArrayList) List(java.util.List) PsiFile(com.intellij.psi.PsiFile) XmlElementDescriptor(com.intellij.xml.XmlElementDescriptor) PsiElement(com.intellij.psi.PsiElement)

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