Search in sources :

Example 1 with ImplicitResolveResult

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

the class PyQualifiedReference method isReferenceTo.

@Override
public boolean isReferenceTo(PsiElement element) {
    // performance: a qualified reference can never resolve to a local variable or parameter
    if (isLocalScope(element)) {
        return false;
    }
    final String referencedName = myElement.getReferencedName();
    PyResolveContext resolveContext = myContext.withoutImplicits();
    // Guess type eval context origin for switching to local dataflow and return type analysis
    if (resolveContext.getTypeEvalContext().getOrigin() == null) {
        final PsiFile containingFile = myElement.getContainingFile();
        if (containingFile instanceof StubBasedPsiElement) {
            assert ((StubBasedPsiElement) containingFile).getStub() == null : "Stub origin for type eval context in isReferenceTo()";
        }
        final TypeEvalContext context = TypeEvalContext.codeAnalysis(containingFile.getProject(), containingFile);
        resolveContext = resolveContext.withTypeEvalContext(context);
    }
    if (element instanceof PyFunction && Comparing.equal(referencedName, ((PyFunction) element).getName()) && ((PyFunction) element).getContainingClass() != null && !PyNames.INIT.equals(referencedName)) {
        final PyExpression qualifier = myElement.getQualifier();
        if (qualifier != null) {
            final PyType qualifierType = resolveContext.getTypeEvalContext().getType(qualifier);
            if (qualifierType == null || (qualifierType instanceof PyStructuralType && ((PyStructuralType) qualifierType).isInferredFromUsages())) {
                return true;
            }
        }
    }
    for (ResolveResult result : copyWithResolveContext(resolveContext).multiResolve(false)) {
        LOG.assertTrue(!(result instanceof ImplicitResolveResult));
        PsiElement resolveResult = result.getElement();
        if (isResolvedToResult(element, resolveResult)) {
            return true;
        }
    }
    return false;
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) ImplicitResolveResult(com.jetbrains.python.psi.resolve.ImplicitResolveResult) ImplicitResolveResult(com.jetbrains.python.psi.resolve.ImplicitResolveResult) RatedResolveResult(com.jetbrains.python.psi.resolve.RatedResolveResult)

Example 2 with ImplicitResolveResult

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

the class PyQualifiedReference method addImplicitResolveResults.

private void addImplicitResolveResults(String referencedName, ResolveResultList ret) {
    final Project project = myElement.getProject();
    final GlobalSearchScope scope = PyProjectScopeBuilder.excludeSdkTestsScope(project);
    final Collection functions = PyFunctionNameIndex.find(referencedName, project, scope);
    final PsiFile containingFile = myElement.getContainingFile();
    final List<QualifiedName> imports;
    if (containingFile instanceof PyFile) {
        imports = collectImports((PyFile) containingFile);
    } else {
        imports = Collections.emptyList();
    }
    for (Object function : functions) {
        if (!(function instanceof PyFunction)) {
            FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, new Throwable("found non-function object " + function + " in function list"));
            break;
        }
        PyFunction pyFunction = (PyFunction) function;
        if (pyFunction.getContainingClass() != null) {
            ret.add(new ImplicitResolveResult(pyFunction, getImplicitResultRate(pyFunction, imports)));
        }
    }
    final Collection attributes = PyInstanceAttributeIndex.find(referencedName, project, scope);
    for (Object attribute : attributes) {
        if (!(attribute instanceof PyTargetExpression)) {
            FileBasedIndex.getInstance().scheduleRebuild(StubUpdatingIndex.INDEX_ID, new Throwable("found non-target expression object " + attribute + " in target expression list"));
            break;
        }
        ret.add(new ImplicitResolveResult((PyTargetExpression) attribute, getImplicitResultRate((PyTargetExpression) attribute, imports)));
    }
}
Also used : Project(com.intellij.openapi.project.Project) GlobalSearchScope(com.intellij.psi.search.GlobalSearchScope) QualifiedName(com.intellij.psi.util.QualifiedName) ImplicitResolveResult(com.jetbrains.python.psi.resolve.ImplicitResolveResult)

Aggregations

ImplicitResolveResult (com.jetbrains.python.psi.resolve.ImplicitResolveResult)2 Project (com.intellij.openapi.project.Project)1 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)1 QualifiedName (com.intellij.psi.util.QualifiedName)1 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)1 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)1