Search in sources :

Example 1 with PyNumericLiteralExpression

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

the class ReplaceOctalNumericLiteralQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement numericLiteralExpression = descriptor.getPsiElement();
    if (numericLiteralExpression instanceof PyNumericLiteralExpression) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String text = numericLiteralExpression.getText();
        numericLiteralExpression.replace(elementGenerator.createExpressionFromText("0o" + text.substring(1)));
    }
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 2 with PyNumericLiteralExpression

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

the class PyNumericLiteralTest method configureByText.

@NotNull
private PyNumericLiteralExpression configureByText(@NotNull String text) {
    final PsiElement element = myFixture.configureByText(PythonFileType.INSTANCE, text).findElementAt(0);
    assertNotNull(element);
    final PyNumericLiteralExpression numericLiteralExpression = PyUtil.as(element.getParent(), PyNumericLiteralExpression.class);
    assertNotNull(numericLiteralExpression);
    return numericLiteralExpression;
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with PyNumericLiteralExpression

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

the class PyNumericLiteralTest method doTestLiteral.

private void doTestLiteral(@NotNull String text, boolean isInteger, @Nullable Long expectedLong, @NotNull BigInteger expectedInt, @NotNull BigDecimal expectedDecimal, @NotNull String expectedType) {
    final PyNumericLiteralExpression literal = configureByText(text);
    assertEquals(isInteger, literal.isIntegerLiteral());
    assertEquals(expectedLong, literal.getLongValue());
    assertEquals(expectedInt, literal.getBigIntegerValue());
    assertEquals(0, expectedDecimal.compareTo(literal.getBigDecimalValue()));
    final PyType type = TypeEvalContext.codeInsightFallback(myFixture.getProject()).getType(literal);
    assertNotNull(type);
    assertEquals(expectedType, type.getName());
}
Also used : PyType(com.jetbrains.python.psi.types.PyType) PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression)

Example 4 with PyNumericLiteralExpression

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

the class RemoveTrailingLQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    PsiElement numericLiteralExpression = descriptor.getPsiElement();
    if (numericLiteralExpression instanceof PyNumericLiteralExpression) {
        PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        String text = numericLiteralExpression.getText();
        numericLiteralExpression.replace(elementGenerator.createExpressionFromText(text.substring(0, text.length() - 1)));
    }
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Example 5 with PyNumericLiteralExpression

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

the class PyRemoveUnderscoresInNumericLiteralsQuickFix method applyFix.

@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement();
    if (element instanceof PyNumericLiteralExpression) {
        final PyElementGenerator elementGenerator = PyElementGenerator.getInstance(project);
        final String text = element.getText();
        element.replace(elementGenerator.createExpressionFromText(LanguageLevel.forElement(element), text.replaceAll("_", "")));
    }
}
Also used : PyNumericLiteralExpression(com.jetbrains.python.psi.PyNumericLiteralExpression) PyElementGenerator(com.jetbrains.python.psi.PyElementGenerator) PsiElement(com.intellij.psi.PsiElement)

Aggregations

PyNumericLiteralExpression (com.jetbrains.python.psi.PyNumericLiteralExpression)5 PsiElement (com.intellij.psi.PsiElement)4 PyElementGenerator (com.jetbrains.python.psi.PyElementGenerator)3 PyType (com.jetbrains.python.psi.types.PyType)1 NotNull (org.jetbrains.annotations.NotNull)1