Search in sources :

Example 26 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyFileImpl method getPresentation.

@Override
public ItemPresentation getPresentation() {
    return new ItemPresentation() {

        @Override
        public String getPresentableText() {
            return getModuleName(PyFileImpl.this);
        }

        @Override
        public String getLocationString() {
            final String name = getLocationName();
            return name != null ? "(" + name + ")" : null;
        }

        @Override
        public Icon getIcon(final boolean open) {
            if (PyUtil.isPackage(PyFileImpl.this)) {
                return AllIcons.Modules.SourceFolder;
            }
            return PyFileImpl.this.getIcon(0);
        }

        @NotNull
        private String getModuleName(@NotNull PyFile file) {
            if (PyUtil.isPackage(file)) {
                final PsiDirectory dir = file.getContainingDirectory();
                if (dir != null) {
                    return dir.getName();
                }
            }
            return FileUtil.getNameWithoutExtension(file.getName());
        }

        @Nullable
        private String getLocationName() {
            final QualifiedName name = QualifiedNameFinder.findShortestImportableQName(PyFileImpl.this);
            if (name != null) {
                final QualifiedName prefix = name.removeTail(1);
                if (prefix.getComponentCount() > 0) {
                    return prefix.toString();
                }
            }
            final String relativePath = getRelativeContainerPath();
            if (relativePath != null) {
                return relativePath;
            }
            final PsiDirectory psiDirectory = getParent();
            if (psiDirectory != null) {
                return psiDirectory.getVirtualFile().getPresentableUrl();
            }
            return null;
        }

        @Nullable
        private String getRelativeContainerPath() {
            final PsiDirectory psiDirectory = getParent();
            if (psiDirectory != null) {
                final VirtualFile virtualFile = getVirtualFile();
                if (virtualFile != null) {
                    final VirtualFile root = ProjectFileIndex.SERVICE.getInstance(getProject()).getContentRootForFile(virtualFile);
                    if (root != null) {
                        final VirtualFile parent = virtualFile.getParent();
                        final VirtualFile rootParent = root.getParent();
                        if (rootParent != null && parent != null) {
                            return VfsUtilCore.getRelativePath(parent, rootParent, File.separatorChar);
                        }
                    }
                }
            }
            return null;
        }
    };
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) QualifiedName(com.intellij.psi.util.QualifiedName) ItemPresentation(com.intellij.navigation.ItemPresentation) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyFromImportStatementImpl method getImportSourceQName.

public QualifiedName getImportSourceQName() {
    final PyFromImportStatementStub stub = getStub();
    if (stub != null) {
        final QualifiedName qName = stub.getImportSourceQName();
        if (qName != null && qName.getComponentCount() == 0) {
            // relative import only: from .. import the_name
            return null;
        }
        return qName;
    }
    final PyReferenceExpression importSource = getImportSource();
    if (importSource == null) {
        return null;
    }
    return importSource.asQualifiedName();
}
Also used : PyFromImportStatementStub(com.jetbrains.python.psi.stubs.PyFromImportStatementStub) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 28 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyFromImportStatementImpl method resolveImplicitSubModule.

/**
   * The statement 'from pkg1.m1 import ...' makes 'm1' available as a local name in the package 'pkg1'.
   *
   * http://stackoverflow.com/questions/6048786/from-module-import-in-init-py-makes-module-name-visible
   */
