use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyModuleType method collectImportedSubmodules.
@Nullable
public static List<PsiElement> collectImportedSubmodules(@NotNull PsiFileSystemItem pyPackage, @NotNull PsiElement location) {
final PsiElement parentAnchor;
if (pyPackage instanceof PyFile && PyUtil.isPackage(((PyFile) pyPackage))) {
parentAnchor = ((PyFile) pyPackage).getContainingDirectory();
} else if (pyPackage instanceof PsiDirectory && PyUtil.isPackage(((PsiDirectory) pyPackage), location)) {
parentAnchor = pyPackage;
} else {
return null;
}
final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(location);
if (scopeOwner == null) {
return Collections.emptyList();
}
final List<PsiElement> result = new ArrayList<>();
nextImportElement: for (PyImportElement importElement : getVisibleImports(scopeOwner)) {
PsiElement resolvedChild = PyUtil.turnInitIntoDir(importElement.resolve());
if (resolvedChild == null || !PsiTreeUtil.isAncestor(parentAnchor, resolvedChild, true)) {
continue;
}
QualifiedName importedQName = importElement.getImportedQName();
// Looking for strict child of parentAncestor
while (resolvedChild != null && resolvedChild.getParent() != parentAnchor) {
if (importedQName == null || importedQName.getComponentCount() <= 1) {
continue nextImportElement;
}
importedQName = importedQName.removeTail(1);
resolvedChild = PyUtil.turnInitIntoDir(ResolveImportUtil.resolveImportElement(importElement, importedQName));
}
ContainerUtil.addIfNotNull(result, resolvedChild);
}
return result;
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyModuleType method getImportedQNames.
@NotNull
private static List<QualifiedName> getImportedQNames(@NotNull PyImportElement element) {
final List<QualifiedName> importedQNames = new ArrayList<>();
final PyStatement stmt = element.getContainingImportStatement();
if (stmt instanceof PyFromImportStatement) {
final PyFromImportStatement fromImportStatement = (PyFromImportStatement) stmt;
final QualifiedName importedQName = fromImportStatement.getImportSourceQName();
final String visibleName = element.getVisibleName();
if (importedQName != null) {
importedQNames.add(importedQName);
final QualifiedName implicitSubModuleQName = importedQName.append(visibleName);
if (implicitSubModuleQName != null) {
importedQNames.add(implicitSubModuleQName);
}
} else {
final List<PsiElement> elements = ResolveImportUtil.resolveFromImportStatementSource(fromImportStatement, element.getImportedQName());
for (PsiElement psiElement : elements) {
if (psiElement instanceof PsiFile) {
final VirtualFile virtualFile = ((PsiFile) psiElement).getVirtualFile();
final QualifiedName qName = QualifiedNameFinder.findShortestImportableQName(element, virtualFile);
if (qName != null) {
importedQNames.add(qName);
}
}
}
}
} else if (stmt instanceof PyImportStatement) {
final QualifiedName importedQName = element.getImportedQName();
if (importedQName != null) {
importedQNames.add(importedQName);
}
}
if (!ResolveImportUtil.isAbsoluteImportEnabledFor(element)) {
PsiFile file = element.getContainingFile();
if (file != null) {
file = file.getOriginalFile();
}
final QualifiedName absoluteQName = QualifiedNameFinder.findShortestImportableQName(file);
if (file != null && absoluteQName != null) {
final QualifiedName prefixQName = PyUtil.isPackage(file) ? absoluteQName : absoluteQName.removeLastComponent();
if (prefixQName.getComponentCount() > 0) {
final List<QualifiedName> results = new ArrayList<>();
results.addAll(importedQNames);
for (QualifiedName qName : importedQNames) {
final List<String> components = new ArrayList<>();
components.addAll(prefixQName.getComponents());
components.addAll(qName.getComponents());
results.add(QualifiedName.fromComponents(components));
}
return results;
}
}
}
return importedQNames;
}
use of com.intellij.psi.util.QualifiedName 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.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyImportElementElementType method deserialize.
@NotNull
public PyImportElementStub deserialize(@NotNull StubInputStream dataStream, StubElement parentStub) throws IOException {
QualifiedName qName = QualifiedName.deserialize(dataStream);
StringRef asName = dataStream.readName();
return new PyImportElementStubImpl(qName, asName.getString(), parentStub, getStubElementType());
}
use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.
the class PyNamedTupleStubImpl method create.
@Nullable
public static PyNamedTupleStub create(@NotNull PyCallExpression expression) {
final PyReferenceExpression calleeReference = PyUtil.as(expression.getCallee(), PyReferenceExpression.class);
if (calleeReference == null) {
return null;
}
final QualifiedName namedTupleQName = getNamedTupleQName(calleeReference);
if (namedTupleQName != null) {
final String name = resolveTupleName(expression);
if (name == null) {
return null;
}
final List<String> fields = resolveTupleFields(expression);
if (fields == null) {
return null;
}
return new PyNamedTupleStubImpl(namedTupleQName, name, fields);
}
return null;
}
Aggregations