Search in sources :

Example 11 with PomTransactionBase

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

the class XmlTextImpl method _splitText.

@Nullable
private XmlText _splitText(final int displayOffset) throws IncorrectOperationException {
    final XmlTag xmlTag = (XmlTag) getParent();
    if (displayOffset == 0)
        return this;
    final int length = getValue().length();
    if (displayOffset >= length) {
        return null;
    }
    final PomModel model = PomManager.getModel(xmlTag.getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    class MyTransaction extends PomTransactionBase {

        private XmlTextImpl myRight;

        MyTransaction() {
            super(xmlTag, aspect);
        }

        @Override
        @Nullable
        public PomModelEvent runInner() throws IncorrectOperationException {
            final String oldText = getValue();
            final int physicalOffset = displayToPhysical(displayOffset);
            PsiElement childElement = findElementAt(physicalOffset);
            if (childElement != null && childElement.getNode().getElementType() == XmlTokenType.XML_DATA_CHARACTERS) {
                FileElement holder = DummyHolderFactory.createHolder(getManager(), null).getTreeElement();
                int splitOffset = physicalOffset - childElement.getStartOffsetInParent();
                myRight = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(myRight, true);
                holder.rawAddChildren(myRight);
                PsiElement e = childElement;
                while (e != null) {
                    CodeEditUtil.setNodeGenerated(e.getNode(), true);
                    e = e.getNextSibling();
                }
                String leftText = childElement.getText().substring(0, splitOffset);
                String rightText = childElement.getText().substring(splitOffset);
                LeafElement rightElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(rightText));
                CodeEditUtil.setNodeGenerated(rightElement, true);
                LeafElement leftElement = ASTFactory.leaf(XmlTokenType.XML_DATA_CHARACTERS, holder.getCharTable().intern(leftText));
                CodeEditUtil.setNodeGenerated(leftElement, true);
                rawInsertAfterMe(myRight);
                myRight.rawAddChildren(rightElement);
                if (childElement.getNextSibling() != null) {
                    myRight.rawAddChildren((TreeElement) childElement.getNextSibling());
                }
                ((TreeElement) childElement).rawRemove();
                XmlTextImpl.this.rawAddChildren(leftElement);
            } else {
                final PsiFile containingFile = xmlTag.getContainingFile();
                final FileElement holder = DummyHolderFactory.createHolder(containingFile.getManager(), null, ((PsiFileImpl) containingFile).getTreeElement().getCharTable()).getTreeElement();
                final XmlTextImpl rightText = (XmlTextImpl) ASTFactory.composite(XmlElementType.XML_TEXT);
                CodeEditUtil.setNodeGenerated(rightText, true);
                holder.rawAddChildren(rightText);
                ((ASTNode) xmlTag).addChild(rightText, getTreeNext());
                final String value = getValue();
                setValue(value.substring(0, displayOffset));
                rightText.setValue(value.substring(displayOffset));
                CodeEditUtil.setNodeGenerated(rightText, true);
                myRight = rightText;
            }
            clearCaches();
            myRight.clearCaches();
            return createEvent(new XmlTextChangedImpl(XmlTextImpl.this, oldText), new XmlTagChildAddImpl(xmlTag, myRight));
        }

        public XmlText getResult() {
            return myRight;
        }
    }
    final MyTransaction transaction = new MyTransaction();
    model.runTransaction(transaction);
    return transaction.getResult();
}
Also used : PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) TreeElement(com.intellij.psi.impl.source.tree.TreeElement) XmlTagChildAddImpl(com.intellij.pom.xml.impl.events.XmlTagChildAddImpl) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) FileElement(com.intellij.psi.impl.source.tree.FileElement) XmlTextChangedImpl(com.intellij.pom.xml.impl.events.XmlTextChangedImpl) LeafElement(com.intellij.psi.impl.source.tree.LeafElement) Nullable(org.jetbrains.annotations.Nullable)

Example 12 with PomTransactionBase

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

the class XmlTagImpl method collapseIfEmpty.

