int (int offset) { if (myUseNewRendering) return myView.offsetToVisualLine(offset); int textLength = getDocument().getTextLength(); if (offset >= textLength) { return Math.max(0, getVisibleLineCount() - 1); // lines are 0 based } int line = offsetToLogicalLine(offset); int lineStartOffset = line >= myDocument.getLineCount() ? myDocument.getTextLength() : myDocument.getLineStartOffset(line); int result = logicalToVisualLine(line); // There is a possible case that logical line that contains target offset is soft-wrapped (represented in more than one visual // line). Hence, we need to perform necessary adjustments to the visual line that is used to show logical line start if necessary. int i = getSoftWrapModel().getSoftWrapIndex(lineStartOffset); if (i < 0) { i = -i - 1; } List<? extends SoftWrap> softWraps = getSoftWrapModel().getRegisteredSoftWraps(); for (; i < softWraps.size(); i++) { SoftWrap softWrap = softWraps.get(i); if (softWrap.getStart() > offset) { break; } result++; // Assuming that every soft wrap contains only one virtual line feed symbol } return result; }