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;
}
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;
}
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;
}
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;
}
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;
}
Aggregations