Search in sources :

Example 16 with PyResolveContext

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);
            }
        }
    }
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 17 with PyResolveContext

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();
}
Also used : PyBundle(com.jetbrains.python.PyBundle) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) UsageInfo(com.intellij.usageView.UsageInfo) PyParameter(com.jetbrains.python.psi.PyParameter) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PyRefactoringUtil(com.jetbrains.python.refactoring.PyRefactoringUtil) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) StreamEx(one.util.streamex.StreamEx) PsiElement(com.intellij.psi.PsiElement) Project(com.intellij.openapi.project.Project) PyDocstringGenerator(com.jetbrains.python.documentation.docstrings.PyDocstringGenerator) NotNull(org.jetbrains.annotations.NotNull) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PyFunction(com.jetbrains.python.psi.PyFunction) PyFunction(com.jetbrains.python.psi.PyFunction) PyStringLiteralExpression(com.jetbrains.python.psi.PyStringLiteralExpression) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 18 with PyResolveContext

use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.

the class PyGotoDeclarationHandler method getGotoDeclarationTarget.

@Nullable
@Override
public PsiElement getGotoDeclarationTarget(@Nullable final PsiElement sourceElement, final Editor editor) {
    if (sourceElement == null) {
        return null;
    }
    final PyResolveContext context = PyResolveContext.noImplicits().withTypeEvalContext(TypeEvalContext.userInitiated(sourceElement.getProject(), sourceElement.getContainingFile()));
    PyReferenceOwner referenceOwner = null;
    final PsiElement parent = sourceElement.getParent();
    if (sourceElement instanceof PyReferenceOwner) {
        referenceOwner = (PyReferenceOwner) sourceElement;
    } else if (parent instanceof PyReferenceOwner) {
        //Reference expression may be parent of IDENTIFIER
        referenceOwner = (PyReferenceOwner) parent;
    }
    if (referenceOwner != null) {
        return referenceOwner.getReference(context).resolve();
    }
    // If element is not ref owner, it still may have provided references, lets find some
    final PsiElement element = findProvidedReferenceAndResolve(sourceElement);
    if (element != null) {
        return element;
    }
    if (parent != null) {
        return findProvidedReferenceAndResolve(parent);
    }
    return null;
}
Also used : PyReferenceOwner(com.jetbrains.python.psi.PyReferenceOwner) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 19 with PyResolveContext

use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.

the class PyNamedParameterImpl method getType.

