use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceOctalNumericLiteralQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement numericLiteralExpression = descriptor.getPsiElement();
if (numericLiteralExpression instanceof PyNumericLiteralExpression) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
String text = numericLiteralExpression.getText();
numericLiteralExpression.replace(elementGenerator.createExpressionFromText("0o" + text.substring(1)));
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class UnresolvedRefAddFutureImportQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
PyFile file = (PyFile) element.getContainingFile();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyFromImportStatement statement = elementGenerator.createFromText(LanguageLevel.forElement(element), PyFromImportStatement.class, "from __future__ import with_statement");
file.addBefore(statement, file.getStatements().get(0));
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ChainedComparisonsQuickFix method applyFix.
private void applyFix(@NotNull PyBinaryExpression leftExpression, @NotNull PyBinaryExpression rightExpression, @NotNull Project project) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyExpression newLeftExpression, newRightExpression;
final String operator;
if (myCommonIsInLeftLeft) {
newLeftExpression = invertExpression(leftExpression, elementGenerator);
} else {
newLeftExpression = leftExpression;
}
if (myCommonIsInRightLeft) {
operator = getLeftestOperator(rightExpression).getText();
newRightExpression = getLargeRightExpression(rightExpression, project);
} else {
operator = invertOperator(assertNotNull(rightExpression.getPsiOperator()));
final PyExpression rightLeftExpr = rightExpression.getLeftExpression();
if (rightLeftExpr instanceof PyBinaryExpression) {
newRightExpression = invertExpression((PyBinaryExpression) rightLeftExpr, elementGenerator);
} else {
newRightExpression = rightLeftExpr;
}
}
final PyBinaryExpression binaryExpression = elementGenerator.createBinaryExpression(operator, newLeftExpression, newRightExpression);
leftExpression.replace(binaryExpression);
rightExpression.delete();
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ComparisonWithNoneQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement problemElement = descriptor.getPsiElement();
if (problemElement instanceof PyBinaryExpression) {
PyBinaryExpression binaryExpression = (PyBinaryExpression) problemElement;
PyElementType operator = binaryExpression.getOperator();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
String temp;
temp = (operator == PyTokenTypes.EQEQ) ? "is" : "is not";
PyExpression expression = elementGenerator.createBinaryExpression(temp, binaryExpression.getLeftExpression(), binaryExpression.getRightExpression());
binaryExpression.replace(expression);
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ConvertDocstringQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement expression = descriptor.getPsiElement();
if (expression instanceof PyStringLiteralExpression && expression.isWritable()) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
String stringText = expression.getText();
int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(stringText);
String prefix = stringText.substring(0, prefixLength);
String content = stringText.substring(prefixLength);
if (content.startsWith("'''")) {
content = content.substring(3, content.length() - 3);
} else if (content.startsWith("\"\"\""))
return;
else {
content = content.length() == 1 ? "" : content.substring(1, content.length() - 1);
}
if (content.endsWith("\""))
content = StringUtil.replaceSubstring(content, TextRange.create(content.length() - 1, content.length()), "\\\"");
PyExpression newString = elementGenerator.createDocstring(prefix + "\"\"\"" + content + "\"\"\"").getExpression();
expression.replace(newString);
}
}
Aggregations