Search in sources :

Example 66 with IncorrectOperationException

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

the class CreateNSDeclarationIntentionFix method applyFix.

@Override
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PsiFile containingFile = descriptor.getPsiElement().getContainingFile();
    Editor editor = FileEditorManager.getInstance(project).getSelectedTextEditor();
    final PsiFile file = editor != null ? PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()) : null;
    if (file == null || !Comparing.equal(file.getVirtualFile(), containingFile.getVirtualFile()))
        return;
    try {
        invoke(project, editor, containingFile);
    } catch (IncorrectOperationException ex) {
        LOG.error(ex);
    }
}
Also used : PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor)

Example 67 with IncorrectOperationException

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

the class AddMethodQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        // there can be no name clash, else the name would have resolved, and it hasn't.
        final PsiElement problemElement = descriptor.getPsiElement();
        final PyClassType type = getClassType(problemElement);
        if (type == null)
            return;
        final PyClass cls = type.getPyClass();
        boolean callByClass = type.isDefinition();
        PyStatementList clsStmtList = cls.getStatementList();
        sure(FileModificationService.getInstance().preparePsiElementForWrite(clsStmtList));
        // try to at least match parameter count
        // TODO: get parameter style from code style
        PyFunctionBuilder builder = new PyFunctionBuilder(myIdentifier, cls);
        PsiElement pe = problemElement.getParent();
        // set to non-null to add a decorator
        String decoratorName = null;
        PyExpression[] args = PyExpression.EMPTY_ARRAY;
        if (pe instanceof PyCallExpression) {
            PyArgumentList arglist = ((PyCallExpression) pe).getArgumentList();
            if (arglist == null)
                return;
            args = arglist.getArguments();
        }
        boolean madeInstance = false;
        if (callByClass) {
            if (args.length > 0) {
                final TypeEvalContext context = TypeEvalContext.userInitiated(cls.getProject(), cls.getContainingFile());
                final PyType firstArgType = context.getType(args[0]);
                if (firstArgType instanceof PyClassType && ((PyClassType) firstArgType).getPyClass().isSubclass(cls, context)) {
                    // class, first arg ok: instance method
                    // NOTE: might use a name other than 'self', according to code style.
                    builder.parameter("self");
                    madeInstance = true;
                }
            }
            if (!madeInstance) {
                // class, first arg absent or of different type: classmethod
                // NOTE: might use a name other than 'cls', according to code style.
                builder.parameter("cls");
                decoratorName = PyNames.CLASSMETHOD;
            }
        } else {
            // instance method
            // NOTE: might use a name other than 'self', according to code style.
            builder.parameter("self");
        }
        // ClassFoo.meth(foo_instance)
        boolean skipFirst = callByClass && madeInstance;
        for (PyExpression arg : args) {
            if (skipFirst) {
                skipFirst = false;
                continue;
            }
            if (arg instanceof PyKeywordArgument) {
                // foo(bar) -> def foo(self, bar_1)
                builder.parameter(((PyKeywordArgument) arg).getKeyword());
            } else if (arg instanceof PyReferenceExpression) {
                PyReferenceExpression refex = (PyReferenceExpression) arg;
                builder.parameter(refex.getReferencedName());
            } else {
                // use a boring name
                builder.parameter("param");
            }
        }
        PyFunction method = builder.buildFunction(project, LanguageLevel.getDefault());
        if (decoratorName != null) {
            PyElementGenerator generator = PyElementGenerator.getInstance(project);
            PyDecoratorList decoratorList = generator.createFromText(LanguageLevel.getDefault(), PyDecoratorList.class, "@" + decoratorName + "\ndef foo(): pass", new int[] { 0, 0 });
            // in the very beginning
            method.addBefore(decoratorList, method.getFirstChild());
        }
        method = (PyFunction) PyUtil.addElementToStatementList(method, clsStmtList, PyNames.INIT.equals(method.getName()));
        if (myReplaceUsage) {
            showTemplateBuilder(method);
        }
    } catch (IncorrectOperationException ignored) {
        // we failed. tell about this
        PyUtil.showBalloon(project, PyBundle.message("QFIX.failed.to.add.method"), MessageType.ERROR);
    }
}
Also used : PyClassType(com.jetbrains.python.psi.types.PyClassType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) PyType(com.jetbrains.python.psi.types.PyType) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement)

Example 68 with IncorrectOperationException

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

