use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceExceptPartQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement exceptPart = descriptor.getPsiElement();
if (exceptPart instanceof PyExceptPart) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PsiElement element = ((PyExceptPart) exceptPart).getExceptClass().getNextSibling();
while (element instanceof PsiWhiteSpace) {
element = element.getNextSibling();
}
assert element != null;
PyTryExceptStatement newElement = elementGenerator.createFromText(LanguageLevel.forElement(exceptPart), PyTryExceptStatement.class, "try: pass except a as b: pass");
ASTNode node = newElement.getExceptParts()[0].getNode().findChildByType(PyTokenTypes.AS_KEYWORD);
assert node != null;
element.replace(node.getPsi());
}
}
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 RemovePrefixQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PyStringLiteralExpression pyString = as(descriptor.getPsiElement(), PyStringLiteralExpression.class);
if (pyString != null) {
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
for (ASTNode node : pyString.getStringNodes()) {
final String nodeText = node.getText();
final int prefixLength = PyStringLiteralExpressionImpl.getPrefixLength(nodeText);
if (nodeText.substring(0, prefixLength).equalsIgnoreCase(myPrefix)) {
final PyStringLiteralExpression replacement = elementGenerator.createStringLiteralAlreadyEscaped(nodeText.substring(prefixLength));
node.getPsi().replace(replacement.getFirstChild());
}
}
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class RemoveTrailingLQuickFix 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(text.substring(0, text.length() - 1)));
}
}
Aggregations