use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceBackquoteExpressionQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement problemElement = descriptor.getPsiElement();
if (problemElement instanceof PyReprExpression) {
if (((PyReprExpression) problemElement).getExpression() != null) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
problemElement.replace(elementGenerator.createExpressionFromText("repr(" + ((PyReprExpression) problemElement).getExpression().getText() + ")"));
}
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceListComprehensionsQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement expression = descriptor.getPsiElement();
if (expression instanceof PyTupleExpression) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpressionStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(expression), PyExpressionStatement.class, "(" + expression.getText() + ")");
expression.replace(statement.getExpression());
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceNotEqOperatorQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement binaryExpression = descriptor.getPsiElement();
if (binaryExpression instanceof PyBinaryExpression) {
PsiElement operator = ((PyBinaryExpression) binaryExpression).getPsiOperator();
if (operator != null) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
operator.replace(elementGenerator.createFromText(LanguageLevel.forElement(binaryExpression), LeafPsiElement.class, "!="));
}
}
}
use of com.jetbrains.python.psi.PyElementGenerator 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.PyElementGenerator in project intellij-community by JetBrains.
the class PyRemoveUnderscoresInNumericLiteralsQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
if (element instanceof PyNumericLiteralExpression) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final String text = element.getText();
element.replace(elementGenerator.createExpressionFromText(LanguageLevel.forElement(element), text.replaceAll("_", "")));
}
}
Aggregations