Search in sources :

Example 6 with PomTransactionBase

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

the class XmlTagImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull final ASTNode child) {
    final PomModel model = PomManager.getModel(getProject());
    final XmlAspect aspect = model.getModelAspect(XmlAspect.class);
    if (child.getElementType() == XmlElementType.XML_ATTRIBUTE) {
        try {
            model.runTransaction(new PomTransactionBase(this, aspect) {

                @Override
                public PomModelEvent runInner() {
                    final String name = ((XmlAttribute) child).getName();
                    XmlTagImpl.super.deleteChildInternal(child);
                    return XmlAttributeSetImpl.createXmlAttributeSet(model, XmlTagImpl.this, name, null);
                }
            });
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    } else {
        final ASTNode treePrev = child.getTreePrev();
        final ASTNode treeNext = child.getTreeNext();
        super.deleteChildInternal(child);
        if (treePrev != null && treeNext != null && treePrev.getElementType() == XmlElementType.XML_TEXT && treeNext.getElementType() == XmlElementType.XML_TEXT) {
            final XmlText prevText = (XmlText) treePrev.getPsi();
            final XmlText nextText = (XmlText) treeNext.getPsi();
            final String newValue = prevText.getValue() + nextText.getValue();
            // merging two XmlText-s should be done in one transaction to preserve smart pointers
            ChangeUtil.prepareAndRunChangeAction(new ChangeUtil.ChangeAction() {

                @Override
                public void makeChange(TreeChangeEvent destinationTreeChange) {
                    PsiElement anchor = prevText.getPrevSibling();
                    prevText.delete();
                    nextText.delete();
                    XmlText text = (XmlText) addAfter(XmlElementFactory.getInstance(getProject()).createDisplayText("x"), anchor);
                    text.setValue(newValue);
                }
            }, this);
        }
    }
}
Also used : TreeChangeEvent(com.intellij.pom.tree.events.TreeChangeEvent) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent) XmlAspect(com.intellij.pom.xml.XmlAspect) ASTNode(com.intellij.lang.ASTNode) PomModel(com.intellij.pom.PomModel) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 7 with PomTransactionBase

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

the class XmlDocumentImpl method deleteChildInternal.

@Override
public void deleteChildInternal(@NotNull final ASTNode child) {
    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.deleteChildInternal(child);
                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)

Example 8 with PomTransactionBase

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

the class PomModelImpl method reparseFile.

@Nullable
private Runnable reparseFile(@NotNull final PsiFile file, @NotNull FileElement treeElement, @NotNull CharSequence newText) {
    TextRange changedPsiRange = DocumentCommitThread.getChangedPsiRange(file, treeElement, newText);
    if (changedPsiRange == null)
        return null;
    Runnable reparseLeaf = tryReparseOneLeaf(treeElement, newText, changedPsiRange);
    if (reparseLeaf != null)
        return reparseLeaf;
    final DiffLog log = BlockSupport.getInstance(myProject).reparseRange(file, treeElement, changedPsiRange, newText, new EmptyProgressIndicator(), treeElement.getText());
    return () -> runTransaction(new PomTransactionBase(file, getModelAspect(TreeAspect.class)) {

        @Nullable
        @Override
        public PomModelEvent runInner() throws IncorrectOperationException {
            return new TreeAspectEvent(PomModelImpl.this, log.performActualPsiChange(file));
        }
    });
}
Also used : EmptyProgressIndicator(com.intellij.openapi.progress.EmptyProgressIndicator) ThrowableRunnable(com.intellij.util.ThrowableRunnable) TextRange(com.intellij.openapi.util.TextRange) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PomModelEvent(com.intellij.pom.event.PomModelEvent) TreeAspectEvent(com.intellij.pom.tree.TreeAspectEvent) DiffLog(com.intellij.psi.impl.source.text.DiffLog) Nullable(org.jetbrains.annotations.Nullable) Nullable(org.jetbrains.annotations.Nullable)

Example 9 with PomTransactionBase

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

the class ChangeUtil method prepareAndRunChangeAction.

public static void prepareAndRunChangeAction(final ChangeAction action, final TreeElement changedElement) {
    final FileElement changedFile = TreeUtil.getFileElement(changedElement);
    final PsiManager manager = changedFile.getManager();
    final PomModel model = PomManager.getModel(manager.getProject());
    final TreeAspect treeAspect = model.getModelAspect(TreeAspect.class);
    model.runTransaction(new PomTransactionBase(changedElement.getPsi(), treeAspect) {

        @Override
        public PomModelEvent runInner() {
            final PomModelEvent event = new PomModelEvent(model);
            final TreeChangeEvent destinationTreeChange = new TreeChangeEventImpl(treeAspect, changedFile);
            event.registerChangeSet(treeAspect, destinationTreeChange);
            action.makeChange(destinationTreeChange);
            changedElement.clearCaches();
            if (changedElement instanceof CompositeElement) {
                ((CompositeElement) changedElement).subtreeChanged();
            }
            return event;
        }
    });
}
Also used : TreeAspect(com.intellij.pom.tree.TreeAspect) TreeChangeEvent(com.intellij.pom.tree.events.TreeChangeEvent) TreeChangeEventImpl(com.intellij.pom.tree.events.impl.TreeChangeEventImpl) PsiManager(com.intellij.psi.PsiManager) PomModel(com.intellij.pom.PomModel) PomTransactionBase(com.intellij.pom.impl.PomTransactionBase) PomModelEvent(com.intellij.pom.event.PomModelEvent)

Example 10 with PomTransactionBase

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

the class XmlTextImpl method doSetValue.

public void doSetValue(final String s, final XmlPsiPolicy policy) 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() {
            final String oldText = getText();
            final ASTNode firstEncodedElement = policy.encodeXmlTextContents(s, XmlTextImpl.this);
            if (firstEncodedElement == null) {
                delete();
            } else {
                replaceAllChildrenToChildrenOf(firstEncodedElement.getTreeParent());
            }
            clearCaches();
            return XmlTextChangedImpl.createXmlTextChanged(model, XmlTextImpl.this, oldText);
        }
    });
}
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)

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