Search in sources :

Example 41 with QualifiedName

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

the class PyMoveFileHandler method updateRelativeImportsInModule.

private static void updateRelativeImportsInModule(@NotNull PyFile module) {
    final String originalLocation = module.getUserData(ORIGINAL_FILE_LOCATION);
    if (originalLocation == null) {
        return;
    }
    //module.putUserData(ORIGINAL_FILE_LOCATION, null);
    for (PyFromImportStatement statement : module.getFromImports()) {
        if (!canBeRelative(statement)) {
            continue;
        }
        final int relativeLevel = Math.max(statement.getRelativeLevel(), 1);
        final PsiFileSystemItem sourceElement = resolveRelativeImportFromModuleLocation(statement.getManager(), originalLocation, statement.getImportSource(), relativeLevel);
        if (sourceElement == null) {
            continue;
        }
        final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(sourceElement);
        replaceRelativeImportSourceWithQualifiedExpression(statement, newName);
    }
    for (PyImportElement importElement : module.getImportTargets()) {
        final PyReferenceExpression referenceExpr = importElement.getImportReferenceExpression();
        if (!canBeRelative(importElement) || referenceExpr == null) {
            continue;
        }
        final PsiFileSystemItem resolved = resolveRelativeImportFromModuleLocation(importElement.getManager(), originalLocation, referenceExpr, 1);
        if (resolved == null) {
            continue;
        }
        final QualifiedName newName = QualifiedNameFinder.findShortestImportableQName(resolved);
        replaceWithQualifiedExpression(referenceExpr, newName);
        final QualifiedName oldQualifiedName = referenceExpr.asQualifiedName();
        if (!Comparing.equal(oldQualifiedName, newName)) {
            final ScopeOwner scopeOwner = ScopeUtil.getScopeOwner(importElement);
            if (scopeOwner == null) {
                continue;
            }
            scopeOwner.accept(new PyRecursiveElementVisitor() {

                @Override
                public void visitPyReferenceExpression(PyReferenceExpression node) {
                    if (Comparing.equal(node.asQualifiedName(), oldQualifiedName)) {
                        replaceWithQualifiedExpression(node, newName);
                    } else {
                        super.visitPyReferenceExpression(node);
                    }
                }
            });
        }
    }
}
Also used : ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) QualifiedName(com.intellij.psi.util.QualifiedName)

Example 42 with QualifiedName

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

the class NumpyDocStringTypeProvider method resolveRedirectToFunction.

/**
   * Returns PyFunction object for specified fully qualified name accessible from specified reference.
   *
   * @param redirect  A fully qualified name of function that is redirected to.
   * @param reference An original reference element.
   * @return Resolved function or null if it was not resolved.
   */
@Nullable
private static PyFunction resolveRedirectToFunction(@NotNull String redirect, @NotNull PsiElement reference) {
    final QualifiedName qualifiedName = QualifiedName.fromDottedString(redirect);
    final String functionName = qualifiedName.getLastComponent();
    final List<PsiElement> items = PyResolveImportUtil.resolveQualifiedName(qualifiedName.removeLastComponent(), PyResolveImportUtil.fromFoothold(reference));
    for (PsiElement item : items) {
        if (item instanceof PsiDirectory) {
            item = ((PsiDirectory) item).findFile(PyNames.INIT_DOT_PY);
        }
        if (item instanceof PyFile) {
            final PsiElement element = ((PyFile) item).getElementNamed(functionName);
            if (element instanceof PyFunction) {
                return (PyFunction) element;
            }
        }
    }
    return null;
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) QualifiedName(com.intellij.psi.util.QualifiedName) NumpyDocString(com.jetbrains.python.documentation.docstrings.NumpyDocString) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with QualifiedName

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

the class PyQtTypeProvider method getReturnType.

@Override
public Ref<PyType> getReturnType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
    if (PyNames.INIT.equals(callable.getName()) && callable instanceof PyFunction) {
        final PyFunction function = (PyFunction) callable;
        final PyClass containingClass = function.getContainingClass();
        if (containingClass != null && ourQt4Signal.equals(containingClass.getName())) {
            final String classQName = containingClass.getQualifiedName();
            if (classQName != null) {
                final QualifiedName name = QualifiedName.fromDottedString(classQName);
                final String qtVersion = name.getComponents().get(0);
                final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, function.getProject());
                if (aClass != null) {
                    final PyType type = new PyClassTypeImpl(aClass, false);
                    return Ref.create(type);
                }
            }
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) PyType(com.jetbrains.python.psi.types.PyType)

Example 44 with QualifiedName

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

the class PyQtTypeProvider method getCallableType.

@Nullable
@Override
public PyType getCallableType(@NotNull PyCallable callable, @NotNull TypeEvalContext context) {
    if (callable instanceof PyFunction) {
        final String qualifiedName = callable.getQualifiedName();
        if (qualifiedName != null && qualifiedName.startsWith("PyQt")) {
            final QualifiedName name = QualifiedName.fromDottedString(qualifiedName);
            final String qtVersion = name.getComponents().get(0);
            final String docstring = ((PyFunction) callable).getDocStringValue();
            if (docstring != null && docstring.contains("[signal]")) {
                final PyClass aClass = PyClassNameIndex.findClass(qtVersion + "." + ourQtBoundSignal, callable.getProject());
                if (aClass != null)
                    return new PyClassTypeImpl(aClass, false);
            }
        }
    }
    return null;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 45 with QualifiedName

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

the class PyClassRefactoringUtil method rememberReference.

private static void rememberReference(@NotNull PyReferenceExpression node, @NotNull PsiElement element) {
    // We will remember reference in deepest node (except for references to PyImportedModules, as we need references to modules, not to
    // their packages)
    final PyExpression qualifier = node.getQualifier();
    if (qualifier != null && !(resolveExpression(qualifier) instanceof PyImportedModule)) {
        return;
    }
    final List<PsiElement> allResolveResults = multiResolveExpression(node);
    PsiElement target = ContainerUtil.getFirstItem(allResolveResults);
    if (target instanceof PsiNamedElement && !PsiTreeUtil.isAncestor(element, target, false)) {
        final PyImportedNameDefiner importElement = getImportElement(node);
        if (!PyUtil.inSameFile(element, target) && importElement == null && !(target instanceof PsiFileSystemItem)) {
            return;
        }
        if (target instanceof PyTargetExpression && PyNames.ALL.equals(((PyTargetExpression) target).getName())) {
            for (PsiElement result : allResolveResults) {
                if (result instanceof PyImportElement) {
                    final QualifiedName importedQName = ((PyImportElement) result).getImportedQName();
                    if (importedQName != null) {
                        target = new DynamicNamedElement(target.getContainingFile(), importedQName.toString());
                        break;
                    }
                }
            }
        }
        node.putCopyableUserData(ENCODED_IMPORT, (PsiNamedElement) target);
        if (importElement instanceof PyImportElement) {
            node.putCopyableUserData(ENCODED_IMPORT_AS, ((PyImportElement) importElement).getAsName());
        }
        node.putCopyableUserData(ENCODED_USE_FROM_IMPORT, qualifier == null);
    }
}
Also used : QualifiedName(com.intellij.psi.util.QualifiedName) PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule)

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