Search in sources :

Example 11 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyJavaSuperMethodsSearchExecutor method execute.

public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
    PyFunction func = queryParameters.getDerivedMethod();
    PyClass containingClass = func.getContainingClass();
    if (containingClass != null) {
        for (PyClassLikeType type : containingClass.getSuperClassTypes(TypeEvalContext.codeInsightFallback(containingClass.getProject()))) {
            if (type instanceof PyJavaClassType) {
                final PsiClass psiClass = ((PyJavaClassType) type).getPsiClass();
                PsiMethod[] methods = psiClass.findMethodsByName(func.getName(), true);
                // the Python method actually does override/implement all of Java super methods with the same name
                if (!ContainerUtil.process(methods, consumer))
                    return false;
            }
        }
    }
    return true;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiMethod(com.intellij.psi.PsiMethod) PsiClass(com.intellij.psi.PsiClass) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType)

Example 12 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyCustomMember method resolve.

@Nullable
public PsiElement resolve(@NotNull final PsiElement context) {
    if (myTarget != null) {
        return myTarget;
    }
    PyClass targetClass = null;
    if (myTypeName != null) {
        final ParameterizedCachedValueProvider<PyClass, PsiElement> provider = new ParameterizedCachedValueProvider<PyClass, PsiElement>() {

            @Nullable
            @Override
            public CachedValueProvider.Result<PyClass> compute(final PsiElement param) {
                final PyClass result = PyPsiFacade.getInstance(param.getProject()).createClassByQName(myTypeName, param);
                return CachedValueProvider.Result.create(result, PsiModificationTracker.MODIFICATION_COUNT);
            }
        };
        targetClass = CachedValuesManager.getManager(context.getProject()).getParameterizedCachedValue(this, RESOLVE, provider, false, context);
    }
    final PsiElement resolveTarget = findResolveTarget(context);
    if (resolveTarget instanceof PyFunction && !myAlwaysResolveToCustomElement) {
        return resolveTarget;
    }
    if (resolveTarget != null || targetClass != null) {
        return new MyInstanceElement(targetClass, context, resolveTarget);
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement) ASTWrapperPsiElement(com.intellij.extapi.psi.ASTWrapperPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 13 with PyFunction

use of com.jetbrains.python.psi.PyFunction 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)

Example 14 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyRemoveParameterQuickFix method applyFix.

public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement parameter = descriptor.getPsiElement();
    assert parameter instanceof PyParameter;
    final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
    if (function != null) {
        final PyResolveContext resolveContext = PyResolveContext.noImplicits();
        StreamEx.of(PyRefactoringUtil.findUsages(function, false)).map(UsageInfo::getElement).nonNull().map(PsiElement::getParent).select(PyCallExpression.class).flatMap(callExpression -> callExpression.multiMapArguments(resolveContext).stream()).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == parameter).forEach(entry -> entry.getKey().delete());
        final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
        final String parameterName = ((PyParameter) parameter).getName();
        if (docStringExpression != null && parameterName != null) {
            PyDocstringGenerator.forDocStringOwner(function).withoutParam(parameterName).buildAndInsert();
        }
    }
    parameter.delete();
}
Also used : PyBundle(com.jetbrains.python.PyBundle) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) UsageInfo(com.intellij.usageView.UsageInfo) PyParameter(com.jetbrains.python.psi.PyParameter) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PyRefactoringUtil(com.jetbrains.python.refactoring.PyRefactoringUtil) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PyFunction(com.jetbrains.python.psi.PyFunction) PyFunction(com.jetbrains.python.psi.PyFunction) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 15 with PyFunction

use of com.jetbrains.python.psi.PyFunction in project intellij-community by JetBrains.

the class PyDeprecationTest method testFunction.

public void testFunction() {
    myFixture.configureByText(PythonFileType.INSTANCE, "def getstatus(file):\n" + "    \"\"\"Return output of \"ls -ld <file>\" in a string.\"\"\"\n" + "    import warnings\n" + "    warnings.warn(\"commands.getstatus() is deprecated\", DeprecationWarning, 2)\n" + "    return getoutput('ls -ld' + mkarg(file))");
    PyFunction getstatus = ((PyFile) myFixture.getFile()).findTopLevelFunction("getstatus");
    assertEquals("commands.getstatus() is deprecated", getstatus.getDeprecationMessage());
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyFile(com.jetbrains.python.psi.PyFile)

Aggregations

PyFunction (com.jetbrains.python.psi.PyFunction)61 PyClass (com.jetbrains.python.psi.PyClass)33 PsiElement (com.intellij.psi.PsiElement)24 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)10 PyFile (com.jetbrains.python.psi.PyFile)9 ArrayList (java.util.ArrayList)8 PyMethodMember (com.jetbrains.python.codeInsight.override.PyMethodMember)7 PsiFile (com.intellij.psi.PsiFile)5 Editor (com.intellij.openapi.editor.Editor)4 VirtualFile (com.intellij.openapi.vfs.VirtualFile)4 Document (com.intellij.openapi.editor.Document)3 Project (com.intellij.openapi.project.Project)3 PsiDirectory (com.intellij.psi.PsiDirectory)3 PsiNamedElement (com.intellij.psi.PsiNamedElement)3 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)3 AbstractTreeNode (com.intellij.ide.util.treeView.AbstractTreeNode)2 ASTNode (com.intellij.lang.ASTNode)2 ItemPresentation (com.intellij.navigation.ItemPresentation)2 Pair (com.intellij.openapi.util.Pair)2