Point (@NotNull Point originalLocation, @NotNull Dimension popupSize, @NotNull Editor editor) { Rectangle preferredBounds = new Rectangle(originalLocation, popupSize); Rectangle adjustedBounds = new Rectangle(preferredBounds); ScreenUtil.moveRectangleToFitTheScreen(adjustedBounds); if (preferredBounds.y - adjustedBounds.y <= 0) { return originalLocation; } int adjustedY = preferredBounds.y - editor.getLineHeight() - popupSize.height; if (adjustedY < 0) { return originalLocation; } return new Point(preferredBounds.x, adjustedY); }