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