use of com.jetbrains.python.psi.impl.PyFunctionBuilder in project intellij-community by JetBrains.
the class PyExtractMethodUtil method processParameters.
private static void processParameters(@NotNull final Project project, @NotNull final PyFunction generatedMethod, @NotNull final AbstractVariableData[] variableData, final boolean isMethod, final boolean isClassMethod, final boolean isStaticMethod) {
final Map<String, String> map = createMap(variableData);
// Rename parameters
for (PyParameter parameter : generatedMethod.getParameterList().getParameters()) {
final String name = parameter.getName();
final String newName = map.get(name);
if (name != null && newName != null && !name.equals(newName)) {
final Map<PsiElement, String> allRenames = new java.util.HashMap<>();
allRenames.put(parameter, newName);
final UsageInfo[] usages = RenameUtil.findUsages(parameter, newName, false, false, allRenames);
try {
RenameUtil.doRename(parameter, newName, usages, project, new RefactoringElementListenerComposite());
} catch (IncorrectOperationException e) {
RenameUtil.showErrorMessage(e, parameter, project);
return;
}
}
}
// Change signature according to pass settings and
final PyFunctionBuilder builder = new PyFunctionBuilder("foo", generatedMethod);
if (isClassMethod) {
builder.parameter("cls");
} else if (isMethod && !isStaticMethod) {
builder.parameter("self");
}
for (AbstractVariableData data : variableData) {
if (data.isPassAsParameter()) {
builder.parameter(data.getName());
}
}
final PyParameterList pyParameterList = builder.buildFunction(project, LanguageLevel.forElement(generatedMethod)).getParameterList();
generatedMethod.getParameterList().replace(pyParameterList);
}
use of com.jetbrains.python.psi.impl.PyFunctionBuilder in project intellij-community by JetBrains.
the class PyIntroduceFieldHandler method addFieldToSetUp.
@NotNull
private static PsiElement addFieldToSetUp(PyClass clazz, final Function<String, PyStatement> callback) {
final PyFunction init = clazz.findMethodByName(PythonUnitTestUtil.TESTCASE_SETUP_NAME, false, null);
if (init != null) {
return AddFieldQuickFix.appendToMethod(init, callback);
}
final PyFunctionBuilder builder = new PyFunctionBuilder(PythonUnitTestUtil.TESTCASE_SETUP_NAME, clazz);
builder.parameter(PyNames.CANONICAL_SELF);
PyFunction setUp = builder.buildFunction(clazz.getProject(), LanguageLevel.getDefault());
final PyStatementList statements = clazz.getStatementList();
final PsiElement anchor = statements.getFirstChild();
setUp = (PyFunction) statements.addBefore(setUp, anchor);
return AddFieldQuickFix.appendToMethod(setUp, callback);
}
Aggregations