Search in sources :

Example 1 with PyParameterInfo

use of com.jetbrains.python.refactoring.changeSignature.PyParameterInfo in project intellij-community by JetBrains.

the class PyChangeSignatureQuickFix method applyFix.

public void applyFix(@NotNull final Project project, @NotNull final ProblemDescriptor descriptor) {
    final PyFunction function = PsiTreeUtil.getParentOfType(descriptor.getPsiElement(), PyFunction.class);
    if (function == null)
        return;
    final PyClass cls = function.getContainingClass();
    assert cls != null;
    final String functionName = function.getName();
    final String complementaryName = PyNames.NEW.equals(functionName) ? PyNames.INIT : PyNames.NEW;
    final TypeEvalContext context = TypeEvalContext.userInitiated(project, descriptor.getEndElement().getContainingFile());
    final PyFunction complementaryMethod = myOverridenMethod ? (PyFunction) PySuperMethodsSearch.search(function, context).findFirst() : cls.findMethodByName(complementaryName, true, null);
    assert complementaryMethod != null;
    final PyMethodDescriptor methodDescriptor = new PyMethodDescriptor(function) {

        @Override
        public List<PyParameterInfo> getParameters() {
            final List<PyParameterInfo> parameterInfos = super.getParameters();
            final int paramLength = function.getParameterList().getParameters().length;
            final int complementaryParamLength = complementaryMethod.getParameterList().getParameters().length;
            if (complementaryParamLength > paramLength)
                parameterInfos.add(new PyParameterInfo(-1, "**kwargs", "", false));
            return parameterInfos;
        }
    };
    final PyChangeSignatureDialog dialog = new PyChangeSignatureDialog(project, methodDescriptor);
    dialog.show();
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyChangeSignatureDialog(com.jetbrains.python.refactoring.changeSignature.PyChangeSignatureDialog) PyFunction(com.jetbrains.python.psi.PyFunction) PyMethodDescriptor(com.jetbrains.python.refactoring.changeSignature.PyMethodDescriptor) PyParameterInfo(com.jetbrains.python.refactoring.changeSignature.PyParameterInfo) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Aggregations

PyClass (com.jetbrains.python.psi.PyClass)1 PyFunction (com.jetbrains.python.psi.PyFunction)1 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)1 PyChangeSignatureDialog (com.jetbrains.python.refactoring.changeSignature.PyChangeSignatureDialog)1 PyMethodDescriptor (com.jetbrains.python.refactoring.changeSignature.PyMethodDescriptor)1 PyParameterInfo (com.jetbrains.python.refactoring.changeSignature.PyParameterInfo)1