Search in sources :

Example 31 with TypeEvalContext

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

the class PySuperMethodsSearch method findDeepestSuperMethod.

public static PyFunction findDeepestSuperMethod(PyFunction function) {
    TypeEvalContext context = TypeEvalContext.userInitiated(function.getProject(), null);
    List<PsiElement> superMethods = new ArrayList<>(search(function, true, context).findAll());
    while (superMethods.size() > 0) {
        function = getBaseMethod(superMethods, function.getContainingClass());
        superMethods = new ArrayList<>(search(function, true, context).findAll());
    }
    return function;
}
Also used : ArrayList(java.util.ArrayList) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PsiElement(com.intellij.psi.PsiElement)

Example 32 with TypeEvalContext

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

the class PyIterableVariableMacro method getIterableElements.

@NotNull
protected List<PsiNamedElement> getIterableElements(@NotNull PsiElement element) {
    final TypeEvalContext typeEvalContext = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
    final List<PsiNamedElement> components = new ArrayList<>();
    for (PsiNamedElement namedElement : getVisibleNamedElements(element)) {
        if (namedElement instanceof PyTypedElement) {
            final PyType type = typeEvalContext.getType((PyTypedElement) namedElement);
            if (type != null && PyABCUtil.isSubtype(type, PyNames.ITERABLE, typeEvalContext)) {
                components.add(namedElement);
            }
        }
    }
    return components;
}
Also used : PsiNamedElement(com.intellij.psi.PsiNamedElement) PyType(com.jetbrains.python.psi.types.PyType) ArrayList(java.util.ArrayList) PyTypedElement(com.jetbrains.python.psi.PyTypedElement) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) NotNull(org.jetbrains.annotations.NotNull)

Example 33 with TypeEvalContext

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

the class PyMethodMember method buildNameFor.

private static String buildNameFor(final PyElement element) {
    if (element instanceof PyFunction) {
        final TypeEvalContext context = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile());
        final List<PyParameter> parameters = PyUtil.getParameters((PyFunction) element, context);
        return element.getName() + "(" + StringUtil.join(parameters, parameter -> PyUtil.getReadableRepr(parameter, false), ", ") + ")";
    }
    final PyClass cls = as(element, PyClass.class);
    if (cls != null && PyNames.TYPES_INSTANCE_TYPE.equals(cls.getQualifiedName())) {
        return "<old-style class>";
    }
    return element.getName();
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext)

Example 34 with TypeEvalContext

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

the class PyOverrideImplementUtil method buildOverriddenFunction.

