void (int startOffset, int endOffset) { int startLineNum = getDocument().getLineNumber(startOffset); int endLineNum = getDocument().getLineNumber(endOffset); if (endLineNum == startLineNum) { return; } int selectionIndent = -1; int selectionStartLine = -1; int selectionEndLine = -1; int selectionSegment = getTemplate().getVariableSegmentNumber(Template.SELECTION); if (selectionSegment >= 0) { int selectionStart = getTemplate().getSegmentOffset(selectionSegment); selectionIndent = 0; String templateText = getTemplate().getTemplateText(); while (selectionStart > 0 && templateText.charAt(selectionStart - 1) == ' ') { // TODO handle tabs selectionIndent++; selectionStart--; } selectionStartLine = getDocument().getLineNumber(getSegments().getSegmentStart(selectionSegment)); selectionEndLine = getDocument().getLineNumber(getSegments().getSegmentEnd(selectionSegment)); } int indentLineNum = startLineNum; int lineLength = 0; for (; indentLineNum >= 0; indentLineNum--) { lineLength = getDocument().getLineEndOffset(indentLineNum) - getDocument().getLineStartOffset(indentLineNum); if (lineLength > 0) { break; } } if (indentLineNum < 0) { return; } StringBuilder buffer = new StringBuilder(); CharSequence text = getDocument().getCharsSequence(); for (int i = 0; i < lineLength; i++) { char ch = text.charAt(getDocument().getLineStartOffset(indentLineNum) + i); if (ch != ' ' && ch != '\t') { break; } buffer.append(ch); } if (buffer.length() == 0 && selectionIndent <= 0 || startLineNum >= endLineNum) { return; } String stringToInsert = buffer.toString(); int finalSelectionStartLine = selectionStartLine; int finalSelectionEndLine = selectionEndLine; int finalSelectionIndent = selectionIndent; DocumentUtil.executeInBulk(getDocument(), () -> { for (int i = startLineNum + 1; i <= endLineNum; i++) { if (i > finalSelectionStartLine && i <= finalSelectionEndLine) { getDocument().insertString(getDocument().getLineStartOffset(i), StringUtil.repeatSymbol(' ', finalSelectionIndent)); } else { getDocument().insertString(getDocument().getLineStartOffset(i), stringToInsert); } } }); }