@Override
public void collapseIfEmpty() {
    final XmlTag[] tags = getSubTags();
    if (tags.length > 0) {
        return;
    }
    final ASTNode closingName = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(this);
    final ASTNode startTagEnd = XmlChildRole.START_TAG_END_FINDER.findChild(this);
    if (closingName == null || startTagEnd == null) {
        return;
    }
    final PomModel pomModel = PomManager.getModel(getProject());
    final PomTransactionBase transaction = new PomTransactionBase(this, pomModel.getModelAspect(XmlAspect.class)) {

        @Override
        @Nullable
        public PomModelEvent runInner() {
            final ASTNode closingBracket = closingName.getTreeNext();
            removeRange(startTagEnd, closingBracket);
            final LeafElement emptyTagEnd = Factory.createSingleLeafElement(XmlTokenType.XML_EMPTY_ELEMENT_END, "/>", 0, 2, null, getManager());
            replaceChild(closingBracket, emptyTagEnd);
            return null;
        }
    };
    try {
        pomModel.runTransaction(transaction);
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
}
Also used : 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)

Example 13 with PomTransactionBase

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

the class XmlTagImpl method setName.

@Override
public PsiElement setName(@NotNull final String name) throws IncorrectOperationException {
    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 oldName = getName();
            final XmlTagImpl dummyTag = (XmlTagImpl) XmlElementFactory.getInstance(getProject()).createTagFromText(XmlTagUtil.composeTagText(name, "aa"));
            final XmlTagImpl tag = XmlTagImpl.this;
            final CharTable charTableByTree = SharedImplUtil.findCharTableByTree(tag);
            ASTNode child = XmlChildRole.START_TAG_NAME_FINDER.findChild(tag);
            LOG.assertTrue(child != null, "It seems '" + name + "' is not a valid tag name");
            TreeElement tagElement = (TreeElement) XmlChildRole.START_TAG_NAME_FINDER.findChild(dummyTag);
            LOG.assertTrue(tagElement != null, "What's wrong with it? '" + name + "'");
            tag.replaceChild(child, ChangeUtil.copyElement(tagElement, charTableByTree));
            final ASTNode childByRole = XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(tag);
            if (childByRole != null) {
                final TreeElement treeElement = (TreeElement) XmlChildRole.CLOSING_TAG_NAME_FINDER.findChild(dummyTag);
                if (treeElement != null) {
                    tag.replaceChild(childByRole, ChangeUtil.copyElement(treeElement, charTableByTree));
                }
            }
            return XmlTagNameChangedImpl.createXmlTagNameChanged(model, tag, oldName);
        }
    });
    return this;
}
Also used : 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) CharTable(com.intellij.util.CharTable)

Example 14 with PomTransactionBase

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

the class XmlDocumentImpl method addInternal.

@Override
public TreeElement addInternal(final TreeElement first, final ASTNode last, final ASTNode anchor, final Boolean before) {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    final TreeElement[] holder = new TreeElement[1];
    try {
        model.runTransaction(new PomTransactionBase(this, aspect) {

            @Override
            public PomModelEvent runInner() {
                holder[0] = XmlDocumentImpl.super.addInternal(first, last, anchor, before);
                return XmlDocumentChangedImpl.createXmlDocumentChanged(model, XmlDocumentImpl.this);
            }
        });
    } catch (IncorrectOperationException ignored) {
    }
    return holder[0];
}
Also used : XmlAspect(com.intellij.pom.xml.XmlAspect) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent) IncorrectOperationException(com.intellij.util.IncorrectOperationException) TreeElement(com.intellij.psi.impl.source.tree.TreeElement)

Example 15 with PomTransactionBase

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

the class XmlDocumentImpl method replaceChildInternal.

@Override
public void replaceChildInternal(@NotNull final ASTNode child, @NotNull final TreeElement newElement) {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    try {
        model.runTransaction(new PomTransactionBase(this, aspect) {

            @Override
            public PomModelEvent runInner() {
                XmlDocumentImpl.super.replaceChildInternal(child, newElement);
                return XmlDocumentChangedImpl.createXmlDocumentChanged(model, XmlDocumentImpl.this);
            }
        });
    } catch (IncorrectOperationException ignored) {
    }
}
Also used : XmlAspect(com.intellij.pom.xml.XmlAspect) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

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