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;
}
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);
}
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);
}
}
Aggregations