Search in sources :

Example 1 with XmlElementVisitor

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

the class HtmlLocalInspectionTool method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlToken(final XmlToken token) {
            IElementType tokenType = token.getTokenType();
            if (tokenType == XmlTokenType.XML_NAME || tokenType == XmlTokenType.XML_TAG_NAME) {
                PsiElement element = token.getPrevSibling();
                while (element instanceof PsiWhiteSpace) element = element.getPrevSibling();
                if (element instanceof XmlToken && ((XmlToken) element).getTokenType() == XmlTokenType.XML_START_TAG_START) {
                    PsiElement parent = element.getParent();
                    if (parent instanceof XmlTag && !(token.getNextSibling() instanceof OuterLanguageElement)) {
                        XmlTag tag = (XmlTag) parent;
                        checkTag(tag, holder, isOnTheFly);
                    }
                }
            }
        }

        @Override
        public void visitXmlAttribute(final XmlAttribute attribute) {
            checkAttribute(attribute, holder, isOnTheFly);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) IElementType(com.intellij.psi.tree.IElementType) OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) PsiWhiteSpace(com.intellij.psi.PsiWhiteSpace) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with XmlElementVisitor

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

the class CheckEmptyTagInspection method buildVisitor.

@Override
@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(final XmlTag tag) {
            if (!isTagWithEmptyEndNotAllowed(tag)) {
                return;
            }
            final ASTNode child = XmlChildRole.EMPTY_TAG_END_FINDER.findChild(tag.getNode());
            if (child == null) {
                return;
            }
            final LocalQuickFix fix = new MyLocalQuickFix();
            holder.registerProblem(tag, XmlBundle.message("html.inspections.check.empty.script.message"), tag.getContainingFile().getContext() != null ? ProblemHighlightType.INFORMATION : ProblemHighlightType.GENERIC_ERROR_OR_WARNING, fix);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) ASTNode(com.intellij.lang.ASTNode) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with XmlElementVisitor

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

the class XsltDeclarationInspection method buildVisitor.

@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(holder.getFile() instanceof XmlFile))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(final XmlTag tag) {
            final XmlAttribute nameAttr = tag.getAttribute("name", null);
            if (nameAttr == null || PsiTreeUtil.hasErrorElements(nameAttr))
                return;
            if (XsltSupport.isVariableOrParam(tag)) {
                final XsltNamedElement instance = getXsltElementFactory().wrapElement(tag, XsltNamedElement.class);
                checkDeclaration(instance, nameAttr.getValue(), holder);
            } else if (XsltSupport.isTemplate(tag)) {
                final XsltTemplate tmpl = getXsltElementFactory().wrapElement(tag, XsltTemplate.class);
                checkDeclaration(tmpl, nameAttr.getValue(), holder);
            }
        }

        private void checkDeclaration(final XsltNamedElement element, final String name, ProblemsHolder holder) {
            final XmlTag tag = element.getTag();
            final PsiElement token = element.getNameIdentifier();
            if (name == null || name.length() == 0) {
                if (token != null) {
                    holder.registerProblem(token, "Empty name not permitted");
                } else {
                    final XmlAttribute attribute = element.getNameAttribute();
                    if (attribute != null) {
                        final XmlAttributeValue e = attribute.getValueElement();
                        if (e != null) {
                            holder.registerProblem(e, "Empty name not permitted");
                        }
                    }
                }
            } else if (!isLegalName(name, holder.getManager().getProject())) {
                assert token != null;
                holder.registerProblem(token, "Illegal name");
            } else {
                assert token != null;
                final XmlFile file = (XmlFile) tag.getContainingFile();
                final XmlTag duplicatedSymbol = DeclarationChecker.getInstance(file).getDuplicatedSymbol(tag);
                if (duplicatedSymbol != null) {
                    if (duplicatedSymbol.getContainingFile() == file) {
                        holder.registerProblem(token, "Duplicate declaration");
                    } else {
                        holder.registerProblem(token, "Duplicates declaration from '" + duplicatedSymbol.getContainingFile().getName() + "'");
                    }
                }
            }
        }

        private boolean isLegalName(String value, Project project) {
            return getNamesValidator().isIdentifier(value, project);
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) Project(com.intellij.openapi.project.Project) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) XsltNamedElement(org.intellij.lang.xpath.xslt.psi.XsltNamedElement) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with XmlElementVisitor

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

the class XmlAspectImpl method update.

