Search in sources :

Example 31 with ASTNode

use of com.intellij.lang.ASTNode 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 32 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlTagImpl method getValueTextRanges.

@NotNull
private TextRange[] getValueTextRanges() {
    TextRange[] elements = myTextElements;
    if (elements == null) {
        List<TextRange> list = ContainerUtil.newSmartList();
        // don't use getValue().getXmlElements() because it processes includes & entities, and we only need textual AST here
        for (ASTNode child = getFirstChildNode(); child != null; child = child.getTreeNext()) {
            PsiElement psi = child.getPsi();
            if (psi instanceof XmlText) {
                list.add(TextRange.from(psi.getStartOffsetInParent(), psi.getTextLength()));
            }
        }
        myTextElements = elements = list.toArray(new TextRange[list.size()]);
    }
    return elements;
}
Also used : ASTNode(com.intellij.lang.ASTNode) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlTagImpl method addInternal.

private TreeElement addInternal(TreeElement child, ASTNode anchor, boolean before) throws IncorrectOperationException {
    final PomModel model = PomManager.getModel(getProject());
    if (anchor != null && child.getElementType() == XmlElementType.XML_TEXT) {
        XmlText psi = null;
        if (anchor.getPsi() instanceof XmlText) {
            psi = (XmlText) anchor.getPsi();
        } else {
            final ASTNode other = before ? anchor.getTreePrev() : anchor.getTreeNext();
            if (other != null && other.getPsi() instanceof XmlText) {
                before = !before;
                psi = (XmlText) other.getPsi();
            }
        }
        if (psi != null) {
            if (before) {
                psi.insertText(((XmlText) child.getPsi()).getValue(), 0);
            } else {
                psi.insertText(((XmlText) child.getPsi()).getValue(), psi.getValue().length());
            }
            return (TreeElement) psi.getNode();
        }
    }
    LOG.assertTrue(child.getPsi() instanceof XmlAttribute || child.getPsi() instanceof XmlTagChild);
    final InsertTransaction transaction;
    if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
        transaction = new InsertAttributeTransaction(child, anchor, before, model);
    } else if (anchor == null) {
        transaction = getBodyInsertTransaction(child);
    } else {
        transaction = new GenericInsertTransaction(child, anchor, before);
    }
    model.runTransaction(transaction);
    return transaction.getFirstInserted();
}
Also used : ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel)

Example 34 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlTagImpl method expandTag.

private ASTNode expandTag() throws IncorrectOperationException {
    ASTNode endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(this);
    if (endTagStart == null) {
        final XmlTagImpl tagFromText = (XmlTagImpl) XmlElementFactory.getInstance(getProject()).createTagFromText("<" + getName() + "></" + getName() + ">");
        final ASTNode startTagStart = XmlChildRole.START_TAG_END_FINDER.findChild(tagFromText);
        endTagStart = XmlChildRole.CLOSING_TAG_START_FINDER.findChild(tagFromText);
        final LeafElement emptyTagEnd = (LeafElement) XmlChildRole.EMPTY_TAG_END_FINDER.findChild(this);
        if (emptyTagEnd != null)
            removeChild(emptyTagEnd);
        addChildren(startTagStart, null, null);
    }
    return endTagStart;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 35 with ASTNode

use of com.intellij.lang.ASTNode in project intellij-community by JetBrains.

the class XmlTagImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull final ASTNode child) {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
        try {
            model.runTransaction(new PomTransactionBase(this, aspect) {

                @Override
                public PomModelEvent runInner() {
                    final String name = ((XmlAttribute) child).getName();
                    XmlTagImpl.super.deleteChildInternal(child);
                    return XmlAttributeSetImpl.createXmlAttributeSet(model, XmlTagImpl.this, name, null);
                }
            });
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    } else {
        final ASTNode treePrev = child.getTreePrev();
        final ASTNode treeNext = child.getTreeNext();
        super.deleteChildInternal(child);
        if (treePrev != null && treeNext != null && treePrev.getElementType() == XmlElementType.XML_TEXT && treeNext.getElementType() == XmlElementType.XML_TEXT) {
            final XmlText prevText = (XmlText) treePrev.getPsi();
            final XmlText nextText = (XmlText) treeNext.getPsi();
            final String newValue = prevText.getValue() + nextText.getValue();
            // merging two XmlText-s should be done in one transaction to preserve smart pointers
            ChangeUtil.prepareAndRunChangeAction(new ChangeUtil.ChangeAction() {

                @Override
                public void makeChange(TreeChangeEvent destinationTreeChange) {
                    PsiElement anchor = prevText.getPrevSibling();
                    prevText.delete();
                    nextText.delete();
                    XmlText text = (XmlText) addAfter(XmlElementFactory.getInstance(getProject()).createDisplayText("x"), anchor);
                    text.setValue(newValue);
                }
            }, this);
        }
    }
}
Also used : TreeChangeEvent(com.intellij.pom.tree.events.TreeChangeEvent) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Aggregations

ASTNode (com.intellij.lang.ASTNode)839 NotNull (org.jetbrains.annotations.NotNull)154 PsiElement (com.intellij.psi.PsiElement)152 IElementType (com.intellij.psi.tree.IElementType)152 Nullable (org.jetbrains.annotations.Nullable)113 TextRange (com.intellij.openapi.util.TextRange)97 ArrayList (java.util.ArrayList)60 PsiFile (com.intellij.psi.PsiFile)36 IncorrectOperationException (com.intellij.util.IncorrectOperationException)35 Annotation (com.intellij.lang.annotation.Annotation)25 AbstractBlock (com.intellij.psi.formatter.common.AbstractBlock)22 CharTable (com.intellij.util.CharTable)22 FileASTNode (com.intellij.lang.FileASTNode)20 FoldingDescriptor (com.intellij.lang.folding.FoldingDescriptor)20 Document (com.intellij.openapi.editor.Document)20 PsiWhiteSpace (com.intellij.psi.PsiWhiteSpace)18 CompositeElement (com.intellij.psi.impl.source.tree.CompositeElement)18 LeafPsiElement (com.intellij.psi.impl.source.tree.LeafPsiElement)18 Project (com.intellij.openapi.project.Project)17 XmlTag (com.intellij.psi.xml.XmlTag)17