use of com.jetbrains.python.psi.PyExpression in project intellij-community by JetBrains.
the class TransformClassicClassQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement psiElement = descriptor.getPsiElement();
psiElement = PsiTreeUtil.getParentOfType(psiElement, PyClass.class);
if (psiElement != null) {
PyClass pyClass = (PyClass) psiElement;
PyExpression[] superClassExpressions = pyClass.getSuperClassExpressions();
PyElementGenerator generator = PyElementGenerator.getInstance(project);
if (superClassExpressions.length == 0) {
pyClass.replace(generator.createFromText(LanguageLevel.getDefault(), PyClass.class, "class " + pyClass.getName() + "(" + PyNames.OBJECT + "):\n " + pyClass.getStatementList().getText()));
} else {
StringBuilder stringBuilder = new StringBuilder("class ");
stringBuilder.append(pyClass.getName()).append("(");
for (PyExpression expression : superClassExpressions) {
stringBuilder.append(expression.getText()).append(", ");
}
stringBuilder.append(PyNames.OBJECT).append(":\n ");
stringBuilder.append(pyClass.getStatementList().getText());
pyClass.replace(generator.createFromText(LanguageLevel.getDefault(), PyClass.class, stringBuilder.toString()));
}
}
}
use of com.jetbrains.python.psi.PyExpression in project intellij-community by JetBrains.
the class UnresolvedRefTrueFalseQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpression expression = elementGenerator.createExpressionFromText(newName);
myElement.replace(expression);
}
use of com.jetbrains.python.psi.PyExpression in project intellij-community by JetBrains.
the class PyChangeBaseClassQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
assert pyClass != null;
final PyArgumentList expressionList = pyClass.getSuperClassExpressionList();
if (expressionList != null && expressionList.getArguments().length != 0) {
final PyExpression argument = expressionList.getArguments()[0];
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(argument);
builder.replaceElement(argument, argument.getText());
final VirtualFile virtualFile = element.getContainingFile().getVirtualFile();
if (virtualFile != null) {
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
assert editor != null;
builder.run(editor, false);
}
}
}
use of com.jetbrains.python.psi.PyExpression in project intellij-community by JetBrains.
the class ChainedComparisonsQuickFix method getLargeRightExpression.
@Nullable
private static PyExpression getLargeRightExpression(@NotNull PyBinaryExpression expression, @NotNull Project project) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpression left = expression.getLeftExpression();
PyExpression right = expression.getRightExpression();
PsiElement operator = expression.getPsiOperator();
while (isComparisonExpression(left)) {
assert operator != null;
right = elementGenerator.createBinaryExpression(operator.getText(), ((PyBinaryExpression) left).getRightExpression(), right);
operator = ((PyBinaryExpression) left).getPsiOperator();
left = ((PyBinaryExpression) left).getLeftExpression();
}
return right;
}
use of com.jetbrains.python.psi.PyExpression in project intellij-community by JetBrains.
the class PyRemoveArgumentQuickFix method applyFix.
public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (!(element instanceof PyExpression))
return;
final PyExpression expression = (PyExpression) element;
final PsiElement nextSibling = PsiTreeUtil.skipSiblingsForward(expression, PsiWhiteSpace.class);
final PsiElement prevSibling = PsiTreeUtil.skipSiblingsBackward(expression, PsiWhiteSpace.class);
expression.delete();
if (nextSibling != null && nextSibling.getNode().getElementType().equals(PyTokenTypes.COMMA)) {
nextSibling.delete();
return;
}
if (prevSibling != null && prevSibling.getNode().getElementType().equals(PyTokenTypes.COMMA)) {
prevSibling.delete();
}
}
Aggregations