Search in sources :

Example 26 with PyExpression

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

the class PyPackageUtilTest method checkSetupArgumentText.

private static void checkSetupArgumentText(@NotNull Module module, @NotNull String keyword, @Nullable String text) {
    final PyCallExpression setupCall = PyPackageUtil.findSetupCall(module);
    assertNotNull(setupCall);
    final PyExpression argument = setupCall.getKeywordArgument(keyword);
    if (text == null) {
        assertNull(argument);
    } else {
        assertNotNull(argument);
        assertEquals(text, argument.getText());
    }
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PyCallExpression(com.jetbrains.python.psi.PyCallExpression)

Example 27 with PyExpression

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

the class OldStyleConvertToFStringProcessor method convertSubstitutionChunk.

@Override
protected boolean convertSubstitutionChunk(@NotNull SubstitutionChunk subsChunk, @NotNull StringBuilder fStringText) {
    final char conversionChar = subsChunk.getConversionType();
    String widthAndPrecision = StringUtil.notNullize(subsChunk.getWidth());
    if (StringUtil.isNotEmpty(subsChunk.getPrecision())) {
        widthAndPrecision += "." + subsChunk.getPrecision();
    }
    final String conversionFlags = subsChunk.getConversionFlags();
    fStringText.append("{");
    final PySubstitutionChunkReference reference = createReference(subsChunk);
    final PyExpression resolveResult = adjustResolveResult(reference.resolve());
    assert resolveResult != null;
    final PsiElement adjusted = prepareExpressionToInject(resolveResult, subsChunk);
    if (adjusted == null)
        return false;
    fStringText.append(adjusted.getText());
    // TODO mostly duplicates the logic of ConvertFormatOperatorToMethodIntention
    if (conversionChar == 'r') {
        fStringText.append("!r");
    }
    if ((conversionChar != 'r' && conversionChar != 's') || StringUtil.isNotEmpty(conversionFlags) || StringUtil.isNotEmpty(widthAndPrecision)) {
        fStringText.append(":");
    }
    fStringText.append(convertFormatSpec(StringUtil.notNullize(conversionFlags), widthAndPrecision, String.valueOf(conversionChar)));
    if (StringUtil.isNotEmpty(widthAndPrecision)) {
        fStringText.append(widthAndPrecision);
    }
    if ('i' == conversionChar || 'u' == conversionChar) {
        fStringText.append("d");
    } else if ('s' != conversionChar && 'r' != conversionChar) {
        fStringText.append(conversionChar);
    }
    fStringText.append("}");
    return true;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) PySubstitutionChunkReference(com.jetbrains.python.codeInsight.PySubstitutionChunkReference) PsiElement(com.intellij.psi.PsiElement)

Example 28 with PyExpression

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

the class PyConvertLiteralToTupleIntention method prepareOriginalElementCopy.

@NotNull
@Override
protected PsiElement prepareOriginalElementCopy(@NotNull PsiElement copy) {
    final PySequenceExpression sequenceExpression = unwrapCollection(copy);
    final PyExpression[] elements = sequenceExpression.getElements();
    if (elements.length == 1) {
        final PyExpression onlyElement = elements[0];
        final PsiElement next = PyPsiUtils.getNextNonCommentSibling(onlyElement, true);
        if (next != null && next.getNode().getElementType() != PyTokenTypes.COMMA) {
            final PyElementGenerator generator = PyElementGenerator.getInstance(copy.getProject());
            final ASTNode anchor = onlyElement.getNode().getTreeNext();
            sequenceExpression.getNode().addChild(generator.createComma(), anchor);
        }
    }
    return copy;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) ASTNode(com.intellij.lang.ASTNode) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PySequenceExpression(com.jetbrains.python.psi.PySequenceExpression) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 29 with PyExpression

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

the class PyWithItemImpl method getTarget.

@Override
public PyExpression getTarget() {
    ASTNode[] children = getNode().getChildren(null);
    boolean foundAs = false;
    for (ASTNode child : children) {
        if (child.getElementType() == PyTokenTypes.AS_KEYWORD) {
            foundAs = true;
        } else if (foundAs && child.getPsi() instanceof PyExpression) {
            return (PyExpression) child.getPsi();
        }
    }
    return null;
}
Also used : ASTNode(com.intellij.lang.ASTNode) PyExpression(com.jetbrains.python.psi.PyExpression)

Example 30 with PyExpression

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

the class PyYieldExpressionImpl method getType.

@Override
public PyType getType(@NotNull TypeEvalContext context, @NotNull TypeEvalContext.Key key) {
    final PyExpression e = getExpression();
    PyType type = e != null ? context.getType(e) : null;
    if (isDelegating()) {
        final PyClassLikeType classType = as(type, PyClassLikeType.class);
        final PyCollectionType collectionType = as(type, PyCollectionType.class);
        if (classType != null && collectionType != null) {
            if (PyTypingTypeProvider.GENERATOR.equals(classType.getClassQName())) {
                final List<PyType> elementTypes = collectionType.getElementTypes(context);
                if (elementTypes.size() == 3) {
                    return elementTypes.get(2);
                }
            }
        }
        return PyNoneType.INSTANCE;
    }
    return type;
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression)

Aggregations

PyExpression (com.jetbrains.python.psi.PyExpression)48 PsiElement (com.intellij.psi.PsiElement)22 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)9 Pair (com.intellij.openapi.util.Pair)8 PyAssignmentStatement (com.jetbrains.python.psi.PyAssignmentStatement)8 PyTargetExpression (com.jetbrains.python.psi.PyTargetExpression)7 PyBinaryExpression (com.jetbrains.python.psi.PyBinaryExpression)4 Nullable (org.jetbrains.annotations.Nullable)4 PyFunctionTypeAnnotation (com.jetbrains.python.codeInsight.functionTypeComments.psi.PyFunctionTypeAnnotation)3 PyClass (com.jetbrains.python.psi.PyClass)3 PyType (com.jetbrains.python.psi.types.PyType)3 ASTNode (com.intellij.lang.ASTNode)2 Document (com.intellij.openapi.editor.Document)2 PyArgumentList (com.jetbrains.python.psi.PyArgumentList)2 PyCallExpression (com.jetbrains.python.psi.PyCallExpression)2 PyPathEvaluator (com.jetbrains.python.psi.impl.PyPathEvaluator)2 TypeEvalContext (com.jetbrains.python.psi.types.TypeEvalContext)2 NotNull (org.jetbrains.annotations.NotNull)2 TemplateBuilder (com.intellij.codeInsight.template.TemplateBuilder)1 Editor (com.intellij.openapi.editor.Editor)1