Search in sources :

Example 11 with PyFunctionBuilder

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);
}
Also used : HashMap(com.intellij.util.containers.hash.HashMap) RefactoringElementListenerComposite(com.intellij.refactoring.listeners.RefactoringElementListenerComposite) AbstractVariableData(com.intellij.refactoring.util.AbstractVariableData) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) UsageInfo(com.intellij.usageView.UsageInfo)

Example 12 with PyFunctionBuilder

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);
}
Also used : PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyFunctionBuilder (com.jetbrains.python.psi.impl.PyFunctionBuilder)12 PsiElement (com.intellij.psi.PsiElement)7 NotNull (org.jetbrains.annotations.NotNull)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 PsiFile (com.intellij.psi.PsiFile)2 PyType (com.jetbrains.python.psi.types.PyType)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1 FileEditor (com.intellij.openapi.fileEditor.FileEditor)1 TextEditor (com.intellij.openapi.fileEditor.TextEditor)1 PsiLanguageInjectionHost (com.intellij.psi.PsiLanguageInjectionHost)1 RefactoringElementListenerComposite (com.intellij.refactoring.listeners.RefactoringElementListenerComposite)1 AbstractVariableData (com.intellij.refactoring.util.AbstractVariableData)1 UsageInfo (com.intellij.usageView.UsageInfo)1 HashMap (com.intellij.util.containers.hash.HashMap)1 PyInspectionExtension (com.jetbrains.python.inspections.PyInspectionExtension)1 ParamHelper (com.jetbrains.python.psi.impl.ParamHelper)1 PyClassType (com.jetbrains.python.psi.types.PyClassType)1 PyModuleType (com.jetbrains.python.psi.types.PyModuleType)1