Search in sources :

Example 36 with PyFunction

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

the class AbstractPythonTestRunConfiguration method getTestSpec.

/**
   * Create test spec (string to be passed to runner, probably glued with {@link AbstractPythonLegacyTestRunConfiguration#TEST_NAME_PARTS_SPLITTER})
   *
   * @param location   test location as reported by runner
   * @param failedTest failed test
   * @return string spec or null if spec calculation is impossible
   */
@Nullable
public String getTestSpec(@NotNull final Location<?> location, @NotNull final AbstractTestProxy failedTest) {
    PsiElement element = location.getPsiElement();
    PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false);
    if (location instanceof PyPsiLocationWithFixedClass) {
        pyClass = ((PyPsiLocationWithFixedClass) location).getFixedClass();
    }
    PyFunction pyFunction = PsiTreeUtil.getParentOfType(element, PyFunction.class, false);
    final VirtualFile virtualFile = location.getVirtualFile();
    if (virtualFile != null) {
        String path = virtualFile.getCanonicalPath();
        if (pyClass != null) {
            path += TEST_NAME_PARTS_SPLITTER + pyClass.getName();
        }
        if (pyFunction != null) {
            path += TEST_NAME_PARTS_SPLITTER + pyFunction.getName();
        }
        return path;
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyFunction(com.jetbrains.python.psi.PyFunction) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 37 with PyFunction

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

the class PyTestConfigurationProducer method getKeywords.

@Nullable
private static String getKeywords(@NotNull final PsiElement element, @NotNull final Sdk sdk) {
    final PyFunction pyFunction = findTestFunction(element);
    final PyClass pyClass = PsiTreeUtil.getParentOfType(element, PyClass.class, false);
    String keywords = null;
    if (pyFunction != null) {
        keywords = pyFunction.getName();
        if (pyClass != null) {
            final List<PyPackage> packages = PyPackageManager.getInstance(sdk).getPackages();
            final PyPackage pytestPackage = packages != null ? PyPackageUtil.findPackage(packages, "pytest") : null;
            if (pytestPackage != null && PackageVersionComparator.VERSION_COMPARATOR.compare(pytestPackage.getVersion(), "2.3.3") >= 0) {
                keywords = pyClass.getName() + " and " + keywords;
            } else {
                keywords = pyClass.getName() + "." + keywords;
            }
        }
    } else if (pyClass != null) {
        keywords = pyClass.getName();
    }
    return keywords;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyPackage(com.jetbrains.python.packaging.PyPackage) PyFunction(com.jetbrains.python.psi.PyFunction) Nullable(org.jetbrains.annotations.Nullable)

Example 38 with PyFunction

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

the class NumpyClassMembersProvider method getMembers.

@NotNull
@Override
public Collection<PyCustomMember> getMembers(PyClassType clazz, PsiElement location, TypeEvalContext typeEvalContext) {
    if (location != null && clazz.getPyClass().isSubclass(PyNames.TYPES_FUNCTION_TYPE, typeEvalContext)) {
        final PsiElement element = location.getOriginalElement();
        final PsiReference reference = element.getReference();
        if (reference != null) {
            final PsiElement resolved = reference.resolve();
            if (resolved instanceof PyFunction) {
                final List<PyCustomMember> result = new ArrayList<>();
                if (NumpyUfuncs.isUFunc(((PyFunction) resolved).getName()) && NumpyDocStringTypeProvider.isInsideNumPy(resolved)) {
                    for (String method : NumpyUfuncs.UFUNC_METHODS) {
                        result.add(new PyCustomMember(method, resolved));
                    }
                    return result;
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : PyFunction(com.jetbrains.python.psi.PyFunction) PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with PyFunction

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

the class PyQtTypeProvider method getReturnType.

@Override
public Ref<PyType> getReturnType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
    if (PyNames.INIT.equals(callable.getName()) && callable instanceof PyFunction) {
        final PyFunction function = (PyFunction) callable;
        final PyClass containingClass = function.getContainingClass();
        if (containingClass != null && ourQt4Signal.equals(containingClass.getName())) {
            final String classQName = containingClass.getQualifiedName();
            if (classQName != null) {
                final QualifiedName name = QualifiedName.fromDottedString(classQName);
                final String qtVersion = name.getComponents().get(0);
                final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, function.getProject());
                if (aClass != null) {
                    final PyType type = new PyClassTypeImpl(aClass, false);
                    return Ref.create(type);
                }
            }
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) PyType(com.jetbrains.python.psi.types.PyType)

Example 40 with PyFunction

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

the class PyQtTypeProvider method getCallableType.

@Nullable
@Override
public PyType getCallableType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
    if (callable instanceof PyFunction) {
        final String qualifiedName = callable.getQualifiedName();
        if (qualifiedName != null && qualifiedName.startsWith("PyQt")) {
            final QualifiedName name = QualifiedName.fromDottedString(qualifiedName);
            final String qtVersion = name.getComponents().get(0);
            final String docstring = ((PyFunction) callable).getDocStringValue();
            if (docstring != null && docstring.contains("[signal]")) {
                final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, callable.getProject());
                if (aClass != null)
                    return new PyClassTypeImpl(aClass, false);
            }
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

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