Search in sources :

Example 1 with RatedResolveResult

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

the class PyUtil method getReturnTypeOfMember.

@Nullable
public static PyType getReturnTypeOfMember(@NotNull PyType type, @NotNull String memberName, @Nullable PyExpression location, @NotNull TypeEvalContext context) {
    final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
    final List<? extends RatedResolveResult> resolveResults = type.resolveMember(memberName, location, AccessDirection.READ, resolveContext);
    if (resolveResults != null) {
        final List<PyType> types = new ArrayList<>();
        for (RatedResolveResult resolveResult : resolveResults) {
            final PyType returnType = getReturnType(resolveResult.getElement(), context);
            if (returnType != null) {
                types.add(returnType);
            }
        }
        return PyUnionType.union(types);
    }
    return null;
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult)

Example 2 with RatedResolveResult

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

the class PyImportedModuleType method resolveMember.

@Nullable
@Override
public List<? extends RatedResolveResult> resolveMember(@NotNull String name, @Nullable PyExpression location, @NotNull AccessDirection direction, @NotNull PyResolveContext resolveContext) {
    final PsiElement resolved = myImportedModule.resolve();
    if (resolved != null) {
        final PsiFile containingFile = location != null ? location.getContainingFile() : null;
        List<PsiElement> elements = Collections.singletonList(ResolveImportUtil.resolveChild(resolved, name, containingFile, false, true, false));
        final PyImportElement importElement = myImportedModule.getImportElement();
        final PyFile resolvedFile = PyUtil.as(resolved, PyFile.class);
        if (location != null && importElement != null && PyUtil.inSameFile(location, importElement) && ResolveImportUtil.getPointInImport(location) == PointInImport.NONE && resolved instanceof PsiFileSystemItem && (resolvedFile == null || !PyUtil.isPackage(resolvedFile) || resolvedFile.getElementNamed(name) == null)) {
            final List<PsiElement> importedSubmodules = PyModuleType.collectImportedSubmodules((PsiFileSystemItem) resolved, location);
            if (importedSubmodules != null) {
                final Set<PsiElement> imported = Sets.newHashSet(importedSubmodules);
                elements = ContainerUtil.filter(elements, element -> imported.contains(element));
            }
        }
        return ResolveImportUtil.rateResults(elements);
    }
    return null;
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) ArrayUtil(com.intellij.util.ArrayUtil) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ResolveImportUtil(com.jetbrains.python.psi.resolve.ResolveImportUtil) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Sets(com.google.common.collect.Sets) ArrayList(java.util.ArrayList) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) com.jetbrains.python.psi(com.jetbrains.python.psi) PsiDirectory(com.intellij.psi.PsiDirectory) PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) PointInImport(com.jetbrains.python.psi.resolve.PointInImport) NotNull(org.jetbrains.annotations.NotNull) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) Collections(java.util.Collections) PsiFile(com.intellij.psi.PsiFile) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with RatedResolveResult

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

the class PyClassTypeImpl method doResolveMember.