the class AddFunctionQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    try {
        final PsiElement problemElement = descriptor.getPsiElement();
        if (!(problemElement instanceof PyQualifiedExpression))
            return;
        final PyExpression qualifier = ((PyQualifiedExpression) problemElement).getQualifier();
        if (qualifier == null)
            return;
        final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier);
        if (!(type instanceof PyModuleType))
            return;
        final PyFile file = ((PyModuleType) type).getModule();
        sure(file);
        sure(FileModificationService.getInstance().preparePsiElementForWrite(file));
        // try to at least match parameter count
        // TODO: get parameter style from code style
        PyFunctionBuilder builder = new PyFunctionBuilder(myIdentifier, problemElement);
        PsiElement problemParent = problemElement.getParent();
        if (problemParent instanceof PyCallExpression) {
            PyArgumentList arglist = ((PyCallExpression) problemParent).getArgumentList();
            if (arglist == null)
                return;
            final PyExpression[] args = arglist.getArguments();
            for (PyExpression arg : args) {
                if (arg instanceof PyKeywordArgument) {
                    // foo(bar) -> def foo(bar_1)
                    builder.parameter(((PyKeywordArgument) arg).getKeyword());
                } else if (arg instanceof PyReferenceExpression) {
                    PyReferenceExpression refex = (PyReferenceExpression) arg;
                    builder.parameter(refex.getReferencedName());
                } else {
                    // use a boring name
                    builder.parameter("param");
                }
            }
        } else if (problemParent != null) {
            for (PyInspectionExtension extension : Extensions.getExtensions(PyInspectionExtension.EP_NAME)) {
                List<String> params = extension.getFunctionParametersFromUsage(problemElement);
                if (params != null) {
                    for (String param : params) {
                        builder.parameter(param);
                    }
                    break;
                }
            }
        }
        // else: no arglist, use empty args
        PyFunction function = builder.buildFunction(project, LanguageLevel.forElement(file));
        // add to the bottom
        function = (PyFunction) file.add(function);
        showTemplateBuilder(function, file);
    } catch (IncorrectOperationException ignored) {
        // we failed. tell about this
        PyUtil.showBalloon(project, PyBundle.message("QFIX.failed.to.add.function"), MessageType.ERROR);
    }
}
Also used : PyInspectionExtension(com.jetbrains.python.inspections.PyInspectionExtension) PyType(com.jetbrains.python.psi.types.PyType) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) List(java.util.List) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PsiElement(com.intellij.psi.PsiElement) PyModuleType(com.jetbrains.python.psi.types.PyModuleType)

Example 69 with IncorrectOperationException

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

the class XmlTagTest method testXHTMLRangeMarkers2.

public void testXHTMLRangeMarkers2() throws Exception {
    XmlTag tag = createTag("file.xhtml", "<a>xyz</a>");
    PsiFile psiFile = tag.getContainingFile();
    Document document = PsiDocumentManager.getInstance(psiFile.getProject()).getDocument(psiFile);
    RangeMarker rangeMarker = document.createRangeMarker(5, 5);
    final XmlText text = (XmlText) tag.getValue().getChildren()[0];
    ApplicationManager.getApplication().runWriteAction(() -> CommandProcessor.getInstance().executeCommand(getProject(), () -> {
        try {
            text.removeText(2, 3);
        } catch (IncorrectOperationException ioe) {
        }
    }, "", null, UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION));
    assertEquals(5, rangeMarker.getStartOffset());
    assertEquals(5, rangeMarker.getEndOffset());
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) RangeMarker(com.intellij.openapi.editor.RangeMarker) Document(com.intellij.openapi.editor.Document)

Example 70 with IncorrectOperationException

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

the class AbstractCreateFormAction method createFormBody.

protected String createFormBody(@Nullable String fqn, @NonNls String formName, String layoutManager) throws IncorrectOperationException {
    String s;
    try {
        s = FileUtil.loadTextAndClose(getClass().getResourceAsStream(formName));
    } catch (IOException e) {
        throw new IncorrectOperationException(UIDesignerBundle.message("error.cannot.read", formName), (Throwable) e);
    }
    if (fqn != null) {
        s = StringUtil.replace(s, "$CLASS$", fqn);
    } else {
        s = StringUtil.replace(s, "bind-to-class=\"$CLASS$\"", "");
    }
    s = StringUtil.replace(s, "$LAYOUT$", layoutManager);
    return StringUtil.convertLineSeparators(s);
}
Also used : IncorrectOperationException(com.intellij.util.IncorrectOperationException) IOException(java.io.IOException)

Aggregations

IncorrectOperationException (com.intellij.util.IncorrectOperationException)485 Project (com.intellij.openapi.project.Project)91 NotNull (org.jetbrains.annotations.NotNull)91 Nullable (org.jetbrains.annotations.Nullable)54 PsiElement (com.intellij.psi.PsiElement)50 TextRange (com.intellij.openapi.util.TextRange)43 VirtualFile (com.intellij.openapi.vfs.VirtualFile)38 Document (com.intellij.openapi.editor.Document)37 ASTNode (com.intellij.lang.ASTNode)35 PsiFile (com.intellij.psi.PsiFile)35 Editor (com.intellij.openapi.editor.Editor)33 NonNls (org.jetbrains.annotations.NonNls)32 UsageInfo (com.intellij.usageView.UsageInfo)31 ArrayList (java.util.ArrayList)31 JavaCodeStyleManager (com.intellij.psi.codeStyle.JavaCodeStyleManager)24 IOException (java.io.IOException)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