Search in sources :

Example 1 with PomTransactionBase

use of com.intellij.pom.impl.PomTransactionBase in project intellij-community by JetBrains.

the class XmlAttributeImpl method setValue.

@Override
public void setValue(String valueText) throws IncorrectOperationException {
    final ASTNode value = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild(this);
    final PomModel model = PomManager.getModel(getProject());
    final XmlAttribute attribute = XmlElementFactory.getInstance(getProject()).createAttribute("a", valueText, this);
    final ASTNode newValue = XmlChildRole.ATTRIBUTE_VALUE_FINDER.findChild((ASTNode) attribute);
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    model.runTransaction(new PomTransactionBase(this, aspect) {

        @Override
        public PomModelEvent runInner() {
            final XmlAttributeImpl att = XmlAttributeImpl.this;
            if (value != null) {
                if (newValue != null) {
                    att.replaceChild(value, newValue.copyElement());
                } else {
                    att.removeChild(value);
                }
            } else {
                if (newValue != null) {
                    att.addChild(newValue.getTreePrev().copyElement());
                    att.addChild(newValue.copyElement());
                }
            }
            return XmlAttributeSetImpl.createXmlAttributeSet(model, getParent(), getName(), newValue != null ? newValue.getText() : null);
        }
    });
}
Also used : 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 2 with PomTransactionBase

use of com.intellij.pom.impl.PomTransactionBase 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 3 with PomTransactionBase

use of com.intellij.pom.impl.PomTransactionBase in project intellij-community by JetBrains.

the class XmlTextImpl method insertAtOffset.

@Override
public XmlElement insertAtOffset(final XmlElement element, final int displayOffset) throws IncorrectOperationException {
    if (element instanceof XmlText) {
        insertText(((XmlText) element).getValue(), displayOffset);
    } else {
        final PomModel model = PomManager.getModel(getProject());
        final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
        model.runTransaction(new PomTransactionBase(getParent(), aspect) {

            @Override
            public PomModelEvent runInner() throws IncorrectOperationException {
                final XmlTag tag = getParentTag();
                assert tag != null;
                final XmlText rightPart = _splitText(displayOffset);
                PsiElement result;
                if (rightPart != null) {
                    result = tag.addBefore(element, rightPart);
                } else {
                    result = tag.addAfter(element, XmlTextImpl.this);
                }
                return createEvent(new XmlTagChildAddImpl(tag, (XmlTagChild) result));
            }
        });
    }
    return this;
}
Also used : XmlTagChildAddImpl(com.intellij.pom.xml.impl.events.XmlTagChildAddImpl) XmlAspect(com.intellij.pom.xml.XmlAspect) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Example 4 with PomTransactionBase

use of com.intellij.pom.impl.PomTransactionBase 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 5 with PomTransactionBase

use of com.intellij.pom.impl.PomTransactionBase 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

PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)15 PomModel (com.intellij.pom.PomModel)14 PomModelEvent (com.intellij.pom.event.PomModelEvent)13 XmlAspect (com.intellij.pom.xml.XmlAspect)13 ASTNode (com.intellij.lang.ASTNode)9 IncorrectOperationException (com.intellij.util.IncorrectOperationException)9 TreeChangeEvent (com.intellij.pom.tree.events.TreeChangeEvent)2 XmlTagChildAddImpl (com.intellij.pom.xml.impl.events.XmlTagChildAddImpl)2 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)2 IElementType (com.intellij.psi.tree.IElementType)2 Nullable (org.jetbrains.annotations.Nullable)2 EmptyProgressIndicator (com.intellij.openapi.progress.EmptyProgressIndicator)1 TextRange (com.intellij.openapi.util.TextRange)1 TreeAspect (com.intellij.pom.tree.TreeAspect)1 TreeAspectEvent (com.intellij.pom.tree.TreeAspectEvent)1 TreeChangeEventImpl (com.intellij.pom.tree.events.impl.TreeChangeEventImpl)1 XmlChangeSet (com.intellij.pom.xml.XmlChangeSet)1 XmlAspectChangeSetImpl (com.intellij.pom.xml.impl.XmlAspectChangeSetImpl)1 XmlAttributeSetImpl (com.intellij.pom.xml.impl.events.XmlAttributeSetImpl)1 XmlTextChangedImpl (com.intellij.pom.xml.impl.events.XmlTextChangedImpl)1