@Nullable
private List<? extends RatedResolveResult> doResolveMember(@NotNull String name, @Nullable PyExpression location, @NotNull AccessDirection direction, @NotNull PyResolveContext resolveContext, boolean inherited) {
    final TypeEvalContext context = resolveContext.getTypeEvalContext();
    PsiElement classMember = //overriding members provers have priority to normal resolve
    resolveByOverridingMembersProviders(this, name, location, context);
    if (classMember != null) {
        return ResolveResultList.to(classMember);
    }
    if (resolveContext.allowProperties()) {
        final Ref<ResolveResultList> resultRef = findProperty(name, direction, true, resolveContext.getTypeEvalContext());
        if (resultRef != null) {
            return resultRef.get();
        }
    }
    if ("super".equals(getClassQName()) && isBuiltin() && location instanceof PyCallExpression) {
        // methods of super() call are not of class super!
        PyExpression first_arg = ((PyCallExpression) location).getArgument(0, PyExpression.class);
        if (first_arg != null) {
            // the usual case: first arg is the derived class that super() is proxying for
            PyType first_arg_type = context.getType(first_arg);
            if (first_arg_type instanceof PyClassType) {
                PyClass derived_class = ((PyClassType) first_arg_type).getPyClass();
                final Iterator<PyClass> base_it = derived_class.getAncestorClasses(context).iterator();
                if (base_it.hasNext()) {
                    return new PyClassTypeImpl(base_it.next(), true).resolveMember(name, location, direction, resolveContext);
                } else {
                    // no base classes = super() cannot proxy anything meaningful from a base class
                    return null;
                }
            }
        }
    }
    final List<? extends RatedResolveResult> classMembers = resolveInner(myClass, myIsDefinition, name, location);
    if (PyNames.__CLASS__.equals(name)) {
        return resolveDunderClass(context, classMembers);
    }
    if (!classMembers.isEmpty()) {
        return classMembers;
    }
    if (PyNames.DOC.equals(name)) {
        return Optional.ofNullable(PyBuiltinCache.getInstance(myClass).getObjectType()).map(type -> type.resolveMember(name, location, direction, resolveContext)).orElse(Collections.emptyList());
    }
    classMember = resolveByOverridingAncestorsMembersProviders(this, name, location);
    if (classMember != null) {
        final ResolveResultList list = new ResolveResultList();
        int rate = RatedResolveResult.RATE_NORMAL;
        for (PyResolveResultRater rater : Extensions.getExtensions(PyResolveResultRater.EP_NAME)) {
            rate += rater.getMemberRate(classMember, this, context);
        }
        list.poke(classMember, rate);
        return list;
    }
    if (inherited) {
        for (PyClassLikeType type : myClass.getAncestorTypes(context)) {
            if (type instanceof PyClassType) {
                if (!myIsDefinition) {
                    type = type.toInstance();
                }
                final List<? extends RatedResolveResult> superMembers = resolveInner(((PyClassType) type).getPyClass(), myIsDefinition, name, location);
                if (!superMembers.isEmpty()) {
                    return superMembers;
                }
            }
            if (type != null) {
                final List<? extends RatedResolveResult> results = type.resolveMember(name, location, direction, resolveContext, false);
                if (results != null && !results.isEmpty()) {
                    return results;
                }
            }
        }
    }
    if (inherited && !PyNames.INIT.equals(name) && !PyNames.NEW.equals(name)) {
        final List<? extends RatedResolveResult> typeMembers = resolveMetaClassMember(name, location, direction, resolveContext);
        if (typeMembers != null) {
            return typeMembers;
        }
    }
    if (inherited) {
        classMember = resolveByMembersProviders(this, name, location, //ask providers after real class introspection as providers have less priority
        context);
    }
    if (classMember != null) {
        return ResolveResultList.to(classMember);
    }
    if (inherited) {
        for (PyClassLikeType type : myClass.getAncestorTypes(context)) {
            if (type instanceof PyClassType) {
                final PyClass pyClass = ((PyClassType) type).getPyClass();
                PsiElement superMember = resolveByMembersProviders(new PyClassTypeImpl(pyClass, isDefinition()), name, location, resolveContext.getTypeEvalContext());
                if (superMember != null) {
                    return ResolveResultList.to(superMember);
                }
            }
        }
    }
    return Collections.emptyList();
}
Also used : com.intellij.openapi.util(com.intellij.openapi.util) PyNames(com.jetbrains.python.PyNames) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyUtil.as(com.jetbrains.python.psi.PyUtil.as) PyResolveProcessor(com.jetbrains.python.psi.resolve.PyResolveProcessor) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) CompletionVariantsProcessor(com.jetbrains.python.psi.resolve.CompletionVariantsProcessor) Lists(com.google.common.collect.Lists) PsiElement(com.intellij.psi.PsiElement) ResolveResultList(com.jetbrains.python.psi.impl.ResolveResultList) PsiFile(com.intellij.psi.PsiFile) com.jetbrains.python.psi(com.jetbrains.python.psi) QualifiedName(com.intellij.psi.util.QualifiedName) CompletionUtil(com.intellij.codeInsight.completion.CompletionUtil) PyCustomMemberUtils(com.jetbrains.python.codeInsight.PyCustomMemberUtils) PsiInvalidElementAccessException(com.intellij.psi.PsiInvalidElementAccessException) ProcessingContext(com.intellij.util.ProcessingContext) Extensions(com.intellij.openapi.extensions.Extensions) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) PyReferenceImpl(com.jetbrains.python.psi.impl.references.PyReferenceImpl) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) PyBuiltinCache(com.jetbrains.python.psi.impl.PyBuiltinCache) Nullable(org.jetbrains.annotations.Nullable) PyResolveResultRater(com.jetbrains.python.psi.impl.PyResolveResultRater) Maybe(com.jetbrains.python.toolbox.Maybe) PyResolveImportUtil.resolveTopLevelMember(com.jetbrains.python.psi.resolve.PyResolveImportUtil.resolveTopLevelMember) Processor(com.intellij.util.Processor) PyCustomMember(com.jetbrains.python.codeInsight.PyCustomMember) ObjectUtils(com.intellij.util.ObjectUtils) PyResolveImportUtil.fromFoothold(com.jetbrains.python.psi.resolve.PyResolveImportUtil.fromFoothold) NotNull(org.jetbrains.annotations.NotNull) PyResolveResultRater(com.jetbrains.python.psi.impl.PyResolveResultRater) ResolveResultList(com.jetbrains.python.psi.impl.ResolveResultList) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with RatedResolveResult

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