@Override
public void update(PomModelEvent event) {
    if (!event.getChangedAspects().contains(myTreeAspect))
        return;
    final TreeChangeEvent changeSet = (TreeChangeEvent) event.getChangeSet(myTreeAspect);
    if (changeSet == null)
        return;
    final ASTNode rootElement = changeSet.getRootElement();
    final PsiFile file = (PsiFile) rootElement.getPsi();
    if (!(file instanceof XmlFile))
        return;
    final XmlAspectChangeSetImpl xmlChangeSet = event.registerChangeSetIfAbsent(this, new XmlAspectChangeSetImpl(myModel));
    xmlChangeSet.addChangedFile((XmlFile) file);
    final ASTNode[] changedElements = changeSet.getChangedElements();
    final CharTable table = ((FileElement) changeSet.getRootElement()).getCharTable();
    for (ASTNode changedElement : changedElements) {
        TreeChange changesByElement = changeSet.getChangesByElement(changedElement);
        PsiElement psiElement = null;
        while (changedElement != null && (psiElement = changedElement.getPsi()) == null) {
            final ASTNode parent = changedElement.getTreeParent();
            final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, changedElement);
            changeInfo.compactChange(changesByElement);
            changesByElement = new TreeChangeImpl(parent);
            changesByElement.addChange(changedElement, changeInfo);
            changedElement = parent;
        }
        if (changedElement == null)
            continue;
        final TreeChange finalChangedElement = changesByElement;
        psiElement.accept(new XmlElementVisitor() {

            TreeChange myChange = finalChangedElement;

            @Override
            public void visitElement(PsiElement element) {
                final ASTNode child = element.getNode();
                final ASTNode treeParent = child.getTreeParent();
                if (treeParent == null)
                    return;
                final PsiElement parent = treeParent.getPsi();
                final ChangeInfoImpl changeInfo = ChangeInfoImpl.create(ChangeInfo.CONTENTS_CHANGED, child);
                changeInfo.compactChange(myChange);
                myChange = new TreeChangeImpl(treeParent);
                myChange.addChange(child, changeInfo);
                parent.accept(this);
            }

            @Override
            public void visitXmlAttribute(XmlAttribute attribute) {
                final ASTNode[] affectedChildren = myChange.getAffectedChildren();
                String oldName = null;
                String oldValue = null;
                for (final ASTNode treeElement : affectedChildren) {
                    final ChangeInfo changeByChild = myChange.getChangeByChild(treeElement);
                    final int changeType = changeByChild.getChangeType();
                    if (treeElement.getElementType() == XmlTokenType.XML_NAME) {
                        if (changeType == ChangeInfo.REMOVED) {
                            oldName = treeElement.getText();
                        } else if (changeType == ChangeInfo.REPLACE) {
                            oldName = ((ReplaceChangeInfo) changeByChild).getReplaced().getText();
                        }
                    }
                    if (treeElement.getElementType() == XmlElementType.XML_ATTRIBUTE_VALUE) {
                        if (changeType == ChangeInfo.REMOVED) {
                            oldValue = treeElement.getText();
                        } else if (changeType == ChangeInfo.REPLACE) {
                            oldValue = ((ReplaceChangeInfo) changeByChild).getReplaced().getText();
                        }
                    }
                }
                if (oldName != null && !oldName.equals(attribute.getName())) {
                    xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), oldName, null));
                    xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), attribute.getName(), attribute.getValue()));
                } else if (oldValue != null) {
                    xmlChangeSet.add(new XmlAttributeSetImpl(attribute.getParent(), attribute.getName(), attribute.getValue()));
                } else {
                    xmlChangeSet.add(new XmlElementChangedImpl(attribute));
                }
            }

            @Override
            public void visitXmlTag(XmlTag tag) {
                ASTNode[] affectedChildren = shortenChange(myChange.getAffectedChildren(), changeSet);
                for (final ASTNode treeElement : affectedChildren) {
                    if (!(treeElement.getPsi() instanceof XmlTagChild)) {
                        visitElement(tag);
                        return;
                    }
                }
                for (final ASTNode treeElement : affectedChildren) {
                    final ChangeInfo changeByChild = myChange.getChangeByChild(treeElement);
                    final int changeType = changeByChild.getChangeType();
                    final IElementType type = treeElement.getElementType();
                    if (type == TokenType.WHITE_SPACE)
                        continue;
                    /*
            if (type == ElementType.XML_NAME) {
              final XmlToken xmlToken = (XmlToken)((ReplaceChangeInfo)changeByChild).getReplaced();
              xmlChangeSet.add(new XmlTagNameChangedImpl(tag, xmlToken.getText()));
              continue;
            }
            */
                    final PsiElement element = treeElement.getPsi();
                    switch(changeType) {
                        case ChangeInfo.ADD:
                            xmlChangeSet.add(new XmlTagChildAddImpl(tag, (XmlTagChild) element));
                            break;
                        case ChangeInfo.REMOVED:
                            treeElement.putUserData(CharTable.CHAR_TABLE_KEY, table);
                            xmlChangeSet.add(new XmlTagChildRemovedImpl(tag, (XmlTagChild) element));
                            break;
                        case ChangeInfo.CONTENTS_CHANGED:
                            xmlChangeSet.add(new XmlTagChildChangedImpl(tag, (XmlTagChild) element));
                            break;
                        case ChangeInfo.REPLACE:
                            final PsiElement psi = ((ReplaceChangeInfo) changeByChild).getReplaced().getPsi();
                            if (psi instanceof XmlTagChild) {
                                final XmlTagChild replaced = (XmlTagChild) psi;
                                replaced.putUserData(CharTable.CHAR_TABLE_KEY, table);
                                xmlChangeSet.add(new XmlTagChildRemovedImpl(tag, replaced));
                                xmlChangeSet.add(new XmlTagChildAddImpl(tag, (XmlTagChild) element));
                            }
                            break;
                    }
                }
            }

            @Override
            public void visitXmlDocument(XmlDocument document) {
                xmlChangeSet.clear();
                xmlChangeSet.add(new XmlDocumentChangedImpl(document));
            }

            @Override
            public void visitFile(PsiFile file) {
                final XmlDocument document = ((XmlFile) file).getDocument();
                if (document != null) {
                    xmlChangeSet.clear();
                    xmlChangeSet.add(new XmlDocumentChangedImpl(document));
                }
            }
        });
    }
}
Also used : TreeChangeEvent(com.intellij.pom.tree.events.TreeChangeEvent) ChangeInfoImpl(com.intellij.pom.tree.events.impl.ChangeInfoImpl) CharTable(com.intellij.util.CharTable) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) XmlElementVisitor(com.intellij.psi.XmlElementVisitor) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) TreeChangeImpl(com.intellij.pom.tree.events.impl.TreeChangeImpl) ReplaceChangeInfo(com.intellij.pom.tree.events.ReplaceChangeInfo) ChangeInfo(com.intellij.pom.tree.events.ChangeInfo) IElementType(com.intellij.psi.tree.IElementType) TreeChange(com.intellij.pom.tree.events.TreeChange) FileElement(com.intellij.psi.impl.source.tree.FileElement)

