Search in sources :

Example 1 with PomModel

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

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

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

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

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

PomModel (com.intellij.pom.PomModel)17 PomTransactionBase (com.intellij.pom.impl.PomTransactionBase)14 XmlAspect (com.intellij.pom.xml.XmlAspect)14 PomModelEvent (com.intellij.pom.event.PomModelEvent)12 ASTNode (com.intellij.lang.ASTNode)10 IncorrectOperationException (com.intellij.util.IncorrectOperationException)8 TreeAspect (com.intellij.pom.tree.TreeAspect)2 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 StartupManagerImpl (com.intellij.ide.startup.impl.StartupManagerImpl)1 com.intellij.lang (com.intellij.lang)1 PsiBuilderFactoryImpl (com.intellij.lang.impl.PsiBuilderFactoryImpl)1 com.intellij.mock (com.intellij.mock)1 Disposable (com.intellij.openapi.Disposable)1 PathManagerEx (com.intellij.openapi.application.ex.PathManagerEx)1 EditorFactory (com.intellij.openapi.editor.EditorFactory)1 ExtensionPointName (com.intellij.openapi.extensions.ExtensionPointName)1 Extensions (com.intellij.openapi.extensions.Extensions)1