the class PyOperatorReference method resolveMember.

@NotNull
private List<RatedResolveResult> resolveMember(@Nullable PyExpression object, @Nullable String name) {
    final ArrayList<RatedResolveResult> results = new ArrayList<>();
    if (object != null && name != null) {
        final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
        PyType type = typeEvalContext.getType(object);
        typeEvalContext.trace("Side text is %s, type is %s", object.getText(), type);
        if (type instanceof PyClassLikeType) {
            if (((PyClassLikeType) type).isDefinition()) {
                type = ((PyClassLikeType) type).getMetaClassType(typeEvalContext, true);
            }
        }
        if (type != null) {
            List<? extends RatedResolveResult> res = type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
            if (res != null && res.size() > 0) {
                results.addAll(res);
            } else if (typeEvalContext.tracing()) {
                VirtualFile vFile = null;
                if (type instanceof PyClassType) {
                    final PyClass pyClass = ((PyClassType) type).getPyClass();
                    vFile = pyClass.getContainingFile().getVirtualFile();
                }
                type.resolveMember(name, object, AccessDirection.of(myElement), myContext);
                typeEvalContext.trace("Could not resolve member %s in type %s from file %s", name, type, vFile);
            }
        }
    }
    return results;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyClassLikeType(com.jetbrains.python.psi.types.PyClassLikeType) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with RatedResolveResult

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

the class PyTargetExpressionImpl method findMethodByName.

@Nullable
private static PyFunction findMethodByName(@NotNull PyType type, @NotNull String name, @NotNull TypeEvalContext context) {
    final PyResolveContext resolveContext = PyResolveContext.defaultContext().withTypeEvalContext(context);
    final List<? extends RatedResolveResult> results = type.resolveMember(name, null, AccessDirection.READ, resolveContext);
    if (results != null && !results.isEmpty()) {
        final RatedResolveResult result = results.get(0);
        final PsiElement element = result.getElement();
        if (element instanceof PyFunction) {
            return (PyFunction) element;
        }
    }
    return null;
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)8 PsiElement (com.intellij.psi.PsiElement)5 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)5 NotNull (org.jetbrains.annotations.NotNull)5 Nullable (org.jetbrains.annotations.Nullable)4 Sets (com.google.common.collect.Sets)3 LookupElement (com.intellij.codeInsight.lookup.LookupElement)3 PsiFile (com.intellij.psi.PsiFile)3 PsiInvalidElementAccessException (com.intellij.psi.PsiInvalidElementAccessException)3 ArrayUtil (com.intellij.util.ArrayUtil)3 ProcessingContext (com.intellij.util.ProcessingContext)3 ContainerUtil (com.intellij.util.containers.ContainerUtil)3 com.jetbrains.python.psi (com.jetbrains.python.psi)3 PyResolveProcessor (com.jetbrains.python.psi.resolve.PyResolveProcessor)3 Lists (com.google.common.collect.Lists)2 CompletionUtil (com.intellij.codeInsight.completion.CompletionUtil)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)2 Extensions (com.intellij.openapi.extensions.Extensions)2 com.intellij.openapi.util (com.intellij.openapi.util)2 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)2