Search in sources :

Example 26 with TypeEvalContext

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

the class PyReferenceImpl method isReferenceTo.

@Override
public boolean isReferenceTo(PsiElement element) {
    if (element instanceof PsiFileSystemItem) {
        // may be import via alias, so don't check if names match, do simple resolve check instead
        PsiElement resolveResult = resolve();
        if (resolveResult instanceof PyImportedModule) {
            resolveResult = resolveResult.getNavigationElement();
        }
        if (element instanceof PsiDirectory) {
            if (resolveResult instanceof PyFile) {
                final PyFile file = (PyFile) resolveResult;
                if (PyUtil.isPackage(file) && file.getContainingDirectory() == element) {
                    return true;
                }
            } else if (resolveResult instanceof PsiDirectory) {
                final PsiDirectory directory = (PsiDirectory) resolveResult;
                if (PyUtil.isPackage(directory, null) && directory == element) {
                    return true;
                }
            }
        }
        return resolveResult == element;
    }
    if (element instanceof PsiNamedElement) {
        final String elementName = ((PsiNamedElement) element).getName();
        if ((Comparing.equal(myElement.getReferencedName(), elementName) || PyNames.INIT.equals(elementName))) {
            if (!haveQualifiers(element)) {
                final ScopeOwner ourScopeOwner = ScopeUtil.getScopeOwner(getElement());
                final ScopeOwner theirScopeOwner = ScopeUtil.getScopeOwner(element);
                if (element instanceof PyParameter || element instanceof PyTargetExpression) {
                    // Check if the reference is in the same or inner scope of the element scope, not shadowed by an intermediate declaration
                    if (resolvesToSameLocal(element, elementName, ourScopeOwner, theirScopeOwner)) {
                        return true;
                    }
                }
                final PsiElement resolveResult = resolve();
                if (resolveResult == element) {
                    return true;
                }
                // we shadow their name or they shadow ours (PY-6241)
                if (resolveResult instanceof PsiNamedElement && resolveResult instanceof ScopeOwner && element instanceof ScopeOwner && theirScopeOwner == ScopeUtil.getScopeOwner(resolveResult)) {
                    return true;
                }
                if (!haveQualifiers(element) && ourScopeOwner != null && theirScopeOwner != null) {
                    if (resolvesToSameGlobal(element, elementName, ourScopeOwner, theirScopeOwner, resolveResult))
                        return true;
                }
                if (resolvesToWrapper(element, resolveResult)) {
                    return true;
                }
            }
            if (element instanceof PyExpression) {
                final PyExpression expr = (PyExpression) element;
                if (PyUtil.isClassAttribute(myElement) && (PyUtil.isClassAttribute(expr) || PyUtil.isInstanceAttribute(expr))) {
                    final PyClass c1 = PsiTreeUtil.getParentOfType(element, PyClass.class);
                    final PyClass c2 = PsiTreeUtil.getParentOfType(myElement, PyClass.class);
                    final TypeEvalContext context = myContext.getTypeEvalContext();
                    if (c1 != null && c2 != null && (c1.isSubclass(c2, context) || c2.isSubclass(c1, context))) {
                        return true;
                    }
                }
            }
        }
    }
    return false;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner)

Example 27 with TypeEvalContext

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

the class PyReferenceImpl method resolveByReferenceResolveProviders.