@Nullable
private PsiElement resolveImplicitSubModule() {
    final QualifiedName importSourceQName = getImportSourceQName();
    if (importSourceQName != null) {
        final String name = importSourceQName.getLastComponent();
        final PsiFile file = getContainingFile();
        if (name != null && PyUtil.isPackage(file)) {
            final PsiElement resolvedImportSource = PyUtil.turnInitIntoDir(resolveImportSource());
            if (resolvedImportSource != null && resolvedImportSource.getParent() == file.getContainingDirectory()) {
                return resolvedImportSource;
            }
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 29 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyQualifiedReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    PyExpression qualifier = myElement.getQualifier();
    if (qualifier != null) {
        qualifier = CompletionUtil.getOriginalOrSelf(qualifier);
    }
    if (qualifier == null) {
        return EMPTY_ARRAY;
    }
    final PyQualifiedExpression element = CompletionUtil.getOriginalOrSelf(myElement);
    PyType qualifierType = TypeEvalContext.codeCompletion(element.getProject(), element.getContainingFile()).getType(qualifier);
    ProcessingContext ctx = new ProcessingContext();
    final Set<String> namesAlready = new HashSet<>();
    ctx.put(PyType.CTX_NAMES, namesAlready);
    final Collection<Object> variants = new ArrayList<>();
    if (qualifierType != null) {
        Collections.addAll(variants, getVariantFromHasAttr(qualifier));
        if (qualifierType instanceof PyStructuralType && ((PyStructuralType) qualifierType).isInferredFromUsages()) {
            final PyClassType guessedType = guessClassTypeByName();
            if (guessedType != null) {
                Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
            }
        }
        if (qualifier instanceof PyQualifiedExpression) {
            final PyQualifiedExpression qualifierExpression = (PyQualifiedExpression) qualifier;
            final QualifiedName qualifiedName = qualifierExpression.asQualifiedName();
            if (qualifiedName == null) {
                return variants.toArray();
            }
            final Collection<PyExpression> attrs = collectAssignedAttributes(qualifiedName, qualifier);
            for (PyExpression ex : attrs) {
                final String name = ex.getName();
                if (name != null && name.endsWith(CompletionUtil.DUMMY_IDENTIFIER_TRIMMED)) {
                    continue;
                }
                if (ex instanceof PsiNamedElement && qualifierType instanceof PyClassType && name != null) {
                    variants.add(LookupElementBuilder.createWithSmartPointer(name, ex).withTypeText(qualifierType.getName()).withIcon(PlatformIcons.FIELD_ICON));
                }
                if (ex instanceof PyReferenceExpression) {
                    PyReferenceExpression refExpr = (PyReferenceExpression) ex;
                    namesAlready.add(refExpr.getReferencedName());
                } else if (ex instanceof PyTargetExpression) {
                    PyTargetExpression targetExpr = (PyTargetExpression) ex;
                    namesAlready.add(targetExpr.getName());
                }
            }
            Collections.addAll(variants, qualifierType.getCompletionVariants(element.getName(), element, ctx));
            return variants.toArray();
        } else {
            return qualifierType.getCompletionVariants(element.getName(), element, ctx);
        }
    } else {
        final PyClassType guessedType = guessClassTypeByName();
        if (guessedType != null) {
            Collections.addAll(variants, getTypeCompletionVariants(myElement, guessedType));
        }
        if (qualifier instanceof PyReferenceExpression) {
            Collections.addAll(variants, collectSeenMembers(qualifier.getText()));
        }
        return variants.toArray();
    }
}
Also used : ProcessingContext(com.intellij.util.ProcessingContext) QualifiedName(com.intellij.psi.util.QualifiedName) NotNull(org.jetbrains.annotations.NotNull)

Example 30 with QualifiedName

use of com.intellij.psi.util.QualifiedName in project intellij-community by JetBrains.

the class PyImportStatementImpl method resolveImplicitSubModule.

/**
   * The statement 'import pkg1.m1' makes 'm1' available as a local name in the package 'pkg1'.
   *
   * http://stackoverflow.com/questions/6048786/from-module-import-in-init-py-makes-module-name-visible
   */
@Nullable
private PsiElement resolveImplicitSubModule() {
    final PyImportElement[] elements = getImportElements();
    if (elements.length == 1) {
        final PyImportElement element = elements[0];
        final QualifiedName importedQName = element.getImportedQName();
        final PsiFile file = element.getContainingFile();
        if (file != null) {
            if (importedQName != null && importedQName.getComponentCount() > 1 && PyUtil.isPackage(file)) {
                final QualifiedName packageQName = importedQName.removeLastComponent();
                final PsiElement resolvedImport = PyUtil.turnDirIntoInit(ResolveImportUtil.resolveImportElement(element, packageQName));
                if (resolvedImport == file) {
                    return element.resolve();
                }
            }
        }
    }
    return null;
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) 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