private static PyFunctionBuilder buildOverriddenFunction(PyClass pyClass, PyFunction baseFunction, boolean implement) {
    final boolean overridingNew = PyNames.NEW.equals(baseFunction.getName());
    assert baseFunction.getName() != null;
    PyFunctionBuilder pyFunctionBuilder = new PyFunctionBuilder(baseFunction.getName(), baseFunction);
    final PyDecoratorList decorators = baseFunction.getDecoratorList();
    boolean baseMethodIsStatic = false;
    if (decorators != null) {
        if (decorators.findDecorator(PyNames.CLASSMETHOD) != null) {
            pyFunctionBuilder.decorate(PyNames.CLASSMETHOD);
        } else if (decorators.findDecorator(PyNames.STATICMETHOD) != null) {
            baseMethodIsStatic = true;
            pyFunctionBuilder.decorate(PyNames.STATICMETHOD);
        } else if (decorators.findDecorator(PyNames.PROPERTY) != null || decorators.findDecorator(PyNames.ABSTRACTPROPERTY) != null) {
            pyFunctionBuilder.decorate(PyNames.PROPERTY);
        }
    }
    final LanguageLevel level = LanguageLevel.forElement(pyClass);
    PyAnnotation anno = baseFunction.getAnnotation();
    if (anno != null && level.isAtLeast(LanguageLevel.PYTHON30)) {
        pyFunctionBuilder.annotation(anno.getText());
    }
    final TypeEvalContext context = TypeEvalContext.userInitiated(baseFunction.getProject(), baseFunction.getContainingFile());
    final List<PyParameter> baseParams = PyUtil.getParameters(baseFunction, context);
    for (PyParameter parameter : baseParams) {
        final PyNamedParameter namedParameter = parameter.getAsNamed();
        if (namedParameter != null) {
            final StringBuilder parameterBuilder = new StringBuilder();
            if (namedParameter.isPositionalContainer()) {
                parameterBuilder.append("*");
            } else if (namedParameter.isKeywordContainer()) {
                parameterBuilder.append("**");
            }
            parameterBuilder.append(namedParameter.getName());
            final PyAnnotation annotation = namedParameter.getAnnotation();
            if (annotation != null && level.isAtLeast(LanguageLevel.PYTHON30)) {
                parameterBuilder.append(annotation.getText());
            }
            final PyExpression defaultValue = namedParameter.getDefaultValue();
            if (defaultValue != null) {
                parameterBuilder.append("=");
                parameterBuilder.append(defaultValue.getText());
            }
            pyFunctionBuilder.parameter(parameterBuilder.toString());
        } else {
            pyFunctionBuilder.parameter(parameter.getText());
        }
    }
    PyClass baseClass = baseFunction.getContainingClass();
    assert baseClass != null;
    StringBuilder statementBody = new StringBuilder();
    boolean hadStar = false;
    List<String> parameters = new ArrayList<>();
    for (PyParameter parameter : baseParams) {
        final PyNamedParameter pyNamedParameter = parameter.getAsNamed();
        if (pyNamedParameter != null) {
            String repr = pyNamedParameter.getRepr(false);
            parameters.add(hadStar && !pyNamedParameter.isKeywordContainer() ? pyNamedParameter.getName() + "=" + repr : repr);
            if (pyNamedParameter.isPositionalContainer()) {
                hadStar = true;
            }
        } else if (parameter instanceof PySingleStarParameter) {
            hadStar = true;
        } else {
            parameters.add(parameter.getText());
        }
    }
    if (PyNames.TYPES_INSTANCE_TYPE.equals(baseClass.getQualifiedName()) || raisesNotImplementedError(baseFunction) || implement) {
        statementBody.append(PyNames.PASS);
    } else {
        if (!PyNames.INIT.equals(baseFunction.getName()) && context.getReturnType(baseFunction) != PyNoneType.INSTANCE || overridingNew) {
            statementBody.append("return ");
        }
        if (baseClass.isNewStyleClass(context)) {
            statementBody.append(PyNames.SUPER);
            statementBody.append("(");
            final LanguageLevel langLevel = ((PyFile) pyClass.getContainingFile()).getLanguageLevel();
            if (!langLevel.isPy3K()) {
                final String baseFirstName = !baseParams.isEmpty() ? baseParams.get(0).getName() : null;
                final String firstName = baseFirstName != null ? baseFirstName : PyNames.CANONICAL_SELF;
                PsiElement outerClass = PsiTreeUtil.getParentOfType(pyClass, PyClass.class, true, PyFunction.class);
                String className = pyClass.getName();
                final List<String> nameResult = Lists.newArrayList(className);
                while (outerClass != null) {
                    nameResult.add(0, ((PyClass) outerClass).getName());
                    outerClass = PsiTreeUtil.getParentOfType(outerClass, PyClass.class, true, PyFunction.class);
                }
                StringUtil.join(nameResult, ".", statementBody);
                statementBody.append(", ").append(firstName);
            }
            statementBody.append(").").append(baseFunction.getName()).append("(");
            // type.__new__ is explicitly decorated as @staticmethod in our stubs, but not in real Python code
            if (parameters.size() > 0 && !(baseMethodIsStatic || overridingNew)) {
                parameters.remove(0);
            }
        } else {
            statementBody.append(getReferenceText(pyClass, baseClass)).append(".").append(baseFunction.getName()).append("(");
        }
        StringUtil.join(parameters, ", ", statementBody);
        statementBody.append(")");
    }
    pyFunctionBuilder.statement(statementBody.toString());
    return pyFunctionBuilder;
}
Also used : TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyFunctionBuilder(com.jetbrains.python.psi.impl.PyFunctionBuilder) PsiElement(com.intellij.psi.PsiElement)

Example 35 with TypeEvalContext

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

the class PyOverrideImplementUtil method chooseAndOverrideOrImplementMethods.

private static void chooseAndOverrideOrImplementMethods(final Project project, @NotNull final Editor editor, @NotNull final PyClass pyClass) {
    PyPsiUtils.assertValid(pyClass);
    ApplicationManager.getApplication().assertReadAccessAllowed();
    final Set<PyFunction> result = new HashSet<>();
    TypeEvalContext context = TypeEvalContext.codeCompletion(project, null);
    final Collection<PyFunction> superFunctions = getAllSuperFunctions(pyClass, context);
    result.addAll(superFunctions);
    chooseAndOverrideOrImplementMethods(project, editor, pyClass, result, "Select Methods to Override", false);
}
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