Search in sources :

Example 36 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.

the class PyImportReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    // no completion in invalid import statements
    PyImportElement importElement = PsiTreeUtil.getParentOfType(myElement, PyImportElement.class);
    if (importElement != null) {
        PsiErrorElement prevError = PsiTreeUtil.getPrevSiblingOfType(importElement, PsiErrorElement.class);
        if (prevError != null) {
            return ArrayUtil.EMPTY_OBJECT_ARRAY;
        }
    }
    PyExpression qualifier = myElement.getQualifier();
    final TypeEvalContext context = TypeEvalContext.codeCompletion(myElement.getProject(), CompletionUtil.getOriginalOrSelf(myElement).getContainingFile());
    if (qualifier != null) {
        // qualifier's type must be module, it should know how to complete
        PyType type = context.getType(qualifier);
        if (type != null) {
            Object[] variants = getTypeCompletionVariants(myElement, type);
            if (!alreadyHasImportKeyword()) {
                replaceInsertHandler(variants, ImportKeywordHandler.INSTANCE);
            }
            return variants;
        } else {
            return ArrayUtil.EMPTY_OBJECT_ARRAY;
        }
    } else {
        // complete to possible modules
        return new ImportVariantCollector(context).execute();
    }
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 37 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.

the class PyOperatorReference method resolveLeftAndRightOperators.

private void resolveLeftAndRightOperators(List<RatedResolveResult> res, PyBinaryExpression expr, String name) {
    final TypeEvalContext typeEvalContext = myContext.getTypeEvalContext();
    typeEvalContext.trace("Trying to resolve left operator");
    typeEvalContext.traceIndent();
    try {
        res.addAll(resolveMember(expr.getLeftExpression(), name));
    } finally {
        typeEvalContext.traceUnindent();
    }
    typeEvalContext.trace("Trying to resolve right operator");
    typeEvalContext.traceIndent();
    try {
        res.addAll(resolveMember(expr.getRightExpression(), leftToRightOperatorName(name)));
    } finally {
        typeEvalContext.traceUnindent();
    }
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 38 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.

the class PyResolveCalleeTest method resolveCallee.

@NotNull
private PyCallExpression.PyMarkedCallee resolveCallee() {
    final PsiReference ref = myFixture.getReferenceAtCaretPosition("/resolve/callee/" + getTestName(false) + ".py");
    final PyCallExpression call = PsiTreeUtil.getParentOfType(ref.getElement(), PyCallExpression.class);
    final TypeEvalContext context = TypeEvalContext.codeAnalysis(myFixture.getProject(), myFixture.getFile());
    final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
    final List<PyCallExpression.PyMarkedCallee> callees = call.multiResolveCallee(resolveContext);
    assertEquals(1, callees.size());
    return callees.get(0);
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) PsiReference(com.intellij.psi.PsiReference) PyCallExpression(com.jetbrains.python.psi.PyCallExpression) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 39 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.

the class PyPackageUtil method resolveRequiresValue.

@Nullable
private static PyListLiteralExpression resolveRequiresValue(@NotNull Module module, @Nullable PyExpression requires) {
    if (requires instanceof PyListLiteralExpression) {
        return (PyListLiteralExpression) requires;
    }
    if (requires instanceof PyReferenceExpression) {
        final TypeEvalContext context = TypeEvalContext.deepCodeInsight(module.getProject());
        final PyResolveContext resolveContext = PyResolveContext.noImplicits().withTypeEvalContext(context);
        final QualifiedResolveResult result = ((PyReferenceExpression) requires).followAssignmentsChain(resolveContext);
        final PsiElement element = result.getElement();
        if (element instanceof PyListLiteralExpression) {
            return (PyListLiteralExpression) element;
        }
    }
    return null;
}
Also used : PyResolveContext(com.jetbrains.python.psi.resolve.PyResolveContext) QualifiedResolveResult(com.jetbrains.python.psi.resolve.QualifiedResolveResult) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 40 with TypeEvalContext

use of com.jetbrains.python.psi.types.TypeEvalContext in project intellij-community by JetBrains.

the class PyMoveSymbolDelegate method isSuitableInstanceMethod.

private static boolean isSuitableInstanceMethod(@Nullable PsiElement element) {
    final PyFunction function = as(element, PyFunction.class);
    if (function == null || function.getContainingClass() == null) {
        return false;
    }
    final String funcName = function.getName();
    if (funcName == null || PyUtil.isSpecialName(funcName)) {
        return false;
    }
    final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(function.getProject(), function.getContainingFile());
    if (PySuperMethodsSearch.search(function, typeEvalContext).findFirst() != null)
        return false;
    if (PyOverridingMethodsSearch.search(function, true).findFirst() != null)
        return false;
    if (function.getDecoratorList() != null || function.getModifier() != null)
        return false;
    if (function.getContainingClass().findPropertyByCallable(function) != null)
        return false;
    return true;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Aggregations

TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)44 PyType (com.jetbrains.python.psi.types.PyType)15 PsiElement (com.intellij.psi.PsiElement)13 NotNull (org.jetbrains.annotations.NotNull)10 Nullable (org.jetbrains.annotations.Nullable)8 PyResolveContext (com.jetbrains.python.psi.resolve.PyResolveContext)6 PyClassLikeType (com.jetbrains.python.psi.types.PyClassLikeType)5 ArrayList (java.util.ArrayList)5 ScopeOwner (com.jetbrains.python.codeInsight.controlflow.ScopeOwner)4 com.jetbrains.python.psi (com.jetbrains.python.psi)4 TextRange (com.intellij.openapi.util.TextRange)3 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)3 XmlStringUtil (com.intellij.xml.util.XmlStringUtil)3 PyNames (com.jetbrains.python.PyNames)3 PyFunction (com.jetbrains.python.psi.PyFunction)3 PyClassType (com.jetbrains.python.psi.types.PyClassType)3 Lists (com.google.common.collect.Lists)2 Instruction (com.intellij.codeInsight.controlflow.Instruction)2 LookupElement (com.intellij.codeInsight.lookup.LookupElement)2 ASTNode (com.intellij.lang.ASTNode)2