Search in sources :

Example 1 with XmlTagChildAddImpl

use of com.intellij.pom.xml.impl.events.XmlTagChildAddImpl 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 2 with XmlTagChildAddImpl

use of com.intellij.pom.xml.impl.events.XmlTagChildAddImpl 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)

Aggregations

PomModel (com.intellij.pom.PomModel)2 PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)2 XmlAspect (com.intellij.pom.xml.XmlAspect)2 XmlTagChildAddImpl (com.intellij.pom.xml.impl.events.XmlTagChildAddImpl)2 ASTNode (com.intellij.lang.ASTNode)1 PomModelEvent (com.intellij.pom.event.PomModelEvent)1 XmlTextChangedImpl (com.intellij.pom.xml.impl.events.XmlTextChangedImpl)1 FileElement (com.intellij.psi.impl.source.tree.FileElement)1 LeafElement (com.intellij.psi.impl.source.tree.LeafElement)1 TreeElement (com.intellij.psi.impl.source.tree.TreeElement)1 IncorrectOperationException (com.intellij.util.IncorrectOperationException)1 Nullable (org.jetbrains.annotations.Nullable)1