int (@NotNull CharSequence text, int tabSize, int startLineOffset, int endLineOffset, int targetRangeEndOffset, int reservedWidthInColumns, int rightMargin) { int width = 0; int symbolWidth; int result = Integer.MAX_VALUE; boolean wrapLine = false; for (int i = startLineOffset; i < Math.min(endLineOffset, targetRangeEndOffset); i++) { char c = text.charAt(i); symbolWidth = c == '\t' ? tabSize - (width % tabSize) : 1; if (width + symbolWidth + reservedWidthInColumns >= rightMargin && (Math.min(endLineOffset, targetRangeEndOffset) - i) >= reservedWidthInColumns) { // Remember preferred position. result = i - 1; } if (width + symbolWidth >= rightMargin) { wrapLine = true; break; } width += symbolWidth; } return wrapLine ? result : -1; }