Java 类com.intellij.psi.util.PsiConcatenationUtil 实例源码

项目:intellij-ce-playground    文件:ReplaceConcatenationWithFormatStringIntention.java   
@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());
}
项目:tools-idea    文件:ReplaceConcatenationWithFormatStringIntention.java   
@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);
}
项目:consulo-java    文件:ReplaceConcatenationWithFormatStringIntention.java   
@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);
}
项目:intellij-ce-playground    文件:ConcatenationToMessageFormatActionTest.java   
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());
  }
}
项目:tools-idea    文件:ConcatenationToMessageFormatActionTest.java   
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());
  }
}
项目:consulo-java    文件:ConcatenationToMessageFormatAction.java   
public static void buildMessageFormatString(PsiExpression expression,
                                            StringBuilder formatString,
                                            List<PsiExpression> args)
  throws IncorrectOperationException {
  PsiConcatenationUtil.buildFormatString(expression, formatString, args, false);

}