Search in sources :

Example 1 with PsiLiteralExpression

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

the class I18nizeAction method getHandler.

@Nullable
public static I18nQuickFixHandler getHandler(final AnActionEvent e) {
    final Editor editor = getEditor(e);
    if (editor == null)
        return null;
    PsiFile psiFile = e.getData(CommonDataKeys.PSI_FILE);
    if (psiFile == null)
        return null;
    TextRange range = JavaI18nUtil.getSelectedRange(editor, psiFile);
    if (range == null)
        return null;
    final PsiLiteralExpression literalExpression = getEnclosingStringLiteral(psiFile, editor);
    PsiElement element = psiFile.findElementAt(editor.getCaretModel().getOffset());
    if (element == null)
        return null;
    if (I18nizeConcatenationQuickFix.getEnclosingLiteralConcatenation(element) != null) {
        return new I18nizeConcatenationQuickFix();
    } else if (literalExpression != null && literalExpression.getTextRange().contains(range)) {
        return new I18nizeQuickFix();
    }
    for (I18nizeHandlerProvider handlerProvider : I18nizeHandlerProvider.EP_NAME.getExtensions()) {
        I18nQuickFixHandler handler = handlerProvider.getHandler(psiFile, editor, range);
        if (handler != null) {
            return handler;
        }
    }
    return null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Editor(com.intellij.openapi.editor.Editor) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with PsiLiteralExpression

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

the class I18nizeAction method getEnclosingStringLiteral.

@Nullable
public static PsiLiteralExpression getEnclosingStringLiteral(final PsiFile psiFile, final Editor editor) {
    PsiElement psiElement = psiFile.findElementAt(editor.getCaretModel().getOffset());
    if (psiElement == null)
        return null;
    PsiLiteralExpression expression = PsiTreeUtil.getParentOfType(psiElement, PsiLiteralExpression.class);
    if (expression == null || !(expression.getValue() instanceof String))
        return null;
    return expression;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method findLiteralValue.

private static String findLiteralValue(PsiExpression argument) {
    if (argument instanceof PsiLiteralExpression) {
        PsiLiteralExpression literalExpression = (PsiLiteralExpression) argument;
        Object value = literalExpression.getValue();
        if (value instanceof String) {
            return (String) value;
        }
    } else if (argument instanceof PsiBinaryExpression) {
        PsiBinaryExpression binaryExpression = (PsiBinaryExpression) argument;
        if (binaryExpression.getOperationTokenType() == JavaTokenType.PLUS) {
            String left = findLiteralValue(binaryExpression.getLOperand());
            String right = findLiteralValue(binaryExpression.getROperand());
            if (left != null && right != null) {
                return left + right;
            }
        }
    } else if (argument instanceof PsiReferenceExpression) {
        PsiReferenceExpression referenceExpression = (PsiReferenceExpression) argument;
        PsiElement resolved = referenceExpression.resolve();
        if (resolved instanceof PsiField) {
            PsiField field = (PsiField) resolved;
            Object value = field.computeConstantValue();
            if (value instanceof String) {
                return (String) value;
            }
        }
    }
    return null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiReferenceExpression(com.intellij.psi.PsiReferenceExpression) PsiField(com.intellij.psi.PsiField) PsiElement(com.intellij.psi.PsiElement) PsiBinaryExpression(com.intellij.psi.PsiBinaryExpression)

Example 4 with PsiLiteralExpression

use of com.intellij.psi.PsiLiteralExpression in project timber by JakeWharton.

the class WrongTimberUsageDetector method getType.

private static Class<?> getType(PsiExpression expression) {
    if (expression == null) {
        return null;
    }
    if (expression instanceof PsiMethodCallExpression) {
        PsiMethodCallExpression call = (PsiMethodCallExpression) expression;
        PsiMethod method = call.resolveMethod();
        if (method == null) {
            return null;
        }
        String methodName = method.getName();
        if (methodName.equals(GET_STRING_METHOD)) {
            return String.class;
        }
    } else if (expression instanceof PsiLiteralExpression) {
        PsiLiteralExpression literalExpression = (PsiLiteralExpression) expression;
        PsiType expressionType = literalExpression.getType();
        if (LintUtils.isString(expressionType)) {
            return String.class;
        } else if (expressionType == PsiType.INT) {
            return Integer.TYPE;
        } else if (expressionType == PsiType.FLOAT) {
            return Float.TYPE;
        } else if (expressionType == PsiType.CHAR) {
            return Character.TYPE;
        } else if (expressionType == PsiType.BOOLEAN) {
            return Boolean.TYPE;
        } else if (expressionType == PsiType.NULL) {
            return Object.class;
        }
    }
    PsiType type = expression.getType();
    if (type != null) {
        Class<?> typeClass = getTypeClass(type);
        return typeClass != null ? typeClass : Object.class;
    }
    return null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiMethod(com.intellij.psi.PsiMethod) PsiMethodCallExpression(com.intellij.psi.PsiMethodCallExpression) PsiType(com.intellij.psi.PsiType)

Example 5 with PsiLiteralExpression

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

the class InplaceIntroduceVariableTest method getExpressionFromEditor.

@Nullable
@Override
protected PsiExpression getExpressionFromEditor() {
    final PsiExpression expression = super.getExpressionFromEditor();
    if (expression != null) {
        return expression;
    }
    final PsiExpression expr = PsiTreeUtil.getParentOfType(getFile().findElementAt(getEditor().getCaretModel().getOffset()), PsiExpression.class);
    if (expr == null && InjectedLanguageManager.getInstance(getProject()).isInjectedFragment(getFile())) {
        return PsiTreeUtil.getParentOfType(InjectedLanguageUtil.getTopLevelFile(getFile()).findElementAt(InjectedLanguageUtil.getTopLevelEditor(getEditor()).getCaretModel().getOffset()), PsiExpression.class);
    }
    return expr instanceof PsiLiteralExpression ? expr : null;
}
Also used : PsiLiteralExpression(com.intellij.psi.PsiLiteralExpression) PsiExpression(com.intellij.psi.PsiExpression) Nullable(org.jetbrains.annotations.Nullable)

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