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