@NotNull private static List<RatedResolveResult> resolveInPackageDirectory( @Nullable PsiElement parent, @NotNull String referencedName, @Nullable PsiFile containingFile, boolean fileOnly, boolean checkForPackage) { final PsiElement parentDir = PyUtil.turnInitIntoDir(parent); if (parentDir instanceof PsiDirectory) { final List<RatedResolveResult> resolved = resolveInDirectory( referencedName, containingFile, (PsiDirectory) parentDir, fileOnly, checkForPackage); if (!resolved.isEmpty()) { for (RatedResolveResult result : resolved) { if (result.getRate() > RatedResolveResult.RATE_LOW) { return resolved; } } } if (parent instanceof PsiFile) { final PsiElement foreign = resolveForeignImports((PsiFile) parent, referencedName); if (foreign != null) { final ResolveResultList results = new ResolveResultList(); results.addAll(resolved); results.poke(foreign, RatedResolveResult.RATE_NORMAL); return results; } } return resolved; } return Collections.emptyList(); }
@NotNull private static List<RatedResolveResult> resolveInPackageModule( @NotNull PyFile parent, @NotNull String referencedName, @Nullable PsiFile containingFile, boolean fileOnly, boolean checkForPackage) { final List<RatedResolveResult> moduleMembers = resolveModuleMember(parent, referencedName); final List<RatedResolveResult> resolvedInModule = Lists.newArrayList(); final List<RatedResolveResult> results = Lists.newArrayList(); for (RatedResolveResult member : moduleMembers) { final PsiElement moduleMember = member.getElement(); if (!fileOnly || PyUtil.instanceOf(moduleMember, PsiFile.class, PsiDirectory.class)) { results.add(member); if (moduleMember != null && !preferResolveInDirectoryOverModule(moduleMember)) { resolvedInModule.add(member); } } } if (!resolvedInModule.isEmpty()) { return resolvedInModule; } final List<RatedResolveResult> resolvedInDirectory = resolveInPackageDirectory( parent, referencedName, containingFile, fileOnly, checkForPackage); if (!resolvedInDirectory.isEmpty()) { return resolvedInDirectory; } return results; }
/** @deprecated use {@link #multiResolveImportElement(PyImportElement, QualifiedName)} instead. */ @Deprecated @Nullable public static PsiElement resolveImportElement( PyImportElement importElement, @NotNull final QualifiedName qName) { final List<RatedResolveResult> resultList = RatedResolveResult.sorted(multiResolveImportElement(importElement, qName)); return resultList.size() > 0 ? resultList.get(0).getElement() : null; }
/** * @deprecated Use {@link #resolveChildren(PsiElement, String, PsiFile, boolean, boolean)} * instead. */ @Deprecated @Nullable public static PsiElement resolveChild( @Nullable final PsiElement parent, @NotNull final String referencedName, @Nullable final PsiFile containingFile, boolean fileOnly, boolean checkForPackage) { final List<RatedResolveResult> results = resolveChildren(parent, referencedName, containingFile, fileOnly, checkForPackage); return results.isEmpty() ? null : RatedResolveResult.sorted(results).get(0).getElement(); }
@NotNull public static List<RatedResolveResult> resolveNameInFromImport( PyFromImportStatement importStatement, @NotNull QualifiedName qName) { PsiFile file = importStatement.getContainingFile().getOriginalFile(); String name = qName.getComponents().get(0); final List<PsiElement> candidates = importStatement.resolveImportSourceCandidates(); List<PsiElement> resultList = new ArrayList<PsiElement>(); for (PsiElement candidate : candidates) { if (!candidate.isValid()) { throw new PsiInvalidElementAccessException( candidate, "Got an invalid candidate from resolveImportSourceCandidates(): " + candidate.getClass()); } if (candidate instanceof PsiDirectory) { candidate = PyUtil.getPackageElement((PsiDirectory) candidate, importStatement); } List<RatedResolveResult> results = resolveChildren(candidate, name, file, false, true); if (!results.isEmpty()) { for (RatedResolveResult result : results) { final PsiElement element = result.getElement(); if (element != null) { if (!element.isValid()) { throw new PsiInvalidElementAccessException( element, "Got an invalid candidate from resolveChild(): " + element.getClass()); } resultList.add(element); } } } } if (!resultList.isEmpty()) { return rateResults(resultList); } return Collections.emptyList(); }