void (@NotNull PsiElement element) { PsiComment firstComment = (PsiComment)element; while (true) { final PsiElement prevComment = PsiTreeUtil.skipWhitespacesBackward(firstComment); if (!(prevComment instanceof PsiComment) || ((PsiComment)prevComment).getTokenType() != JavaTokenType.END_OF_LINE_COMMENT) { break; } firstComment = (PsiComment)prevComment; } final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(element.getProject()); final PsiElementFactory factory = psiFacade.getElementFactory(); final List<PsiComment> multiLineComments = new ArrayList<>(); PsiElement nextComment = firstComment; String whiteSpace = null; while (true) { nextComment = PsiTreeUtil.skipWhitespacesForward(nextComment); if (!(nextComment instanceof PsiComment) || ((PsiComment)nextComment).getTokenType() != JavaTokenType.END_OF_LINE_COMMENT) { break; } if (whiteSpace == null) { final PsiElement prevSibling = nextComment.getPrevSibling(); assert prevSibling != null; final String text = prevSibling.getText(); whiteSpace = getIndent(text); } multiLineComments.add((PsiComment)nextComment); } final String newCommentString; if (multiLineComments.isEmpty()) { final String text = getCommentContents(firstComment); newCommentString = "/* " + text.trim() + " */"; } else { final StringBuilder text = new StringBuilder(); text.append("/*\n"); text.append(whiteSpace); text.append(getCommentContents(firstComment)); for (PsiComment multiLineComment : multiLineComments) { text.append('\n'); text.append(whiteSpace); text.append(getCommentContents(multiLineComment)); } text.append('\n'); text.append(whiteSpace); text.append("*/"); newCommentString = text.toString(); } final PsiComment newComment = factory.createCommentFromText(newCommentString, element); firstComment.replace(newComment); for (PsiElement commentToDelete : multiLineComments) { commentToDelete.delete(); } }