VisualPosition (int x, int y, boolean trimToLineWidth, @Nullable Caret targetCaret) { if (myDocument.getLineCount() == 0) { return new VisualPosition(0, 0); } if (x < 0) { x = 0; } if (y < 0) { y = 0; } int visualLineCount = getVisibleLineCount(); if (yToVisualLine(y) >= visualLineCount) { y = visualLineToY(Math.max(0, visualLineCount - 1)); } VisualPosition visualPosition = xyToVisualPosition(new Point(x, y)); Caret caret = targetCaret != null ? targetCaret : getCaretModel().getPrimaryCaret(); if (EditorUtil.isBlockLikeCaret(caret) && !visualPosition.leansRight && visualPosition.column > 0) { // Adjustment for block caret when clicking in the second half of the character visualPosition = new VisualPosition(visualPosition.line, visualPosition.column - 1, true); } if (trimToLineWidth && !mySettings.isVirtualSpace()) { LogicalPosition logicalPosition = visualToLogicalPosition(visualPosition); LogicalPosition lineEndPosition = offsetToLogicalPosition(myDocument.getLineEndOffset(logicalPosition.line)); if (logicalPosition.column > lineEndPosition.column) { visualPosition = logicalToVisualPosition(lineEndPosition.leanForward(true)); } else if (mySoftWrapModel.isInsideSoftWrap(visualPosition)) { VisualPosition beforeSoftWrapPosition = myView.logicalToVisualPosition(logicalPosition, true); if (visualPosition.line == beforeSoftWrapPosition.line) { visualPosition = beforeSoftWrapPosition; } else { visualPosition = myView.logicalToVisualPosition(logicalPosition, false); } } } return visualPosition; }