Search in sources :

Example 6 with PyClassLikeType

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

the class PySuperMethodsSearchExecutor method execute.

@Override
public boolean execute(@NotNull final PySuperMethodsSearch.SearchParameters queryParameters, @NotNull final Processor<PsiElement> consumer) {
    final PyFunction func = queryParameters.getDerivedMethod();
    final String name = func.getName();
    final PyClass containingClass = func.getContainingClass();
    final Set<PyClass> foundMethodContainingClasses = new HashSet<>();
    final TypeEvalContext context = queryParameters.getContext();
    if (name != null && containingClass != null) {
        for (PyClass superClass : containingClass.getAncestorClasses(context)) {
            if (!queryParameters.isDeepSearch()) {
                boolean isAlreadyFound = false;
                for (PyClass alreadyFound : foundMethodContainingClasses) {
                    if (alreadyFound.isSubclass(superClass, context)) {
                        isAlreadyFound = true;
                    }
                }
                if (isAlreadyFound) {
                    continue;
                }
            }
            PyFunction superMethod = superClass.findMethodByName(name, false, null);
            if (superMethod != null) {
                final Property property = func.getProperty();
                final Property superProperty = superMethod.getProperty();
                if (property != null && superProperty != null) {
                    final AccessDirection direction = PyUtil.getPropertyAccessDirection(func);
                    final PyCallable callable = superProperty.getByDirection(direction).valueOrNull();
                    superMethod = (callable instanceof PyFunction) ? (PyFunction) callable : null;
                }
            }
            if (superMethod == null && context != null) {
                // If super method still not found and we have context, we may use it to find method
                final PyClassLikeType classLikeType = PyUtil.as(context.getType(superClass), PyClassLikeType.class);
                if (classLikeType != null) {
                    for (final PyFunction function : PyTypeUtil.getMembersOfType(classLikeType, PyFunction.class, true, context)) {
                        final String elemName = function.getName();
                        if (elemName != null && elemName.equals(func.getName())) {
                            consumer.process(function);
                        }
                    }
                }
            }
            if (superMethod != null) {
                foundMethodContainingClasses.add(superClass);
                if (!consumer.process(superMethod)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) HashSet(java.util.HashSet)

Example 7 with PyClassLikeType

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

the class PyiUtil method findSimilarElement.

@Nullable
private static PsiElement findSimilarElement(@NotNull PyElement element, @NotNull PyFile file) {
    if (element instanceof PyFile) {
        return file;
    }
    final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
    final String name = element.getName();
    if (owner != null && name != null) {
        assert owner != element;
        final PsiElement originalOwner = findSimilarElement(owner, file);
        if (originalOwner instanceof PyClass) {
            final PyClass classOwner = (PyClass) originalOwner;
            final PyType type = TypeEvalContext.codeInsightFallback(classOwner.getProject()).getType(classOwner);
            if (type instanceof PyClassLikeType) {
                final PyClassLikeType classType = (PyClassLikeType) type;
                final PyClassLikeType instanceType = classType.toInstance();
                final List<? extends RatedResolveResult> resolveResults = instanceType.resolveMember(name, null, AccessDirection.READ, PyResolveContext.noImplicits(), false);
                if (resolveResults != null && !resolveResults.isEmpty()) {
                    return resolveResults.get(0).getElement();
                }
            }
        } else if (originalOwner instanceof PyFile) {
            return ((PyFile) originalOwner).getElementNamed(name);
        }
    }
    return null;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) PyType(com.jetbrains.python.psi.types.PyType) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with PyClassLikeType

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

the class PyClassMROTest method assertMRO.

public void assertMRO(@NotNull PyClass cls, @NotNull String... mro) {
    final List<PyClassLikeType> types = cls.getAncestorTypes(TypeEvalContext.deepCodeInsight(cls.getProject()));
    final List<String> classNames = new ArrayList<>();
    for (PyClassLikeType type : types) {
        if (type != null) {
            final String name = type.getName();
            if (name != null) {
                classNames.add(name);
                continue;
            }
        }
        classNames.add("unknown");
    }
    assertOrderedEquals(classNames, Arrays.asList(mro));
}
Also used : ArrayList(java.util.ArrayList) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType)

Aggregations

PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)8 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)4 PyType (com.jetbrains.python.psi.types.PyType)3 PsiElement (com.intellij.psi.PsiElement)2 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 ArrayList (java.util.ArrayList)2 Nullable (org.jetbrains.annotations.Nullable)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiClass (com.intellij.psi.PsiClass)1 PsiMethod (com.intellij.psi.PsiMethod)1 PyClass (com.jetbrains.python.psi.PyClass)1 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)1 PyClassType (com.jetbrains.python.psi.types.PyClassType)1 HashSet (java.util.HashSet)1 NotNull (org.jetbrains.annotations.NotNull)1