use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class UnresolvedReferenceAddParameterQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement();
final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
final PyNamedParameter parameter = elementGenerator.createParameter(element.getText() + "=None");
final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class);
if (function != null) {
final PyParameterList parameterList = function.getParameterList();
parameterList.addParameter(parameter);
CodeInsightUtilCore.forcePsiPostprocessAndRestoreElement(parameterList);
final TemplateBuilder builder = TemplateBuilderFactory.getInstance().createTemplateBuilder(parameter);
builder.replaceRange(TextRange.create(parameter.getTextLength() - 4, parameter.getTextLength()), "None");
final VirtualFile virtualFile = function.getContainingFile().getVirtualFile();
if (virtualFile == null)
return;
final Editor editor = FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile), true);
if (editor == null)
return;
builder.run(editor, false);
}
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class UnresolvedReferenceAddSelfQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
PyExpression expression = elementGenerator.createExpressionFromText(LanguageLevel.forElement(myElement), myQualifier + "." + myElement.getText());
myElement.replace(expression);
}
use of com.jetbrains.python.psi.PyElementGenerator in project intellij-community by JetBrains.
the class ReplaceRaiseStatementQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement raiseStatement = descriptor.getPsiElement();
if (raiseStatement instanceof PyRaiseStatement) {
PyExpression[] expressions = ((PyRaiseStatement) raiseStatement).getExpressions();
PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
String newExpressionText = expressions[0].getText() + "(" + expressions[1].getText() + ")";
if (expressions.length == 2) {
raiseStatement.replace(elementGenerator.createFromText(LanguageLevel.forElement(raiseStatement), PyRaiseStatement.class, "raise " + newExpressionText));
} else if (expressions.length == 3) {
raiseStatement.replace(elementGenerator.createFromText(LanguageLevel.forElement(raiseStatement), PyRaiseStatement.class, "raise " + newExpressionText + ".with_traceback(" + expressions[2].getText() + ")"));
}
}
}
use of com.jetbrains.python.psi.PyElementGenerator 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.PyElementGenerator 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);
}
Aggregations