Search in sources :

Example 36 with PyExpression

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

the class PyMethodDescriptor method getParameters.

@Override
public List<PyParameterInfo> getParameters() {
    List<PyParameterInfo> parameterInfos = new ArrayList<>();
    PyParameter[] parameters = myFunction.getParameterList().getParameters();
    for (int i = 0; i < parameters.length; i++) {
        PyParameter parameter = parameters[i];
        final PyExpression defaultValue = parameter.getDefaultValue();
        final String name;
        if (parameter instanceof PyNamedParameter) {
            if (((PyNamedParameter) parameter).isPositionalContainer()) {
                name = "*" + parameter.getName();
            } else if (((PyNamedParameter) parameter).isKeywordContainer()) {
                name = "**" + parameter.getName();
            } else {
                name = parameter.getName();
            }
        } else {
            name = parameter.getText();
        }
        parameterInfos.add(new PyParameterInfo(i, name, defaultValue == null ? null : defaultValue.getText(), defaultValue != null && !StringUtil.isEmptyOrSpaces(defaultValue.getText())));
    }
    return parameterInfos;
}
Also used : ArrayList(java.util.ArrayList) PyExpression(com.jetbrains.python.psi.PyExpression) PyNamedParameter(com.jetbrains.python.psi.PyNamedParameter) PyParameter(com.jetbrains.python.psi.PyParameter)

Example 37 with PyExpression

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

the class PyIntroduceTestCase method buildSuggestions.

protected Collection<String> buildSuggestions(Class<? extends PyExpression> parentClass) {
    myFixture.configureByFile(getTestName(true) + ".py");
    final IntroduceHandler handler = createHandler();
    final PyExpression expr = PsiTreeUtil.getParentOfType(myFixture.getFile().findElementAt(myFixture.getCaretOffset()), parentClass);
    assertNotNull(expr);
    return handler.getSuggestedNames(expr);
}
Also used : PyExpression(com.jetbrains.python.psi.PyExpression) IntroduceHandler(com.jetbrains.python.refactoring.introduce.IntroduceHandler)

Example 38 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 39 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 40 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)

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