Example 5 with XmlElementVisitor

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

the class InspectionMappingConsistencyInspection method buildVisitor.

@NotNull
@Override
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, boolean isOnTheFly, @NotNull LocalInspectionToolSession session) {
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(XmlTag tag) {
            DomElement element = DomUtil.getDomElement(tag);
            if (element instanceof Extension) {
                ExtensionPoint extensionPoint = ((Extension) element).getExtensionPoint();
                if (extensionPoint != null && InheritanceUtil.isInheritor(extensionPoint.getBeanClass().getValue(), "com.intellij.codeInspection.InspectionEP")) {
                    boolean key = tag.getAttribute("key") != null;
                    boolean groupKey = tag.getAttribute("groupKey") != null;
                    if (key) {
                        if (tag.getAttribute("bundle") == null) {
                            checkDefaultBundle(element, holder);
                        }
                    } else if (tag.getAttribute("displayName") == null) {
                        registerProblem(element, holder, "displayName or key should be specified", "displayName", "key");
                    }
                    if (groupKey) {
                        if (tag.getAttribute("bundle") == null && tag.getAttribute("groupBundle") == null) {
                            checkDefaultBundle(element, holder);
                        }
                    } else if (tag.getAttribute("groupName") == null) {
                        registerProblem(element, holder, "groupName or groupKey should be specified", "groupName", "groupKey");
                    }
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) Extension(org.jetbrains.idea.devkit.dom.Extension) DomElement(com.intellij.util.xml.DomElement) ExtensionPoint(org.jetbrains.idea.devkit.dom.ExtensionPoint) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XmlElementVisitor (com.intellij.psi.XmlElementVisitor)8 XmlTag (com.intellij.psi.xml.XmlTag)7 NotNull (org.jetbrains.annotations.NotNull)7 PsiElement (com.intellij.psi.PsiElement)5 ASTNode (com.intellij.lang.ASTNode)3 IElementType (com.intellij.psi.tree.IElementType)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 PsiFile (com.intellij.psi.PsiFile)2 XmlFile (com.intellij.psi.xml.XmlFile)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 ProblemsHolder (com.intellij.codeInspection.ProblemsHolder)1 XmlLikeFileType (com.intellij.ide.highlighter.XmlLikeFileType)1 XmlLexer (com.intellij.lexer.XmlLexer)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 TextRange (com.intellij.openapi.util.TextRange)1 ChangeInfo (com.intellij.pom.tree.events.ChangeInfo)1 ReplaceChangeInfo (com.intellij.pom.tree.events.ReplaceChangeInfo)1 TreeChange (com.intellij.pom.tree.events.TreeChange)1 TreeChangeEvent (com.intellij.pom.tree.events.TreeChangeEvent)1