Search in sources :

Example 1 with XmlToken

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

the class RemoveExtraClosingTagIntentionAction method doFix.

private static void doFix(@NotNull final PsiElement element) throws IncorrectOperationException {
    final XmlToken endNameToken = (XmlToken) element;
    final PsiElement tagElement = endNameToken.getParent();
    if (!(tagElement instanceof XmlTag) && !(tagElement instanceof PsiErrorElement))
        return;
    if (tagElement instanceof PsiErrorElement) {
        tagElement.delete();
    } else {
        final ASTNode astNode = tagElement.getNode();
        if (astNode != null) {
            final ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(astNode);
            if (endTagStart != null) {
                final Document document = PsiDocumentManager.getInstance(element.getProject()).getDocument(tagElement.getContainingFile());
                if (document != null) {
                    document.deleteString(endTagStart.getStartOffset(), tagElement.getLastChild().getTextRange().getEndOffset());
                }
            }
        }
    }
}
Also used : PsiErrorElement(com.intellij.psi.PsiErrorElement) ASTNode(com.intellij.lang.ASTNode) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with XmlToken

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

the class RemoveExtraClosingTagIntentionAction method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (!(element instanceof XmlToken))
        return;
    doFix(element);
}
Also used : PsiElement(com.intellij.psi.PsiElement) XmlToken(com.intellij.psi.xml.XmlToken)

Example 3 with XmlToken

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

the class RenameTagBeginOrEndIntentionAction method invoke.

@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file) throws IncorrectOperationException {
    final int offset = editor.getCaretModel().getOffset();
    PsiElement psiElement = file.findElementAt(offset);
    if (psiElement == null)
        return;
    if (psiElement instanceof PsiWhiteSpace)
        psiElement = PsiTreeUtil.prevLeaf(psiElement);
    if (psiElement instanceof XmlToken) {
        final IElementType tokenType = ((XmlToken) psiElement).getTokenType();
        if (tokenType != XmlTokenType.XML_NAME) {
            if (tokenType == XmlTokenType.XML_TAG_END) {
                psiElement = psiElement.getPrevSibling();
                if (psiElement == null)
                    return;
            }
        }
        PsiElement target = null;
        final String text = psiElement.getText();
        if (!myTargetName.equals(text)) {
            target = psiElement;
        } else {
            // we're in the other
            target = findOtherSide(psiElement, myStart);
        }
        if (target != null) {
            final Document document = PsiDocumentManager.getInstance(project).getDocument(file);
            if (document != null) {
                final TextRange textRange = target.getTextRange();
                document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), myTargetName);
            }
        }
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) XmlToken(com.intellij.psi.xml.XmlToken)

Example 4 with XmlToken

use of com.intellij.psi.xml.XmlToken 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 5 with XmlToken

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

the class XmlWrongClosingTagNameInspection method findEndTagName.

@Nullable
static XmlToken findEndTagName(@Nullable final PsiErrorElement element) {
    if (element == null)
        return null;
    final ASTNode astNode = element.getNode();
    if (astNode == null)
        return null;
    ASTNode current = astNode.getLastChildNode();
    ASTNode prev = current;
    while (current != null) {
        final IElementType elementType = prev.getElementType();
        if ((elementType == XmlTokenType.XML_NAME || elementType == XmlTokenType.XML_TAG_NAME) && current.getElementType() == XmlTokenType.XML_END_TAG_START) {
            return (XmlToken) prev.getPsi();
        }
        prev = current;
        current = current.getTreePrev();
    }
    return null;
}
Also used : IElementType(com.intellij.psi.tree.IElementType) ASTNode(com.intellij.lang.ASTNode) XmlToken(com.intellij.psi.xml.XmlToken) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlToken (com.intellij.psi.xml.XmlToken)29 PsiElement (com.intellij.psi.PsiElement)18 XmlTag (com.intellij.psi.xml.XmlTag)12 TextRange (com.intellij.openapi.util.TextRange)6 IElementType (com.intellij.psi.tree.IElementType)6 Nullable (org.jetbrains.annotations.Nullable)5 NotNull (org.jetbrains.annotations.NotNull)4 ASTNode (com.intellij.lang.ASTNode)3 Document (com.intellij.openapi.editor.Document)3 PsiErrorElement (com.intellij.psi.PsiErrorElement)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 ArrayList (java.util.ArrayList)3 ResourceUrl (com.android.ide.common.resources.ResourceUrl)2 ResourceType (com.android.resources.ResourceType)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 Editor (com.intellij.openapi.editor.Editor)2 Project (com.intellij.openapi.project.Project)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)2 PsiFile (com.intellij.psi.PsiFile)2 HtmlTag (com.intellij.psi.html.HtmlTag)2