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);
}
}
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);
}
}
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);
}
}
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());
}
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);
}
Aggregations