@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()); } }
@Override public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) { final PsiElement element = descriptor.getPsiElement(); final PyFunction function = PsiTreeUtil.getParentOfType(element, PyFunction.class, false); assert function != null; final PyParameterList parameterList = function.getParameterList(); final PyParameter[] parameters = parameterList.getParameters(); final PyElementGenerator generator = PyElementGenerator.getInstance(project); String selfText = parameters.length != 0 ? parameters[0].getText() : PyNames.CANONICAL_SELF; final StringBuilder functionText = new StringBuilder("def foo(" + selfText); if (myHasValue) { String valueText = parameters.length > 1 ? parameters[1].getText() : "value"; functionText.append(", ").append(valueText); } functionText.append("): pass"); final PyParameterList list = generator .createFromText( LanguageLevel.forElement(element), PyFunction.class, functionText.toString()) .getParameterList(); parameterList.replace(list); }