Search in sources :

Example 1 with PyClassTypeImpl

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

the class PyStringConcatenationToFormatIntention method doInvoke.

public void doInvoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    PsiElement element = PsiTreeUtil.getTopmostParentOfType(file.findElementAt(editor.getCaretModel().getOffset()), PyBinaryExpression.class);
    if (element == null)
        return;
    final LanguageLevel languageLevel = LanguageLevel.forElement(element);
    final boolean useFormatMethod = languageLevel.isAtLeast(LanguageLevel.PYTHON27);
    NotNullFunction<String, String> escaper = StringUtil.escaper(false, "\"\'\\");
    StringBuilder stringLiteral = new StringBuilder();
    List<String> parameters = new ArrayList<>();
    Pair<String, String> quotes = Pair.create("\"", "\"");
    boolean quotesDetected = false;
    final TypeEvalContext context = TypeEvalContext.userInitiated(file.getProject(), file);
    int paramCount = 0;
    boolean isUnicode = false;
    final PyClassTypeImpl unicodeType = PyBuiltinCache.getInstance(element).getObjectType("unicode");
    for (PyExpression expression : getSimpleExpressions((PyBinaryExpression) element)) {
        if (expression instanceof PyStringLiteralExpression) {
            final PyType type = context.getType(expression);
            if (type != null && type.equals(unicodeType)) {
                isUnicode = true;
            }
            if (!quotesDetected) {
                quotes = PyStringLiteralUtil.getQuotes(expression.getText());
                quotesDetected = true;
            }
            String value = ((PyStringLiteralExpression) expression).getStringValue();
            if (!useFormatMethod) {
                value = value.replace("%", "%%");
            }
            stringLiteral.append(escaper.fun(value));
        } else {
            addParamToString(stringLiteral, paramCount, useFormatMethod);
            parameters.add(expression.getText());
            ++paramCount;
        }
    }
    if (quotes == null)
        quotes = Pair.create("\"", "\"");
    stringLiteral.insert(0, quotes.getFirst());
    if (isUnicode && !quotes.getFirst().toLowerCase().contains("u"))
        stringLiteral.insert(0, "u");
    stringLiteral.append(quotes.getSecond());
    PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
    if (!parameters.isEmpty()) {
        if (useFormatMethod) {
            stringLiteral.append(".format(").append(StringUtil.join(parameters, ",")).append(")");
        } else {
            final String paramString = parameters.size() > 1 ? "(" + StringUtil.join(parameters, ",") + ")" : StringUtil.join(parameters, ",");
            stringLiteral.append(" % ").append(paramString);
        }
        final PyExpression expression = elementGenerator.createFromText(LanguageLevel.getDefault(), PyExpressionStatement.class, stringLiteral.toString()).getExpression();
        element.replace(expression);
    } else {
        PyStringLiteralExpression stringLiteralExpression = elementGenerator.createStringLiteralAlreadyEscaped(stringLiteral.toString());
        element.replace(stringLiteralExpression);
    }
}
Also used : PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) ArrayList(java.util.ArrayList) TypeEvalContext(com.jetbrains.python.psi.types.TypeEvalContext) PyType(com.jetbrains.python.psi.types.PyType) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyClassTypeImpl

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

the class AddFieldQuickFix method getClassType.

private static PyClassType getClassType(@NotNull final PsiElement element) {
    if (element instanceof PyQualifiedExpression) {
        final PyExpression qualifier = ((PyQualifiedExpression) element).getQualifier();
        if (qualifier == null)
            return null;
        final PyType type = TypeEvalContext.userInitiated(element.getProject(), element.getContainingFile()).getType(qualifier);
        return type instanceof PyClassType ? (PyClassType) type : null;
    }
    final PyClass aClass = PsiTreeUtil.getParentOfType(element, PyClass.class);
    return aClass != null ? new PyClassTypeImpl(aClass, false) : null;
}
Also used : PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType)

Example 3 with PyClassTypeImpl

use of com.jetbrains.python.psi.types.PyClassTypeImpl 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;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) PyType(com.jetbrains.python.psi.types.PyType)

Example 4 with PyClassTypeImpl

use of com.jetbrains.python.psi.types.PyClassTypeImpl 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;
}
Also used : PyClass(com.jetbrains.python.psi.PyClass) PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyFunction(com.jetbrains.python.psi.PyFunction) QualifiedName(com.intellij.psi.util.QualifiedName) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with PyClassTypeImpl

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

the class AddMethodQuickFix method getClassType.

private static PyClassType getClassType(@NotNull final PsiElement problemElement) {
    if ((problemElement instanceof PyQualifiedExpression)) {
        final PyExpression qualifier = ((PyQualifiedExpression) problemElement).getQualifier();
        if (qualifier == null)
            return null;
        final PyType type = TypeEvalContext.userInitiated(problemElement.getProject(), problemElement.getContainingFile()).getType(qualifier);
        return type instanceof PyClassType ? (PyClassType) type : null;
    }
    final PyClass pyClass = PsiTreeUtil.getParentOfType(problemElement, PyClass.class);
    return pyClass != null ? new PyClassTypeImpl(pyClass, false) : null;
}
Also used : PyClassTypeImpl(com.jetbrains.python.psi.types.PyClassTypeImpl) PyClassType(com.jetbrains.python.psi.types.PyClassType) PyType(com.jetbrains.python.psi.types.PyType)

Aggregations

PyClassTypeImpl (com.jetbrains.python.psi.types.PyClassTypeImpl)5 PyType (com.jetbrains.python.psi.types.PyType)4 QualifiedName (com.intellij.psi.util.QualifiedName)2 PyClass (com.jetbrains.python.psi.PyClass)2 PyFunction (com.jetbrains.python.psi.PyFunction)2 PyClassType (com.jetbrains.python.psi.types.PyClassType)2 PsiElement (com.intellij.psi.PsiElement)1 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)1 ArrayList (java.util.ArrayList)1 Nullable (org.jetbrains.annotations.Nullable)1