LogicalPosition (int x, int y, boolean trimToLineWidth) { if (x < 0) { x = 0; } LogicalPosition pos = xyToLogicalPosition(new Point(x, y)); int column = pos.column; int line = pos.line; int softWrapLinesBeforeTargetLogicalLine = pos.softWrapLinesBeforeCurrentLogicalLine; int softWrapLinesOnTargetLogicalLine = pos.softWrapLinesOnCurrentLogicalLine; int softWrapColumns = pos.softWrapColumnDiff; boolean leansForward = pos.leansForward; boolean leansRight = pos.visualPositionLeansRight; final int totalLines = myDocument.getLineCount(); if (totalLines <= 0) { return new LogicalPosition(0, 0); } if (line >= totalLines && totalLines > 0) { int visibleLineCount = getVisibleLineCount(); int newY = visibleLineCount > 0 ? visibleLineToY(visibleLineCount - 1) : 0; if (newY > 0 && newY == y) { newY = visibleLineToY(getVisibleLogicalLinesCount()); } if (newY >= y) { LogMessageEx.error(LOG, "cycled moveCaretToScreenPos() detected", String.format("x=%d, y=%d\nstate=%s", x, y, dumpState())); throw new IllegalStateException("cycled moveCaretToScreenPos() detected"); } return getLogicalPositionForScreenPos(x, newY, trimToLineWidth); } if (!mySettings.isVirtualSpace() && trimToLineWidth) { int lineEndOffset = myDocument.getLineEndOffset(line); int lineEndColumn = calcColumnNumber(lineEndOffset, line); if (column > lineEndColumn) { column = lineEndColumn; leansForward = true; leansRight = true; if (softWrapColumns != 0) { softWrapColumns -= column - lineEndColumn; } } } if (!mySettings.isCaretInsideTabs()) { int offset = logicalPositionToOffset(new LogicalPosition(line, column)); CharSequence text = myDocument.getImmutableCharSequence(); if (offset >= 0 && offset < myDocument.getTextLength()) { if (text.charAt(offset) == '\t') { column = calcColumnNumber(offset, line); } } } return pos.visualPositionAware ? new LogicalPosition( line, column, softWrapLinesBeforeTargetLogicalLine, softWrapLinesOnTargetLogicalLine, softWrapColumns, pos.foldedLines, pos.foldingColumnDiff, leansForward, leansRight ) : new LogicalPosition(line, column, leansForward); }