Search in sources :

Example 11 with IncorrectOperationException

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

the class AttributeChildInvocationHandler method setValue.

@Override
protected void setValue(@Nullable final String value) {
    final XmlTag tag = ensureTagExists();
    final String attributeName = getXmlElementName();
    final String namespace = getXmlApiCompatibleNamespace(getParentHandler());
    final String oldValue = StringUtil.unescapeXml(tag.getAttributeValue(attributeName, namespace));
    final String newValue = XmlStringUtil.escapeString(value);
    if (Comparing.equal(oldValue, newValue, true))
        return;
    getManager().runChange(() -> {
        try {
            XmlAttribute attribute = tag.setAttribute(attributeName, namespace, newValue);
            setXmlElement(attribute);
            getManager().cacheHandler(DomManagerImpl.DOM_ATTRIBUTE_HANDLER_KEY, attribute, this);
        } catch (IncorrectOperationException e) {
            LOG.error(e);
        }
    });
    final DomElement proxy = getProxy();
    getManager().fireEvent(oldValue != null ? new DomEvent(proxy, false) : new DomEvent(proxy, true));
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) DomElement(com.intellij.util.xml.DomElement) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) DomEvent(com.intellij.util.xml.events.DomEvent)

Example 12 with IncorrectOperationException

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

the class AttributeChildInvocationHandler method undefineInternal.

@Override
public final void undefineInternal() {
    final XmlTag tag = getXmlTag();
    if (tag != null) {
        getManager().runChange(() -> {
            try {
                setXmlElement(null);
                tag.setAttribute(getXmlElementName(), getXmlApiCompatibleNamespace(getParentHandler()), null);
            } catch (IncorrectOperationException e) {
                LOG.error(e);
            }
        });
        fireUndefinedEvent();
    }
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag)

Example 13 with IncorrectOperationException

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

the class XmlUtil method createChildTag.

/**
   * @param xmlTag
   * @param localName
   * @param namespace
   * @param bodyText              pass null to create collapsed tag, empty string means creating expanded one
   * @param enforceNamespacesDeep
   * @return
   */
public static XmlTag createChildTag(final XmlTag xmlTag, String localName, String namespace, @Nullable String bodyText, boolean enforceNamespacesDeep) {
    String qname;
    final String prefix = xmlTag.getPrefixByNamespace(namespace);
    if (prefix != null && !prefix.isEmpty()) {
        qname = prefix + ":" + localName;
    } else {
        qname = localName;
    }
    try {
        String tagStart = qname + (!StringUtil.isEmpty(namespace) && xmlTag.getPrefixByNamespace(namespace) == null && !(StringUtil.isEmpty(xmlTag.getNamespacePrefix()) && namespace.equals(xmlTag.getNamespace())) ? " xmlns=\"" + namespace + "\"" : "");
        Language language = xmlTag.getLanguage();
        if (!(language instanceof HTMLLanguage))
            language = XMLLanguage.INSTANCE;
        XmlTag retTag;
        if (bodyText != null) {
            retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + ">" + bodyText + "</" + qname + ">", language);
            if (enforceNamespacesDeep) {
                retTag.acceptChildren(new XmlRecursiveElementVisitor() {

                    @Override
                    public void visitXmlTag(XmlTag tag) {
                        final String namespacePrefix = tag.getNamespacePrefix();
                        if (namespacePrefix.isEmpty()) {
                            String qname;
                            if (prefix != null && !prefix.isEmpty()) {
                                qname = prefix + ":" + tag.getLocalName();
                            } else {
                                qname = tag.getLocalName();
                            }
                            try {
                                tag.setName(qname);
                            } catch (IncorrectOperationException e) {
                                LOG.error(e);
                            }
                        }
                        super.visitXmlTag(tag);
                    }
                });
            }
        } else {
            retTag = XmlElementFactory.getInstance(xmlTag.getProject()).createTagFromText("<" + tagStart + "/>", language);
        }
        return retTag;
    } catch (IncorrectOperationException e) {
        LOG.error(e);
    }
    return null;
}
Also used : Language(com.intellij.lang.Language) XMLLanguage(com.intellij.lang.xml.XMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) XHTMLLanguage(com.intellij.lang.xhtml.XHTMLLanguage) HTMLLanguage(com.intellij.lang.html.HTMLLanguage) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 14 with IncorrectOperationException

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

