@Override protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { PsiPolyadicExpression expression = (PsiPolyadicExpression)element; PsiElement parent = expression.getParent(); while (ExpressionUtils.isConcatenation(parent)) { expression = (PsiPolyadicExpression)parent; parent = expression.getParent(); } final StringBuilder formatString = new StringBuilder(); final List<PsiExpression> formatParameters = new ArrayList(); PsiConcatenationUtil.buildFormatString(expression, formatString, formatParameters, true); if (replaceWithPrintfExpression(expression, formatString, formatParameters)) { return; } final StringBuilder newExpression = new StringBuilder(); newExpression.append("java.lang.String.format(\""); newExpression.append(formatString); newExpression.append('\"'); for (PsiExpression formatParameter : formatParameters) { newExpression.append(", "); newExpression.append(formatParameter.getText()); } newExpression.append(')'); PsiReplacementUtil.replaceExpression(expression, newExpression.toString()); }
@Override protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { PsiPolyadicExpression expression = (PsiPolyadicExpression)element; PsiElement parent = expression.getParent(); while (ConcatenationUtils.isConcatenation(parent)) { expression = (PsiPolyadicExpression)parent; parent = expression.getParent(); } final StringBuilder formatString = new StringBuilder(); final List<PsiExpression> formatParameters = new ArrayList(); PsiConcatenationUtil.buildFormatString(expression, formatString, formatParameters, true); if (replaceWithPrintfExpression(expression, formatString, formatParameters)) { return; } final StringBuilder newExpression = new StringBuilder(); newExpression.append("java.lang.String.format(\""); newExpression.append(formatString); newExpression.append('\"'); for (PsiExpression formatParameter : formatParameters) { newExpression.append(", "); newExpression.append(formatParameter.getText()); } newExpression.append(')'); replaceExpression(newExpression.toString(), expression); }
public void doTest(String expressionText, String messageFormatText, String... foundExpressionTexts) { final PsiExpression expression = JavaPsiFacade.getElementFactory(getProject()).createExpressionFromText(expressionText, null); final StringBuilder result = new StringBuilder(); final ArrayList<PsiExpression> args = new ArrayList<PsiExpression>(); PsiConcatenationUtil.buildFormatString(expression, result, args, false); assertEquals(messageFormatText, result.toString()); assertEquals(foundExpressionTexts.length, args.size()); for (int i = 0; i < foundExpressionTexts.length; i++) { final String foundExpressionText = foundExpressionTexts[i]; assertEquals(foundExpressionText, args.get(i).getText()); } }
public static void buildMessageFormatString(PsiExpression expression, StringBuilder formatString, List<PsiExpression> args) throws IncorrectOperationException { PsiConcatenationUtil.buildFormatString(expression, formatString, args, false); }