private static int getFullVisualLine(@NotNull final Editor editor, int y, int topBound, int bottomBound) { int line = editor.yToVisualLine(y); int yActual = editor.visualLineToY(line); if (yActual < topBound) { line++; } else if (yActual + editor.getLineHeight() > bottomBound) { line--; } return line; }
/** * Scrolls the editor to place the given visual line in the middle of the current window. * * @param editor The editor to scroll * @param visualLine The visual line to place in the middle of the current window */ public static void scrollVisualLineToMiddleOfScreen(@NotNull Editor editor, int visualLine) { final ScrollingModel scrollingModel = editor.getScrollingModel(); int y = editor.visualLineToY(visualLine); int lineHeight = editor.getLineHeight(); int height = scrollingModel.getVisibleArea().height; scrollingModel.scrollVertically(y - ((height - lineHeight) / 2)); }
/** * Gets the number of lines than can be displayed on the screen at one time. This is rounded down to the * nearest whole line if there is a partial line visible at the bottom of the screen. * * Note that this value is only approximate and should be avoided whenever possible! * * @param editor The editor * @return The number of screen lines */ private static int getApproximateScreenHeight(@NotNull final Editor editor) { int lh = editor.getLineHeight(); int height = editor.getScrollingModel().getVisibleArea().y + editor.getScrollingModel().getVisibleArea().height - getVisualLineAtTopOfScreen(editor) * lh; return height / lh; }
private static int getScrollScreenTargetCaretVisualLine(@NotNull final Editor editor, int rawCount, boolean down) { final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); final int caretVisualLine = editor.getCaretModel().getVisualPosition().line; final int scrollOption = getScrollOption(rawCount); int targetCaretVisualLine; if (scrollOption == 0) { // Scroll up/down half window size by default. We can't use line count here because of block inlays final int offset = down ? (visibleArea.height / 2) : editor.getLineHeight() - (visibleArea.height / 2); targetCaretVisualLine = editor.yToVisualLine(editor.visualLineToY(caretVisualLine) + offset); } else { targetCaretVisualLine = down ? caretVisualLine + scrollOption : caretVisualLine - scrollOption; } return targetCaretVisualLine; }
private static int scrollFullPageUp(@NotNull final Editor editor, int pages) { final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); final int lineHeight = editor.getLineHeight(); int y = visibleArea.y; int topBound = visibleArea.y; int bottomBound = visibleArea.y + visibleArea.height; int line = 0; int caretLine = -1; // We know pages is negative for (int i = pages; i < 0; i++) { // E.g. a window showing 73-107 has page size 33. Scrolling up puts 74 at the bottom of the screen line = getFullVisualLine(editor, y, topBound, bottomBound) + 1; if (line == 1) { break; } y = editor.visualLineToY(line); bottomBound = y + lineHeight; topBound = bottomBound - visibleArea.height; y = topBound; caretLine = line; } scrollVisualLineToBottomOfScreen(editor, line); return caretLine; }
if (topInlayHeight > caretScreenOffset) { inlayOffset = topInlayHeight; } else if (bottomInlayHeight > visibleArea.height - caretScreenOffset + editor.getLineHeight()) { inlayOffset = -bottomInlayHeight;
int y = editor.visualLineToY(visualLine); int verticalPos = scrollingModel.getVerticalScrollOffset(); int height = inlayHeight + editor.getLineHeight();