void (@NotNull final PsiElement element, @NotNull final Editor editor) { final Document document = editor.getDocument(); final TextRange textRange = getTextRange(element, editor); if (textRange.isEmpty()) return; final String text = textRange.substring(element.getContainingFile().getText()); final List<String> subStrings = StringUtil.split(text, "\n", true); final String prefix = getPrefix(element); final String postfix = getPostfix(element); final StringBuilder stringBuilder = new StringBuilder(); appendPrefix(element, text, stringBuilder); stringBuilder.append(subStrings.stream() .map(string -> StringUtil.trimStart(string.trim(), prefix.trim())) .map(string -> StringUtil.trimEnd(string, postfix.trim())) .map(String::trim) .filter(finalString -> !StringUtil.isEmptyOrSpaces(finalString)) .collect(Collectors.joining(" "))); appendPostfix(element, text, stringBuilder); final String replacementText = stringBuilder.toString(); CommandProcessor.getInstance().executeCommand(element.getProject(), () -> { document.replaceString(textRange.getStartOffset(), textRange.getEndOffset(), replacementText); final PsiFile file = element.getContainingFile(); FormatterTagHandler formatterTagHandler = new FormatterTagHandler(CodeStyle.getSettings(file)); List<TextRange> enabledRanges = formatterTagHandler.getEnabledRanges(file.getNode(), TextRange.create(0, document.getTextLength())); LineWrappingUtil.doWrapLongLinesIfNecessary(editor, element.getProject(), document, textRange.getStartOffset(), textRange.getStartOffset() + replacementText.length() + 1, enabledRanges, CodeStyle.getSettings(file).getRightMargin(element.getLanguage())); }, null, document); }