Search in sources :

Example 6 with XmlElementVisitor

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

the class VariableShadowingInspection 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 XmlTag shadowedVariable = DeclarationChecker.getInstance((XmlFile) tag.getContainingFile()).getShadowedVariable(tag);
                if (shadowedVariable != null) {
                    final String innerKind = XsltSupport.isParam(tag) ? "Parameter" : "Variable";
                    final String outerKind = XsltSupport.isParam(shadowedVariable) ? "parameter" : "variable";
                    final LocalQuickFix fix1 = new RenameVariableFix(tag, "local").createQuickFix(isOnTheFly);
                    final LocalQuickFix fix2 = new RenameVariableFix(shadowedVariable, "outer").createQuickFix(isOnTheFly);
                    final XmlAttribute name = tag.getAttribute("name");
                    assert name != null;
                    final PsiElement token = XsltSupport.getAttValueToken(name);
                    assert token != null;
                    holder.registerProblem(token, innerKind + " '" + name.getValue() + "' shadows " + outerKind, AbstractFix.createFixes(fix1, fix2));
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) RenameVariableFix(org.intellij.lang.xpath.xslt.quickfix.RenameVariableFix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with XmlElementVisitor

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

the class CheckTagEmptyBodyInspection method buildVisitor.

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

        @Override
        public void visitXmlTag(final XmlTag tag) {
            if (!CheckEmptyTagInspection.isTagWithEmptyEndNotAllowed(tag)) {
                final ASTNode child = XmlChildRole.START_TAG_END_FINDER.findChild(tag.getNode());
                if (child != null) {
                    final ASTNode node = child.getTreeNext();
                    if (node != null && node.getElementType() == XmlTokenType.XML_END_TAG_START) {
                        final LocalQuickFix localQuickFix = new Fix();
                        holder.registerProblem(tag, XmlBundle.message("xml.inspections.tag.empty.body"), isCollapsibleTag(tag) ? localQuickFix : null);
                    }
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) ASTNode(com.intellij.lang.ASTNode) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with XmlElementVisitor

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

the class CheckValidXmlInScriptBodyInspectionBase method buildVisitor.

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

        @Override
        public void visitXmlTag(final XmlTag tag) {
            if (HtmlUtil.isHtmlTag(tag))
                return;
            if (HtmlUtil.SCRIPT_TAG_NAME.equals(tag.getName()) || tag instanceof HtmlTag && HtmlUtil.SCRIPT_TAG_NAME.equalsIgnoreCase(tag.getName())) {
                final PsiFile psiFile = tag.getContainingFile();
                final FileType fileType = psiFile.getFileType();
                if (fileType instanceof XmlLikeFileType) {
                    synchronized (CheckValidXmlInScriptBodyInspectionBase.class) {
                        if (myXmlLexer == null)
                            myXmlLexer = new XmlLexer();
                        final XmlTagValue tagValue = tag.getValue();
                        final String tagBodyText = tagValue.getText();
                        if (!tagBodyText.isEmpty()) {
                            myXmlLexer.start(tagBodyText);
                            while (myXmlLexer.getTokenType() != null) {
                                IElementType tokenType = myXmlLexer.getTokenType();
                                if (tokenType == XmlTokenType.XML_CDATA_START) {
                                    while (tokenType != null && tokenType != XmlTokenType.XML_CDATA_END) {
                                        myXmlLexer.advance();
                                        tokenType = myXmlLexer.getTokenType();
                                    }
                                    if (tokenType == null)
                                        break;
                                }
                                if (tokenType == XmlTokenType.XML_BAD_CHARACTER && "&".equals(TreeUtil.getTokenText(myXmlLexer)) || tokenType == XmlTokenType.XML_START_TAG_START) {
                                    final int valueStart = tagValue.getTextRange().getStartOffset();
                                    final int offset = valueStart + myXmlLexer.getTokenStart();
                                    final PsiElement psiElement = psiFile.findElementAt(offset);
                                    final TextRange elementRange = psiElement.getTextRange();
                                    final int offsetInElement = offset - elementRange.getStartOffset();
                                    holder.registerProblem(psiElement, XmlBundle.message("unescaped.xml.character"), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, createFix(psiElement, offsetInElement));
                                    int endOfElementInScriptTag = elementRange.getEndOffset() - valueStart;
                                    while (myXmlLexer.getTokenEnd() < endOfElementInScriptTag) {
                                        myXmlLexer.advance();
                                        if (myXmlLexer.getTokenType() == null)
                                            break;
                                    }
                                }
                                myXmlLexer.advance();
                            }
                        }
                    }
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) XmlTagValue(com.intellij.psi.xml.XmlTagValue) XmlLikeFileType(com.intellij.ide.highlighter.XmlLikeFileType) HtmlTag(com.intellij.psi.html.HtmlTag) TextRange(com.intellij.openapi.util.TextRange) XmlLexer(com.intellij.lexer.XmlLexer) IElementType(com.intellij.psi.tree.IElementType) FileType(com.intellij.openapi.fileTypes.FileType) XmlLikeFileType(com.intellij.ide.highlighter.XmlLikeFileType) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) 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