Search in sources :

Example 26 with ASTNode

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

the class XmlAttributeImpl method setName.

@Override
public PsiElement setName(@NotNull final String nameText) throws IncorrectOperationException {
    final ASTNode name = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild(this);
    final String oldName = name.getText();
    final PomModel model = PomManager.getModel(getProject());
    final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute(nameText, "", this);
    final ASTNode newName = XmlChildRole.ATTRIBUTE_NAME_FINDER.findChild((ASTNode) attribute);
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    model.runTransaction(new PomTransactionBase(getParent(), aspect) {

        @Override
        public PomModelEvent runInner() {
            final PomModelEvent event = new PomModelEvent(model);
            PsiFile file = getContainingFile();
            XmlChangeSet xmlAspectChangeSet = new XmlAspectChangeSetImpl(model, file instanceof XmlFile ? (XmlFile) file : null);
            xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), oldName, null));
            xmlAspectChangeSet.add(new XmlAttributeSetImpl(getParent(), nameText, getValue()));
            event.registerChangeSet(model.getModelAspect(XmlAspect.class), xmlAspectChangeSet);
            CodeEditUtil.replaceChild(XmlAttributeImpl.this, name, newName);
            return event;
        }
    });
    return this;
}
Also used : XmlAttributeSetImpl(com.intellij.pom.xml.impl.events.XmlAttributeSetImpl) XmlChangeSet(com.intellij.pom.xml.XmlChangeSet) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent) XmlAspectChangeSetImpl(com.intellij.pom.xml.impl.XmlAspectChangeSetImpl) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel)

Example 27 with ASTNode

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

the class XmlAttributeValueImpl method getValue.

@Override
public String getValue() {
    // it is more correct way to strip quotes since injected xml may have quotes encoded
    String text = getText();
    ASTNode startQuote = findChildByType(XmlTokenType.XML_ATTRIBUTE_VALUE_START_DELIMITER);
    if (startQuote != null) {
        text = StringUtil.trimStart(text, startQuote.getText());
    }
    ASTNode endQuote = findChildByType(XmlTokenType.XML_ATTRIBUTE_VALUE_END_DELIMITER);
    if (endQuote != null) {
        text = StringUtil.trimEnd(text, endQuote.getText());
    }
    return text;
}
Also used : ASTNode(com.intellij.lang.ASTNode)

Example 28 with ASTNode

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

the class TagNameReference method getRangeInElement.

@Override
public TextRange getRangeInElement() {
    final ASTNode nameElement = getNameElement();
    if (nameElement == null) {
        return TextRange.EMPTY_RANGE;
    }
    int colon = nameElement.getText().indexOf(':') + 1;
    if (myStartTagFlag) {
        final int parentOffset = ((TreeElement) nameElement).getStartOffsetInParent();
        return new TextRange(parentOffset + colon, parentOffset + nameElement.getTextLength());
    } else {
        final PsiElement element = getElement();
        if (element == myNameElement)
            return new TextRange(colon, myNameElement.getTextLength());
        final int elementLength = element.getTextLength();
        int diffFromEnd = 0;
        for (ASTNode node = element.getNode().getLastChildNode(); node != nameElement && node != null; node = node.getTreePrev()) {
            diffFromEnd += node.getTextLength();
        }
        final int nameEnd = elementLength - diffFromEnd;
        return new TextRange(nameEnd - nameElement.getTextLength() + colon, nameEnd);
    }
}
Also used : ASTNode(com.intellij.lang.ASTNode) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 29 with ASTNode

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

the class XmlTextImpl method insertText.

@Override
public void insertText(String text, int displayOffset) throws IncorrectOperationException {
    if (text == null || text.isEmpty())
        return;
    final int physicalOffset = displayToPhysical(displayOffset);
    final PsiElement psiElement = findElementAt(physicalOffset);
    //if (!(psiElement instanceof XmlTokenImpl)) throw new IncorrectOperationException("Can't insert at offset: " + displayOffset);
    final IElementType elementType = psiElement != null ? psiElement.getNode().getElementType() : null;
    if (elementType == XmlTokenType.XML_DATA_CHARACTERS) {
        int insertOffset = physicalOffset - psiElement.getStartOffsetInParent();
        final String oldElementText = psiElement.getText();
        final String newElementText = oldElementText.substring(0, insertOffset) + text + oldElementText.substring(insertOffset);
        final PomModel model = PomManager.getModel(getProject());
        final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
        model.runTransaction(new PomTransactionBase(this, aspect) {

            @Override
            public PomModelEvent runInner() {
                final String oldText = getText();
                final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
                final ASTNode node = psiElement.getNode();
                final ASTNode treeNext = node.getTreeNext();
                addChildren(e, null, treeNext);
                deleteChildInternal(node);
                clearCaches();
                return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
            }
        });
    } else {
        setValue(new StringBuffer(getValue()).insert(displayOffset, text).toString());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Example 30 with ASTNode

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

the class XmlTextImpl method removeText.

@Override
public void removeText(int displayStart, int displayEnd) throws IncorrectOperationException {
    final String value = getValue();
    final int physicalStart = displayToPhysical(displayStart);
    final PsiElement psiElement = findElementAt(physicalStart);
    if (psiElement != null) {
        final IElementType elementType = psiElement.getNode().getElementType();
        final int elementDisplayEnd = physicalToDisplay(psiElement.getStartOffsetInParent() + psiElement.getTextLength());
        final int elementDisplayStart = physicalToDisplay(psiElement.getStartOffsetInParent());
        if (elementType == XmlTokenType.XML_DATA_CHARACTERS || elementType == TokenType.WHITE_SPACE) {
            if (elementDisplayEnd >= displayEnd && elementDisplayStart <= displayStart) {
                int physicalEnd = physicalStart;
                while (physicalEnd < getTextRange().getLength()) {
                    if (physicalToDisplay(physicalEnd) == displayEnd)
                        break;
                    physicalEnd++;
                }
                int removeStart = physicalStart - psiElement.getStartOffsetInParent();
                int removeEnd = physicalEnd - psiElement.getStartOffsetInParent();
                final String oldElementText = psiElement.getText();
                final String newElementText = oldElementText.substring(0, removeStart) + oldElementText.substring(removeEnd);
                final PomModel model = PomManager.getModel(getProject());
                final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
                model.runTransaction(new PomTransactionBase(this, aspect) {

                    @Override
                    public PomModelEvent runInner() throws IncorrectOperationException {
                        final String oldText = getText();
                        if (!newElementText.isEmpty()) {
                            final ASTNode e = getPolicy().encodeXmlTextContents(newElementText, XmlTextImpl.this);
                            replaceChild(psiElement.getNode(), e);
                        } else {
                            psiElement.delete();
                        }
                        clearCaches();
                        return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
                    }
                });
                return;
            }
        }
    }
    if (displayStart == 0 && displayEnd == value.length()) {
        delete();
    } else {
        setValue(new StringBuffer(getValue()).replace(displayStart, displayEnd, "").toString());
    }
}
Also used : IElementType(com.intellij.psi.tree.IElementType) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PomModelEvent(com.intellij.pom.event.PomModelEvent)

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