Search in sources :

Example 16 with QualifiedName

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;
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName) ArrayList(java.util.ArrayList) Nullable(org.jetbrains.annotations.Nullable)

Example 17 with QualifiedName

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) QualifiedName(com.intellij.psi.util.QualifiedName) ArrayList(java.util.ArrayList) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with QualifiedName

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;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) QualifiedName(com.intellij.psi.util.QualifiedName) ResolveResultList(com.jetbrains.python.psi.impl.ResolveResultList) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with QualifiedName

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());
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) StringRef(com.intellij.util.io.StringRef) NotNull(org.jetbrains.annotations.NotNull)

Example 20 with QualifiedName

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;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

QualifiedName (com.intellij.psi.util.QualifiedName)63 Nullable (org.jetbrains.annotations.Nullable)22 NotNull (org.jetbrains.annotations.NotNull)17 VirtualFile (com.intellij.openapi.vfs.VirtualFile)9 Project (com.intellij.openapi.project.Project)8 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)7 PsiElement (com.intellij.psi.PsiElement)6 ArrayList (java.util.ArrayList)5 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 Sdk (com.intellij.openapi.projectRoots.Sdk)3 PsiFile (com.intellij.psi.PsiFile)3 PsiDirectory (com.intellij.psi.PsiDirectory)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)2 StringRef (com.intellij.util.io.StringRef)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)1 InjectedLanguageManager (com.intellij.lang.injection.InjectedLanguageManager)1