Search in sources :

Example 16 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.

the class I18nizeTest method doTest.

private void doTest(@NonNls String ext) throws Exception {
    configureByFile(getBasePath() + "/before" + getTestName(false) + "." + ext);
    I18nizeAction action = new I18nizeAction();
    DataContext dataContext = DataManager.getInstance().getDataContext(myEditor.getComponent());
    AnActionEvent event = AnActionEvent.createFromAnAction(action, null, "place", dataContext);
    action.update(event);
    @NonNls String afterFile = getBasePath() + "/after" + getTestName(false) + "." + ext;
    boolean afterFileExists = new File(PathManagerEx.getTestDataPath() + afterFile).exists();
    I18nQuickFixHandler handler = I18nizeAction.getHandler(event);
    try {
        if (handler != null) {
            handler.checkApplicability(getFile(), getEditor());
        }
    } catch (IncorrectOperationException e) {
        event.getPresentation().setEnabled(false);
    }
    assertEquals(afterFileExists, event.getPresentation().isEnabled());
    if (afterFileExists) {
        PsiLiteralExpression literalExpression = I18nizeAction.getEnclosingStringLiteral(getFile(), getEditor());
        assertNotNull(handler);
        ApplicationManager.getApplication().runWriteAction(() -> handler.performI18nization(getFile(), getEditor(), literalExpression, Collections.<PropertiesFile>emptyList(), "key1", "value1", "i18nizedExpr", PsiExpression.EMPTY_ARRAY, JavaI18nUtil.DEFAULT_PROPERTY_CREATION_HANDLER));
        checkResultByFile(afterFile);
    }
}
Also used : I18nizeAction(com.intellij.codeInspection.i18n.I18nizeAction) NonNls(org.jetbrains.annotations.NonNls) DataContext(com.intellij.openapi.actionSystem.DataContext) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) I18nQuickFixHandler(com.intellij.codeInspection.i18n.I18nQuickFixHandler) IncorrectOperationException(com.intellij.util.IncorrectOperationException) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) File(java.io.File)

Example 17 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.

the class ConvertToScientificNotationPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiLiteralExpression)) {
        return false;
    }
    final PsiLiteralExpression expression = (PsiLiteralExpression) element;
    final PsiType type = expression.getType();
    if (!PsiType.DOUBLE.equals(type) && !PsiType.FLOAT.equals(type)) {
        return false;
    }
    String text = expression.getText();
    if (text == null) {
        return false;
    }
    text = text.toLowerCase();
    text = StringUtil.trimStart(text, "-");
    if (!text.contains(".") && text.startsWith("0")) {
        //Octal integer
        return false;
    }
    return !text.contains("e");
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiType(com.intellij.psi.PsiType)

Example 18 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.

the class ConvertIntegerToDecimalPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiLiteralExpression)) {
        return false;
    }
    final PsiLiteralExpression expression = (PsiLiteralExpression) element;
    final PsiType type = expression.getType();
    if (PsiType.INT.equals(type) || PsiType.LONG.equals(type)) {
        @NonNls final String text = expression.getText();
        if (text == null || text.length() < 2) {
            return false;
        }
        if ("0".equals(text) || "0L".equals(text) || "0l".equals(text)) {
            return false;
        }
        return text.charAt(0) == '0';
    }
    if (PsiType.DOUBLE.equals(type) || PsiType.FLOAT.equals(type)) {
        @NonNls final String text = expression.getText();
        if (text == null || text.length() < 2) {
            return false;
        }
        return text.startsWith("0x") || text.startsWith("0X");
    }
    return false;
}
Also used : NonNls(org.jetbrains.annotations.NonNls) PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiType(com.intellij.psi.PsiType)

Example 19 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.

the class ConvertToPlainPredicate method satisfiedBy.

public boolean satisfiedBy(PsiElement element) {
    if (!(element instanceof PsiLiteralExpression)) {
        return false;
    }
    final PsiLiteralExpression expression = (PsiLiteralExpression) element;
    final PsiType type = expression.getType();
    if (!PsiType.DOUBLE.equals(type) && !PsiType.FLOAT.equals(type)) {
        return false;
    }
    final String text = expression.getText();
    return text != null && (text.contains("e") || text.contains("E"));
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiType(com.intellij.psi.PsiType)

Example 20 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project intellij-community by JetBrains.

the class CharToStringIntention method processIntention.

@Override
public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException {
    final PsiLiteralExpression charLiteral = (PsiLiteralExpression) element;
    final String charLiteralText = charLiteral.getText();
    final String stringLiteral = stringForCharLiteral(charLiteralText);
    PsiReplacementUtil.replaceExpression(charLiteral, stringLiteral);
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression)

Aggregations

PsiLiteralExpression (com.intellij.psi.PsiLiteralExpression)23 PsiType (com.intellij.psi.PsiType)6 PsiElement (com.intellij.psi.PsiElement)5 NonNls (org.jetbrains.annotations.NonNls)4 Nullable (org.jetbrains.annotations.Nullable)4 TextRange (com.intellij.openapi.util.TextRange)2 PsiExpression (com.intellij.psi.PsiExpression)2 I18nQuickFixHandler (com.intellij.codeInspection.i18n.I18nQuickFixHandler)1 I18nizeAction (com.intellij.codeInspection.i18n.I18nizeAction)1 PropertiesFile (com.intellij.lang.properties.psi.PropertiesFile)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 DataContext (com.intellij.openapi.actionSystem.DataContext)1 CaretModel (com.intellij.openapi.editor.CaretModel)1 Document (com.intellij.openapi.editor.Document)1 Editor (com.intellij.openapi.editor.Editor)1 SelectionModel (com.intellij.openapi.editor.SelectionModel)1 PsiBinaryExpression (com.intellij.psi.PsiBinaryExpression)1 PsiField (com.intellij.psi.PsiField)1 PsiFile (com.intellij.psi.PsiFile)1 PsiMethod (com.intellij.psi.PsiMethod)1