Search in sources :

Example 21 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class DefaultAddAction method performElementAddition.

@Nullable
protected T performElementAddition() {
    final DomElement parent = getParentDomElement();
    final DomManager domManager = parent.getManager();
    final TypeChooser[] oldChoosers = new TypeChooser[] { null };
    final Type[] aClass = new Type[] { null };
    final StableElement<T> result = new WriteCommandAction<StableElement<T>>(domManager.getProject(), DomUtil.getFile(parent)) {

        @Override
        protected void run(@NotNull Result<StableElement<T>> result) throws Throwable {
            final DomElement parentDomElement = getParentDomElement();
            final T t = (T) getDomCollectionChildDescription().addValue(parentDomElement, getElementType());
            tuneNewValue(t);
            aClass[0] = parent.getGenericInfo().getCollectionChildDescription(t.getXmlElementName()).getType();
            oldChoosers[0] = domManager.getTypeChooserManager().getTypeChooser(aClass[0]);
            final SmartPsiElementPointer pointer = SmartPointerManager.getInstance(getProject()).createSmartPsiElementPointer(t.getXmlTag());
            domManager.getTypeChooserManager().registerTypeChooser(aClass[0], new TypeChooser() {

                @Override
                public Type chooseType(final XmlTag tag) {
                    if (tag == pointer.getElement()) {
                        return getElementType();
                    }
                    return oldChoosers[0].chooseType(tag);
                }

                @Override
                public void distinguishTag(final XmlTag tag, final Type aClass) throws IncorrectOperationException {
                    oldChoosers[0].distinguishTag(tag, aClass);
                }

                @Override
                public Type[] getChooserTypes() {
                    return oldChoosers[0].getChooserTypes();
                }
            });
            result.setResult((StableElement<T>) t.createStableCopy());
        }
    }.execute().getResultObject();
    if (result != null) {
        domManager.getTypeChooserManager().registerTypeChooser(aClass[0], oldChoosers[0]);
        return result.getWrappedElement();
    }
    return null;
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Type(java.lang.reflect.Type) SmartPsiElementPointer(com.intellij.psi.SmartPsiElementPointer) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 22 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException in project intellij-community by JetBrains.

the class XmlAttributeValueImpl method updateText.

@Override
public PsiLanguageInjectionHost updateText(@NotNull String text) {
    try {
        final String quoteChar = getTextLength() > 0 ? getText().substring(0, 1) : "";
        String contents = StringUtil.containsAnyChar(quoteChar, "'\"") ? StringUtil.trimEnd(StringUtil.trimStart(text, quoteChar), quoteChar) : text;
        XmlAttribute newAttribute = XmlElementFactory.getInstance(getProject()).createAttribute("q", contents, this);
        XmlAttributeValue newValue = newAttribute.getValueElement();
        CheckUtil.checkWritable(this);
        replaceAllChildrenToChildrenOf(newValue.getNode());
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return this;
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue)

Example 23 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException 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 24 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException 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)

Example 25 with IncorrectOperationException

use of com.intellij.util.IncorrectOperationException 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)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)494 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 PsiElement (com.intellij.psi.PsiElement)55 Nullable (org.jetbrains.annotations.Nullable)55 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)39 Document (com.intellij.openapi.editor.Document)38 PsiFile (com.intellij.psi.PsiFile)38 ASTNode (com.intellij.lang.ASTNode)35 Editor (com.intellij.openapi.editor.Editor)33 ArrayList (java.util.ArrayList)32 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 IOException (java.io.IOException)27 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 GroovyPsiElementFactory (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElementFactory)21 XmlTag (com.intellij.psi.xml.XmlTag)20 CodeStyleManager (com.intellij.psi.codeStyle.CodeStyleManager)19 Module (com.intellij.openapi.module.Module)18