Search in sources :

Example 1 with PyImportedModule

use of com.jetbrains.python.psi.impl.PyImportedModule in project intellij-community by JetBrains.

the class PyFindUsagesHandlerFactory method createFindUsagesHandler.

@Nullable
@Override
public FindUsagesHandler createFindUsagesHandler(@NotNull PsiElement element, boolean forHighlightUsages) {
    if (element instanceof PyImportedModule) {
        final PsiElement resolved = ((PyImportedModule) element).resolve();
        if (resolved != null) {
            element = resolved;
        }
    }
    if (element instanceof PsiFileSystemItem) {
        return new PyModuleFindUsagesHandler((PsiFileSystemItem) element);
    }
    if (element instanceof PyFunction) {
        if (!forHighlightUsages) {
            TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), null);
            final Collection<PsiElement> superMethods = PySuperMethodsSearch.search((PyFunction) element, true, context).findAll();
            if (superMethods.size() > 0) {
                final PsiElement next = superMethods.iterator().next();
                // TODO should do this for Jython functions overriding Java methods too
                if (next instanceof PyFunction && !isInObject((PyFunction) next)) {
                    int rc = Messages.showYesNoDialog(element.getProject(), "Method " + ((PyFunction) element).getName() + " overrides method of class " + ((PyFunction) next).getContainingClass().getName() + ".\nDo you want to find usages of the base method?", "Find Usages", Messages.getQuestionIcon());
                    if (rc == Messages.YES) {
                        List<PsiElement> allMethods = new ArrayList<>();
                        allMethods.add(element);
                        allMethods.addAll(superMethods);
                        return new PyFunctionFindUsagesHandler(element, allMethods);
                    } else {
                        return new PyFunctionFindUsagesHandler(element);
                    }
                }
            }
        }
        return new PyFunctionFindUsagesHandler(element);
    }
    if (element instanceof PyClass) {
        return new PyClassFindUsagesHandler((PyClass) element);
    }
    if (element instanceof PyTargetExpression) {
        return new PyTargetExpressionFindUsagesHandler(((PyTargetExpression) element));
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) PsiFileSystemItem(com.intellij.psi.PsiFileSystemItem) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PyImportedModule

use of com.jetbrains.python.psi.impl.PyImportedModule in project intellij-community by JetBrains.

the class PyModuleFindUsagesHandler method findReferencesToHighlight.

@NotNull
@Override
public Collection<PsiReference> findReferencesToHighlight(@NotNull PsiElement target, @NotNull SearchScope searchScope) {
    if (target instanceof PyImportedModule) {
        target = ((PyImportedModule) target).resolve();
    }
    if (target instanceof PyFile && PyNames.INIT_DOT_PY.equals(((PyFile) target).getName())) {
        List<PsiReference> result = new ArrayList<>();
        result.addAll(super.findReferencesToHighlight(target, searchScope));
        PsiElement targetDir = PyUtil.turnInitIntoDir(target);
        if (targetDir != null) {
            result.addAll(ReferencesSearch.search(targetDir, searchScope, false).findAll());
        }
        return result;
    }
    return super.findReferencesToHighlight(target, searchScope);
}
Also used : PyImportedModule(com.jetbrains.python.psi.impl.PyImportedModule) ArrayList(java.util.ArrayList) PsiReference(com.intellij.psi.PsiReference) PyFile(com.jetbrains.python.psi.PyFile) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyImportedModule

use of com.jetbrains.python.psi.impl.PyImportedModule 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

PyImportedModule (com.jetbrains.python.psi.impl.PyImportedModule)3 PsiElement (com.intellij.psi.PsiElement)2 ArrayList (java.util.ArrayList)2 PsiFileSystemItem (com.intellij.psi.PsiFileSystemItem)1 PsiReference (com.intellij.psi.PsiReference)1 QualifiedName (com.intellij.psi.util.QualifiedName)1 PyFile (com.jetbrains.python.psi.PyFile)1 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1