use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyTypingTypeProvider method tryResolving.
@NotNull
private static List<PsiElement> tryResolving(@NotNull PyExpression expression, @NotNull TypeEvalContext context) {
final List<PsiElement> elements = Lists.newArrayList();
if (expression instanceof PyReferenceExpression) {
final PyReferenceExpression referenceExpr = (PyReferenceExpression) expression;
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
final PsiPolyVariantReference reference = referenceExpr.getReference(resolveContext);
final List<PsiElement> resolved = PyUtil.multiResolveTopPriority(reference);
for (PsiElement element : resolved) {
if (element instanceof PyFunction) {
final PyFunction function = (PyFunction) element;
if (PyUtil.isInit(function)) {
final PyClass cls = function.getContainingClass();
if (cls != null) {
elements.add(cls);
continue;
}
}
}
final String name = element != null ? getQualifiedName(element) : null;
if (name != null && OPAQUE_NAMES.contains(name)) {
elements.add(element);
continue;
}
if (element instanceof PyTargetExpression) {
final PyTargetExpression targetExpr = (PyTargetExpression) element;
// XXX: Requires switching from stub to AST
final PyExpression assignedValue = targetExpr.findAssignedValue();
if (assignedValue != null) {
elements.add(assignedValue);
continue;
}
}
if (element != null) {
elements.add(element);
}
}
}
return !elements.isEmpty() ? elements : Collections.singletonList(expression);
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyUnusedLocalInspectionVisitor method isRangeIteration.
private boolean isRangeIteration(@NotNull PyForStatement forStatement) {
final PyExpression source = forStatement.getForPart().getSource();
if (!(source instanceof PyCallExpression)) {
return false;
}
final PyCallExpression expr = (PyCallExpression) source;
if (expr.isCalleeText("range", "xrange")) {
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(myTypeEvalContext);
final PyBuiltinCache builtinCache = PyBuiltinCache.getInstance(forStatement);
return ContainerUtil.exists(expr.multiResolveCalleeFunction(resolveContext), builtinCache::isBuiltin);
}
return false;
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyReplaceTupleWithListQuickFix method applyFix.
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement element = descriptor.getPsiElement();
assert element instanceof PyAssignmentStatement;
PyExpression[] targets = ((PyAssignmentStatement) element).getTargets();
if (targets.length == 1 && targets[0] instanceof PySubscriptionExpression) {
PySubscriptionExpression subscriptionExpression = (PySubscriptionExpression) targets[0];
if (subscriptionExpression.getOperand() instanceof PyReferenceExpression) {
PyReferenceExpression referenceExpression = (PyReferenceExpression) subscriptionExpression.getOperand();
final TypeEvalContext context = TypeEvalContext.userInitiated(project, element.getContainingFile());
final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
element = referenceExpression.followAssignmentsChain(resolveContext).getElement();
if (element instanceof PyParenthesizedExpression) {
final PyExpression expression = ((PyParenthesizedExpression) element).getContainedExpression();
replaceWithListLiteral(element, (PyTupleExpression) expression);
} else if (element instanceof PyTupleExpression) {
replaceWithListLiteral(element, (PyTupleExpression) element);
}
}
}
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyRemoveParameterQuickFix method applyFix.
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement parameter = descriptor.getPsiElement();
assert parameter instanceof PyParameter;
final PyFunction function = PsiTreeUtil.getParentOfType(parameter, PyFunction.class);
if (function != null) {
final PyResolveContext resolveContext = PyResolveContext.noImplicits();
StreamEx.of(PyRefactoringUtil.findUsages(function, false)).map(UsageInfo::getElement).nonNull().map(PsiElement::getParent).select(PyCallExpression.class).flatMap(callExpression -> callExpression.multiMapArguments(resolveContext).stream()).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == parameter).forEach(entry -> entry.getKey().delete());
final PyStringLiteralExpression docStringExpression = function.getDocStringExpression();
final String parameterName = ((PyParameter) parameter).getName();
if (docStringExpression != null && parameterName != null) {
PyDocstringGenerator.forDocStringOwner(function).withoutParam(parameterName).buildAndInsert();
}
}
parameter.delete();
}
use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.
the class PyMoveFileHandler method retargetUsages.
@Override
public void retargetUsages(List<UsageInfo> usages, Map<PsiElement, PsiElement> oldToNewMap) {
final Set<PsiFile> updatedFiles = new HashSet<>();
for (UsageInfo usage : usages) {
final PsiElement usageElement = usage.getElement();
if (usageElement != null) {
final PsiNamedElement movedElement = usageElement.getCopyableUserData(REFERENCED_ELEMENT);
usageElement.putCopyableUserData(REFERENCED_ELEMENT, null);
if (movedElement != null) {
final PsiFile usageFile = usageElement.getContainingFile();
final PyImportStatementBase importStmt = PsiTreeUtil.getParentOfType(usageElement, PyImportStatementBase.class);
// TODO: Retarget qualified expressions in docstrings
if (importStmt != null) {
if (usageFile.getUserData(ORIGINAL_FILE_LOCATION) != null) {
// Leave relative imports as they are after #updateRelativeImportsInModule
final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(usageFile.getProject(), usageFile);
final PyResolveContext resolveContext = PyResolveContext.defaultContext().withTypeEvalContext(typeEvalContext);
if (ContainerUtil.getFirstItem(PyUtil.multiResolveTopPriority(usageElement, resolveContext)) == movedElement) {
continue;
}
}
updatedFiles.add(usageFile);
final boolean usageInsideImportElement = PsiTreeUtil.getParentOfType(usageElement, PyImportElement.class) != null;
if (usageInsideImportElement) {
// or simple unqualified import of the module (import module).
if (PyClassRefactoringUtil.updateUnqualifiedImportOfElement(importStmt, movedElement)) {
continue;
}
}
final QualifiedName newElementName = QualifiedNameFinder.findCanonicalImportPath(movedElement, usageElement);
if (importStmt instanceof PyFromImportStatement) {
if (!usageInsideImportElement) {
replaceRelativeImportSourceWithQualifiedExpression((PyFromImportStatement) importStmt, newElementName);
}
} else {
replaceWithQualifiedExpression(usageElement, newElementName);
}
} else if (usageElement instanceof PyReferenceExpression) {
updatedFiles.add(usageFile);
if (((PyReferenceExpression) usageElement).isQualified()) {
final QualifiedName newQualifiedName = QualifiedNameFinder.findCanonicalImportPath(movedElement, usageElement);
replaceWithQualifiedExpression(usageElement, newQualifiedName);
} else {
final QualifiedName newName = QualifiedName.fromComponents(PyClassRefactoringUtil.getOriginalName(movedElement));
replaceWithQualifiedExpression(usageElement, newName);
}
}
}
}
}
if (!updatedFiles.isEmpty()) {
final PyImportOptimizer optimizer = new PyImportOptimizer();
for (PsiFile file : updatedFiles) {
final boolean injectedFragment = InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file);
if (!injectedFragment) {
optimizer.processFile(file).run();
}
}
}
}
Aggregations