@NotNull
private ResolveResultList resolveByReferenceResolveProviders() {
    final ResolveResultList results = new ResolveResultList();
    final TypeEvalContext context = myContext.getTypeEvalContext();
    for (PyReferenceResolveProvider provider : Extensions.getExtensions(PyReferenceResolveProvider.EP_NAME)) {
        if (provider instanceof PyOverridingReferenceResolveProvider) {
            continue;
        }
        results.addAll(provider.resolveName(myElement, context));
    }
    return results;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 28 with TypeEvalContext

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

the class PyReferenceImpl method resolveByOverridingReferenceResolveProviders.

@NotNull
private ResolveResultList resolveByOverridingReferenceResolveProviders() {
    final ResolveResultList results = new ResolveResultList();
    final TypeEvalContext context = myContext.getTypeEvalContext();
    Arrays.stream(Extensions.getExtensions(PyReferenceResolveProvider.EP_NAME)).filter(PyOverridingReferenceResolveProvider.class::isInstance).map(provider -> provider.resolveName(myElement, context)).forEach(results::addAll);
    return results;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyNames(com.jetbrains.python.PyNames) java.util(java.util) HighlightSeverity(com.intellij.lang.annotation.HighlightSeverity) com.jetbrains.python.psi.impl(com.jetbrains.python.psi.impl) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Lists(com.google.common.collect.Lists) Scope(com.jetbrains.python.codeInsight.dataflow.scope.Scope) Comparing(com.intellij.openapi.util.Comparing) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) com.jetbrains.python.psi.resolve(com.jetbrains.python.psi.resolve) ControlFlowCache(com.jetbrains.python.codeInsight.controlflow.ControlFlowCache) com.jetbrains.python.psi(com.jetbrains.python.psi) PyiUtil(com.jetbrains.python.pyi.PyiUtil) CompletionUtil(com.intellij.codeInsight.completion.CompletionUtil) Instruction(com.intellij.codeInsight.controlflow.Instruction) ResolveCache(com.intellij.psi.impl.source.resolve.ResolveCache) PyDefUseUtil(com.jetbrains.python.refactoring.PyDefUseUtil) PlatformIcons(com.intellij.util.PlatformIcons) ProcessingContext(com.intellij.util.ProcessingContext) Extensions(com.intellij.openapi.extensions.Extensions) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) IncorrectOperationException(com.intellij.util.IncorrectOperationException) LookupElement(com.intellij.codeInsight.lookup.LookupElement) PyType(com.jetbrains.python.psi.types.PyType) StringUtil(com.intellij.openapi.util.text.StringUtil) TextRange(com.intellij.openapi.util.TextRange) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) Pair(com.intellij.openapi.util.Pair) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) PyModuleType(com.jetbrains.python.psi.types.PyModuleType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext 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 30 with TypeEvalContext

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

the class CompletionVariantsProcessor method setupItem.

private LookupElementBuilder setupItem(LookupElementBuilder item) {
    final PsiElement element = item.getPsiElement();
    if (!myPlainNamesOnly) {
        if (!mySuppressParentheses && element instanceof PyFunction && ((PyFunction) element).getProperty() == null && !PyUtil.hasCustomDecorators((PyFunction) element) && !isSingleArgDecoratorCall(myContext, (PyFunction) element)) {
            final Project project = element.getProject();
            item = item.withInsertHandler(PyFunctionInsertHandler.INSTANCE);
            final TypeEvalContext context = TypeEvalContext.codeCompletion(project, myContext != null ? myContext.getContainingFile() : null);
            final List<PyParameter> parameters = PyUtil.getParameters((PyFunction) element, context);
            final String params = StringUtil.join(parameters, pyParameter -> pyParameter.getName(), ", ");
            item = item.withTailText("(" + params + ")");
        } else if (element instanceof PyClass) {
            item = item.withInsertHandler(PyClassInsertHandler.INSTANCE);
        }
    }
    String source = null;
    if (element != null) {
        PyClass cls = null;
        if (element instanceof PyFunction) {
            cls = ((PyFunction) element).getContainingClass();
        } else if (element instanceof PyTargetExpression) {
            final PyTargetExpression expr = (PyTargetExpression) element;
            if (expr.isQualified() || ScopeUtil.getScopeOwner(expr) instanceof PyClass) {
                cls = expr.getContainingClass();
            }
        } else if (element instanceof PyClass) {
            final ScopeOwner owner = ScopeUtil.getScopeOwner(element);
            if (owner instanceof PyClass) {
                cls = (PyClass) owner;
            }
        }
        if (cls != null) {
            source = cls.getName();
        } else if (myContext == null || !PyUtil.inSameFile(myContext, element)) {
            QualifiedName path = QualifiedNameFinder.findCanonicalImportPath(element, null);
            if (path != null) {
                if (element instanceof PyFile) {
                    path = path.removeLastComponent();
                }
                source = path.toString();
            }
        }
    }
    if (source != null) {
        item = item.withTypeText(source);
    }
    return item;
}
Also used : Project(com.intellij.openapi.project.Project) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName) PsiElement(com.intellij.psi.PsiElement) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Aggregations

TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)44 PyType (com.jetbrains.python.psi.types.PyType)15 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)8 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)6 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)5 ArrayList (java.util.ArrayList)5 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)4 com.jetbrains.python.psi (com.jetbrains.python.psi)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 XmlStringUtil (com.intellij.xml.util.XmlStringUtil)3 PyNames (com.jetbrains.python.PyNames)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 PyClassType (com.jetbrains.python.psi.types.PyClassType)3 Lists (com.google.common.collect.Lists)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ASTNode (com.intellij.lang.ASTNode)2