the class ConvertToJavaProcessor method performRefactoring.

//private static String
@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    final GeneratorClassNameProvider classNameProvider = new GeneratorClassNameProvider();
    ExpressionContext context = new ExpressionContext(myProject, myFiles);
    final ClassGenerator classGenerator = new ClassGenerator(classNameProvider, new ClassItemGeneratorImpl(context));
    for (GroovyFile file : myFiles) {
        final PsiClass[] classes = file.getClasses();
        StringBuilder builder = new StringBuilder();
        boolean first = true;
        for (PsiClass aClass : classes) {
            classGenerator.writeTypeDefinition(builder, aClass, true, first);
            first = false;
            builder.append('\n');
        }
        final Document document = PsiDocumentManager.getInstance(myProject).getDocument(file);
        LOG.assertTrue(document != null);
        document.setText(builder.toString());
        PsiDocumentManager.getInstance(myProject).commitDocument(document);
        String fileName = getNewFileName(file);
        PsiElement newFile;
        try {
            newFile = file.setName(fileName);
        } catch (final IncorrectOperationException e) {
            ApplicationManager.getApplication().invokeLater(() -> Messages.showMessageDialog(myProject, e.getMessage(), RefactoringBundle.message("error.title"), Messages.getErrorIcon()));
            return;
        }
        doPostProcessing(newFile);
    }
}
Also used : Document(com.intellij.openapi.editor.Document) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GroovyFile(org.jetbrains.plugins.groovy.lang.psi.GroovyFile)

Example 15 with IncorrectOperationException

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

the class ExpressionGenerator method visitSafeCastExpression.

@Override
public void visitSafeCastExpression(@NotNull GrSafeCastExpression typeCastExpression) {
    final GrExpression operand = (GrExpression) PsiUtil.skipParenthesesIfSensibly(typeCastExpression.getOperand(), false);
    final GrTypeElement typeElement = typeCastExpression.getCastTypeElement();
    if (operand instanceof GrListOrMap && ((GrListOrMap) operand).isMap() && typeElement != null) {
        AnonymousFromMapGenerator.writeAnonymousMap((GrListOrMap) operand, typeElement, builder, context);
        return;
    }
    final PsiType type = typeElement.getType();
    if (operand instanceof GrListOrMap && !((GrListOrMap) operand).isMap() && type instanceof PsiArrayType) {
        builder.append("new ");
        final GrExpression[] initializers = ((GrListOrMap) operand).getInitializers();
        if (initializers.length == 0) {
            TypeWriter.writeTypeForNew(builder, ((PsiArrayType) type).getComponentType(), typeCastExpression);
            builder.append("[0]");
        } else {
            TypeWriter.writeTypeForNew(builder, type, typeCastExpression);
            builder.append('{');
            for (GrExpression initializer : initializers) {
                initializer.accept(this);
                builder.append(", ");
            }
            if (initializers.length > 0) {
                builder.delete(builder.length() - 2, builder.length());
            //builder.removeFromTheEnd(2);
            }
            builder.append('}');
        }
        return;
    }
    final GroovyResolveResult resolveResult = PsiImplUtil.extractUniqueResult(typeCastExpression.multiResolve(false));
    final PsiElement resolved = resolveResult.getElement();
    if (resolved instanceof PsiMethod) {
        final GrExpression typeParam;
        try {
            typeParam = factory.createExpressionFromText(typeElement.getText(), typeCastExpression);
        } catch (IncorrectOperationException e) {
            generateCast(typeElement, operand);
            return;
        }
        invokeMethodOn(((PsiMethod) resolved), operand, new GrExpression[] { typeParam }, GrNamedArgument.EMPTY_ARRAY, GrClosableBlock.EMPTY_ARRAY, resolveResult.getSubstitutor(), typeCastExpression);
    } else {
        generateCast(typeElement, operand);
    }
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) IncorrectOperationException(com.intellij.util.IncorrectOperationException) GrListOrMap(org.jetbrains.plugins.groovy.lang.psi.api.auxiliary.GrListOrMap) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

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