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;
}
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);
}
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());
}
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;
}
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;
}
Aggregations