use of com.jetbrains.python.psi.impl.ResolveResultList in project intellij-community by JetBrains.
the class DocStringTypeReference method multiResolve.
@NotNull
@Override
public ResolveResult[] multiResolve(boolean incompleteCode) {
PsiElement result = null;
final ResolveResultList results = new ResolveResultList();
if (myType instanceof PyClassType) {
result = ((PyClassType) myType).getPyClass();
} else if (myType instanceof PyImportedModuleType) {
result = ((PyImportedModuleType) myType).getImportedModule().resolve();
} else if (myType instanceof PyModuleType) {
result = ((PyModuleType) myType).getModule();
}
if (result != null) {
if (myImportElement != null) {
results.add(new ImportedResolveResult(result, RatedResolveResult.RATE_NORMAL, myImportElement));
} else {
results.poke(result, RatedResolveResult.RATE_NORMAL);
}
}
return results.toArray(ResolveResult.EMPTY_ARRAY);
}
use of com.jetbrains.python.psi.impl.ResolveResultList in project intellij-community by JetBrains.
the class PyModuleType method resolveImplicitPackageMember.
@Nullable
private List<? extends RatedResolveResult> resolveImplicitPackageMember(@NotNull String name, @NotNull List<PyImportElement> importElements) {
final VirtualFile moduleFile = myModule.getVirtualFile();
if (moduleFile != null) {
for (QualifiedName packageQName : QualifiedNameFinder.findImportableQNames(myModule, myModule.getVirtualFile())) {
final QualifiedName resolvingQName = packageQName.append(name);
for (PyImportElement importElement : importElements) {
for (QualifiedName qName : getImportedQNames(importElement)) {
if (qName.matchesPrefix(resolvingQName)) {
final PsiElement subModule = ResolveImportUtil.resolveChild(myModule, name, myModule, false, true, false);
if (subModule != null) {
final ResolveResultList results = new ResolveResultList();
results.add(new ImportedResolveResult(subModule, RatedResolveResult.RATE_NORMAL, importElement));
return results;
}
}
}
}
}
}
return null;
}
use of com.jetbrains.python.psi.impl.ResolveResultList in project intellij-community by JetBrains.
the class PyClassTypeImpl method doResolveMember.
@Nullable
private List<? extends RatedResolveResult> doResolveMember(@NotNull String name, @Nullable PyExpression location, @NotNull AccessDirection direction, @NotNull PyResolveContext resolveContext, boolean inherited) {
final TypeEvalContext context = resolveContext.getTypeEvalContext();
PsiElement classMember = //overriding members provers have priority to normal resolve
resolveByOverridingMembersProviders(this, name, location, context);
if (classMember != null) {
return ResolveResultList.to(classMember);
}
if (resolveContext.allowProperties()) {
final Ref<ResolveResultList> resultRef = findProperty(name, direction, true, resolveContext.getTypeEvalContext());
if (resultRef != null) {
return resultRef.get();
}
}
if ("super".equals(getClassQName()) && isBuiltin() && location instanceof PyCallExpression) {
// methods of super() call are not of class super!
PyExpression first_arg = ((PyCallExpression) location).getArgument(0, PyExpression.class);
if (first_arg != null) {
// the usual case: first arg is the derived class that super() is proxying for
PyType first_arg_type = context.getType(first_arg);
if (first_arg_type instanceof PyClassType) {
PyClass derived_class = ((PyClassType) first_arg_type).getPyClass();
final Iterator<PyClass> base_it = derived_class.getAncestorClasses(context).iterator();
if (base_it.hasNext()) {
return new PyClassTypeImpl(base_it.next(), true).resolveMember(name, location, direction, resolveContext);
} else {
// no base classes = super() cannot proxy anything meaningful from a base class
return null;
}
}
}
}
final List<? extends RatedResolveResult> classMembers = resolveInner(myClass, myIsDefinition, name, location);
if (PyNames.__CLASS__.equals(name)) {
return resolveDunderClass(context, classMembers);
}
if (!classMembers.isEmpty()) {
return classMembers;
}
if (PyNames.DOC.equals(name)) {
return Optional.ofNullable(PyBuiltinCache.getInstance(myClass).getObjectType()).map(type -> type.resolveMember(name, location, direction, resolveContext)).orElse(Collections.emptyList());
}
classMember = resolveByOverridingAncestorsMembersProviders(this, name, location);
if (classMember != null) {
final ResolveResultList list = new ResolveResultList();
int rate = RatedResolveResult.RATE_NORMAL;
for (PyResolveResultRater rater : Extensions.getExtensions(PyResolveResultRater.EP_NAME)) {
rate += rater.getMemberRate(classMember, this, context);
}
list.poke(classMember, rate);
return list;
}
if (inherited) {
for (PyClassLikeType type : myClass.getAncestorTypes(context)) {
if (type instanceof PyClassType) {
if (!myIsDefinition) {
type = type.toInstance();
}
final List<? extends RatedResolveResult> superMembers = resolveInner(((PyClassType) type).getPyClass(), myIsDefinition, name, location);
if (!superMembers.isEmpty()) {
return superMembers;
}
}
if (type != null) {
final List<? extends RatedResolveResult> results = type.resolveMember(name, location, direction, resolveContext, false);
if (results != null && !results.isEmpty()) {
return results;
}
}
}
}
if (inherited && !PyNames.INIT.equals(name) && !PyNames.NEW.equals(name)) {
final List<? extends RatedResolveResult> typeMembers = resolveMetaClassMember(name, location, direction, resolveContext);
if (typeMembers != null) {
return typeMembers;
}
}
if (inherited) {
classMember = resolveByMembersProviders(this, name, location, //ask providers after real class introspection as providers have less priority
context);
}
if (classMember != null) {
return ResolveResultList.to(classMember);
}
if (inherited) {
for (PyClassLikeType type : myClass.getAncestorTypes(context)) {
if (type instanceof PyClassType) {
final PyClass pyClass = ((PyClassType) type).getPyClass();
PsiElement superMember = resolveByMembersProviders(new PyClassTypeImpl(pyClass, isDefinition()), name, location, resolveContext.getTypeEvalContext());
if (superMember != null) {
return ResolveResultList.to(superMember);
}
}
}
}
return Collections.emptyList();
}
use of com.jetbrains.python.psi.impl.ResolveResultList in project intellij-community by JetBrains.
the class PyClassTypeImpl method findProperty.
private Ref<ResolveResultList> findProperty(String name, AccessDirection direction, boolean inherited, @Nullable TypeEvalContext context) {
Ref<ResolveResultList> resultRef = null;
Property property = myClass.findProperty(name, inherited, context);
if (property != null) {
Maybe<PyCallable> accessor = property.getByDirection(direction);
if (accessor.isDefined()) {
PyCallable accessor_code = accessor.value();
ResolveResultList ret = new ResolveResultList();
if (accessor_code != null)
ret.poke(accessor_code, RatedResolveResult.RATE_NORMAL);
PyTargetExpression site = property.getDefinitionSite();
if (site != null)
ret.poke(site, RatedResolveResult.RATE_LOW);
if (ret.size() > 0) {
resultRef = Ref.create(ret);
} else {
resultRef = Ref.create();
}
// property is found, but the required accessor is explicitly absent
}
}
return resultRef;
}
use of com.jetbrains.python.psi.impl.ResolveResultList in project intellij-community by JetBrains.
the class PyQualifiedReference method resolveInner.
@NotNull
@Override
protected List<RatedResolveResult> resolveInner() {
PyPsiUtils.assertValid(myElement);
ResolveResultList ret = new ResolveResultList();
final String referencedName = myElement.getReferencedName();
if (referencedName == null)
return ret;
final PyExpression qualifier = myElement.getQualifier();
PyPsiUtils.assertValid(qualifier);
if (qualifier == null) {
return ret;
}
// regular attributes
PyType qualifierType = myContext.getTypeEvalContext().getType(qualifier);
// is it a class-private name qualified by a different class?
if (PyUtil.isClassPrivateName(referencedName) && qualifierType instanceof PyClassType) {
if (isOtherClassQualifying(qualifier, (PyClassType) qualifierType))
return Collections.emptyList();
}
//
if (qualifierType != null) {
qualifierType.assertValid("qualifier: " + qualifier);
// resolve within the type proper
AccessDirection ctx = AccessDirection.of(myElement);
final List<? extends RatedResolveResult> membersOfQualifier = qualifierType.resolveMember(referencedName, qualifier, ctx, myContext);
if (membersOfQualifier == null) {
// qualifier is positive that such name cannot exist in it
return ret;
}
ret.addAll(membersOfQualifier);
}
// look for assignment of this attribute in containing function
if (qualifier instanceof PyQualifiedExpression && ret.isEmpty()) {
if (addAssignedAttributes(ret, referencedName, (PyQualifiedExpression) qualifier)) {
return ret;
}
}
if ((PyTypeChecker.isUnknown(qualifierType) || (qualifierType instanceof PyStructuralType && ((PyStructuralType) qualifierType).isInferredFromUsages())) && myContext.allowImplicits() && canQualifyAnImplicitName(qualifier)) {
addImplicitResolveResults(referencedName, ret);
}
// special case of __doc__
if ("__doc__".equals(referencedName)) {
addDocReference(ret, qualifier, qualifierType);
}
return ret;
}
Aggregations