public PyType getType(@NotNull final TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final PsiElement parent = getParentByStub();
    if (parent instanceof PyParameterList) {
        PyParameterList parameterList = (PyParameterList) parent;
        PyFunction func = parameterList.getContainingFunction();
        if (func != null) {
            for (PyTypeProvider provider : Extensions.getExtensions(PyTypeProvider.EP_NAME)) {
                final Ref<PyType> resultRef = provider.getParameterType(this, func, context);
                if (resultRef != null) {
                    return resultRef.get();
                }
            }
            if (isSelf()) {
                // must be 'self' or 'cls'
                final PyClass containingClass = func.getContainingClass();
                if (containingClass != null) {
                    final PyFunction.Modifier modifier = func.getModifier();
                    return new PyClassTypeImpl(containingClass, modifier == PyFunction.Modifier.CLASSMETHOD);
                }
            }
            if (isKeywordContainer()) {
                return PyTypeUtil.toKeywordContainerType(this, null);
            }
            if (isPositionalContainer()) {
                return PyTypeUtil.toPositionalContainerType(this, null);
            }
            if (context.maySwitchToAST(this)) {
                final PyExpression defaultValue = getDefaultValue();
                if (defaultValue != null) {
                    final PyType type = context.getType(defaultValue);
                    if (type != null && !(type instanceof PyNoneType)) {
                        if (type instanceof PyTupleType) {
                            return PyUnionType.createWeakType(type);
                        }
                        return type;
                    }
                }
            }
            // Guess the type from file-local calls
            if (context.allowCallContext(this)) {
                final List<PyType> types = new ArrayList<>();
                final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
                processLocalCalls(func, call -> {
                    StreamEx.of(call.multiMapArguments(resolveContext)).flatMap(mapping -> mapping.getMappedParameters().entrySet().stream()).filter(entry -> entry.getValue() == this).map(Map.Entry::getKey).nonNull().map(context::getType).nonNull().forEach(types::add);
                    return true;
                });
                if (!types.isEmpty()) {
                    return PyUnionType.createWeakType(PyUnionType.union(types));
                }
            }
            if (context.maySwitchToAST(this)) {
                final Set<String> attributes = collectUsedAttributes(context);
                if (!attributes.isEmpty()) {
                    return new PyStructuralType(attributes, true);
                }
            }
        }
    }
    return null;
}
Also used : PyNames(com.jetbrains.python.PyNames) java.util(java.util) PythonDocumentationProvider(com.jetbrains.python.documentation.PythonDocumentationProvider) ScopeUtil(com.jetbrains.python.codeInsight.dataflow.scope.ScopeUtil) SearchScope(com.intellij.psi.search.SearchScope) ContainerUtil(com.intellij.util.containers.ContainerUtil) PyElementTypes(com.jetbrains.python.PyElementTypes) PythonDialectsTokenSetProvider(com.jetbrains.python.PythonDialectsTokenSetProvider) PyNamedParameterStub(com.jetbrains.python.psi.stubs.PyNamedParameterStub) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) ItemPresentation(com.intellij.navigation.ItemPresentation) PsiTreeUtil(com.intellij.psi.util.PsiTreeUtil) com.jetbrains.python.psi(com.jetbrains.python.psi) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyTypingTypeProvider(com.jetbrains.python.codeInsight.typing.PyTypingTypeProvider) PlatformIcons(com.intellij.util.PlatformIcons) Extensions(com.intellij.openapi.extensions.Extensions) IncorrectOperationException(com.intellij.util.IncorrectOperationException) IStubElementType(com.intellij.psi.stubs.IStubElementType) PyTokenTypes(com.jetbrains.python.PyTokenTypes) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) Collectors(java.util.stream.Collectors) ASTNode(com.intellij.lang.ASTNode) Nullable(org.jetbrains.annotations.Nullable) StreamEx(one.util.streamex.StreamEx) Processor(com.intellij.util.Processor) Pair(com.intellij.openapi.util.Pair) ScopeOwner(com.jetbrains.python.codeInsight.controlflow.ScopeOwner) com.intellij.psi(com.intellij.psi) NotNull(org.jetbrains.annotations.NotNull) Ref(com.intellij.openapi.util.Ref) javax.swing(javax.swing) com.jetbrains.python.psi.types(com.jetbrains.python.psi.types) PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext)

Example 20 with PyResolveContext

use of com.jetbrains.python.psi.resolve.PyResolveContext in project intellij-community by JetBrains.

the class PyCallExpressionHelper method mapArguments.

@NotNull
public static ArgumentMappingResults mapArguments(@NotNull PyCallSiteExpression callSite, @NotNull PyCallable callable, @NotNull List<PyParameter> parameters, @NotNull TypeEvalContext context) {
    final List<PyExpression> arguments = PyTypeChecker.getArguments(callSite, callable);
    final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
    final List<PyParameter> explicitParameters = PyTypeChecker.filterExplicitParameters(parameters, callable, callSite, resolveContext);
    return analyzeArguments(arguments, explicitParameters);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)27 PsiElement (com.intellij.psi.PsiElement)12 NotNull (org.jetbrains.annotations.NotNull)11 Nullable (org.jetbrains.annotations.Nullable)11 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)6 com.jetbrains.python.psi (com.jetbrains.python.psi)6 ContainerUtil (com.intellij.util.containers.ContainerUtil)5 RatedResolveResult (com.jetbrains.python.psi.resolve.RatedResolveResult)5 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)5 java.util (java.util)5 LookupElement (com.intellij.codeInsight.lookup.LookupElement)4 Extensions (com.intellij.openapi.extensions.Extensions)4 PsiFile (com.intellij.psi.PsiFile)4 ArrayUtil (com.intellij.util.ArrayUtil)4 Processor (com.intellij.util.Processor)4 PyNames (com.jetbrains.python.PyNames)4 Collectors (java.util.stream.Collectors)4 Sets (com.google.common.collect.Sets)3 Pair (com.intellij.openapi.util.Pair)3 QualifiedName (com.intellij.psi.util.QualifiedName)3