Search in sources :

Example 6 with PyResolveContext

use of com.jetbrains.python.psi.resolve.PyResolveContext 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 7 with PyResolveContext

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

the class PythonPatterns method multiResolveCalledFunction.

@NotNull
private static List<PyCallable> multiResolveCalledFunction(@Nullable Object expression, @Nullable String functionName, int index) {
    if (!isCallArgument(expression, functionName, index)) {
        return Collections.emptyList();
    }
    final PyCallExpression call = (PyCallExpression) ((PyExpression) expression).getParent().getParent();
    // TODO is it better or worse to allow implicits here?
    final PyResolveContext context = PyResolveContext.noImplicits().withTypeEvalContext(TypeEvalContext.codeAnalysis(call.getProject(), call.getContainingFile()));
    return call.multiResolveCalleeFunction(context);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with PyResolveContext

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

the class PyBaseElementImpl method findReferenceAt.

/**
   * Overrides the findReferenceAt() logic in order to provide a resolve context with origin file for returned references.
   * The findReferenceAt() is usually invoked from UI operations, and it helps to be able to do deeper analysis in the
   * current file for such operations.
   *
   * @param offset the offset to find the reference at
   * @return the reference or null.
   */
@Override
public PsiReference findReferenceAt(int offset) {
    // copy/paste from SharedPsiElementImplUtil
    PsiElement element = findElementAt(offset);
    if (element == null || element instanceof OuterLanguageElement)
        return null;
    offset = getTextRange().getStartOffset() + offset - element.getTextRange().getStartOffset();
    List<PsiReference> referencesList = new ArrayList<>();
    final PsiFile file = element.getContainingFile();
    final PyResolveContext resolveContext = file != null ? PyResolveContext.defaultContext().withTypeEvalContext(TypeEvalContext.codeAnalysis(file.getProject(), file)) : PyResolveContext.defaultContext();
    while (element != null) {
        addReferences(offset, element, referencesList, resolveContext);
        offset = element.getStartOffsetInParent() + offset;
        if (element instanceof PsiFile)
            break;
        element = element.getParent();
    }
    if (referencesList.isEmpty())
        return null;
    if (referencesList.size() == 1)
        return referencesList.get(0);
    return new PsiMultiReference(referencesList.toArray(new PsiReference[referencesList.size()]), referencesList.get(referencesList.size() - 1).getElement());
}
Also used : OuterLanguageElement(com.intellij.psi.templateLanguages.OuterLanguageElement) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) ArrayList(java.util.ArrayList) PsiMultiReference(com.intellij.psi.impl.source.resolve.reference.impl.PsiMultiReference)

Example 9 with PyResolveContext

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

the class PyParameterInfoHandler method findElementForParameterInfo.

@Override
@Nullable
public PyArgumentList findElementForParameterInfo(@NotNull CreateParameterInfoContext context) {
    final PyArgumentList argumentList = findArgumentList(context, -1);
    if (argumentList != null) {
        final PyCallExpression call = argumentList.getCallExpression();
        if (call != null) {
            final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(argumentList.getProject(), argumentList.getContainingFile());
            final PyResolveContext resolveContext = PyResolveContext.noImplicits().withRemote().withTypeEvalContext(typeEvalContext);
            final Object[] items = PyUtil.filterTopPriorityResults(call.multiResolveRatedCallee(resolveContext)).stream().map(ratedMarkedCallee -> Pair.createNonNull(call, ratedMarkedCallee.getMarkedCallee())).toArray();
            context.setItemsToShow(items);
            return argumentList;
        }
    }
    return null;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) com.intellij.lang.parameterInfo(com.intellij.lang.parameterInfo) java.util(java.util) ArrayUtil(com.intellij.util.ArrayUtil) LookupElement(com.intellij.codeInsight.lookup.LookupElement) ParamHelper(com.jetbrains.python.psi.impl.ParamHelper) TextRange(com.intellij.openapi.util.TextRange) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PyPsiUtils(com.jetbrains.python.psi.impl.PyPsiUtils) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) Nullable(org.jetbrains.annotations.Nullable) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) Pair(com.intellij.openapi.util.Pair) PsiElement(com.intellij.psi.PsiElement) PsiFile(com.intellij.psi.PsiFile) XmlStringUtil(com.intellij.xml.util.XmlStringUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) PyMarkedCallee(com.jetbrains.python.psi.PyCallExpression.PyMarkedCallee) PyCallExpressionHelper(com.jetbrains.python.psi.impl.PyCallExpressionHelper) NotNull(org.jetbrains.annotations.NotNull) CharArrayUtil(com.intellij.util.text.CharArrayUtil) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) Nullable(org.jetbrains.annotations.Nullable)

Example 10 with PyResolveContext

use of com.jetbrains.python.psi.resolve.PyResolveContext 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)

Aggregations

PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)27 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)11 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)6 com.jetbrains.python.psi (com.jetbrains.python.psi)6 ContainerUtil (com.intellij.util.containers.ContainerUtil)5 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)5 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)5 java.util (java.util)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 Extensions (com.intellij.openapi.extensions.Extensions)4 PsiFile (com.intellij.psi.PsiFile)4 ArrayUtil (com.intellij.util.ArrayUtil)4 Processor (com.intellij.util.Processor)4 PyNames (com.jetbrains.python.PyNames)4 Collectors (java.util.stream.Collectors)4 Sets (com.google.common.collect.Sets)3 Pair (com.intellij.openapi.util.Pair)3 QualifiedName (com.intellij.psi.util.QualifiedName)3