void (PsiPolyadicExpression polyadicExpression, boolean seenStringBefore) { PsiExpression[] operands = polyadicExpression.getOperands(); PsiExpression beforeLast = operands[operands.length - 2]; StringBuilder builder = new StringBuilder(); boolean meetBeforeLast = false; boolean afterPlus = false; CommentTracker generalCommentTracker = new CommentTracker(); CommentTracker beforeLastCommentTracker = new CommentTracker(); for (PsiElement child : polyadicExpression.getChildren()) { if (!meetBeforeLast) { if (beforeLast == child) { meetBeforeLast = true; } builder.append(child.getText()); generalCommentTracker.markUnchanged(child); } else { if (child instanceof PsiJavaToken token && token.getTokenType() == JavaTokenType.PLUS) { afterPlus = true; } if (!afterPlus) { beforeLastCommentTracker.grabComments(child); generalCommentTracker.markUnchanged(child); } } } String text = builder.toString().trim(); if (!seenStringBefore) { text = "String.valueOf(" + text + ')'; } String finalText = text; final PsiElement replacementExpression = CodeStyleManager.getInstance(polyadicExpression.getProject()).performActionWithFormatterDisabled(new Computable<>() { @Override public PsiElement compute() { return generalCommentTracker.replaceAndRestoreComments(polyadicExpression, finalText); } }); if (replacementExpression instanceof PsiPolyadicExpression psiPolyadicExpression) { PsiExpression[] expressionOperands = psiPolyadicExpression.getOperands(); if (expressionOperands.length == 0) { return; } PsiExpression lastOperand = expressionOperands[expressionOperands.length - 1]; beforeLastCommentTracker.insertCommentsBefore(lastOperand); } }