/** * Gets the number of characters that are visible on a screen line * * @param editor The editor * @return The number of screen columns */ public static int getScreenWidth(@NotNull final Editor editor) { Rectangle rect = editor.getScrollingModel().getVisibleArea(); Point pt = new Point(rect.width, 0); VisualPosition vp = editor.xyToVisualPosition(pt); return vp.column; }
public static int getVisualLineAtMiddleOfScreen(@NotNull final Editor editor) { final ScrollingModel scrollingModel = editor.getScrollingModel(); final Rectangle visibleArea = scrollingModel.getVisibleArea(); return editor.yToVisualLine(visibleArea.y + (visibleArea.height / 2)); }
/** * Gets the column currently displayed at the left edge of the editor. * * @param editor The editor * @return The column number */ public static int getVisualColumnAtLeftOfScreen(@NotNull final Editor editor) { int cw = getColumnWidth(editor); if (cw == 0) return 0; return (editor.getScrollingModel().getHorizontalScrollOffset() + cw - 1) / cw; }
/** * Gets the number of pixels per column of text. * * @param editor The editor * @return The number of pixels */ public static int getColumnWidth(@NotNull final Editor editor) { Rectangle rect = editor.getScrollingModel().getVisibleArea(); if (rect.width == 0) return 0; Point pt = new Point(rect.width, 0); VisualPosition vp = editor.xyToVisualPosition(pt); if (vp.column == 0) return 0; return rect.width / vp.column; }
public static int getVisualLineAtTopOfScreen(@NotNull final Editor editor) { final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); return getFullVisualLine(editor, visibleArea.y, visibleArea.y, visibleArea.y + visibleArea.height); }
public static int getVisualLineAtBottomOfScreen(@NotNull final Editor editor) { final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); return getFullVisualLine(editor, visibleArea.y + visibleArea.height, visibleArea.y, visibleArea.y + visibleArea.height); }
private static void scrollColumnToLeftOfScreen(@NotNull Editor editor, int column) { editor.getScrollingModel().scrollHorizontally(column * EditorHelper.getColumnWidth(editor)); }
/** * 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; }
/** * 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)); }
@Override public void invoke(@NotNull Project project, @NotNull PsiFile file, @Nullable("is null when called from inspection") Editor editor, @NotNull PsiElement startElement, @NotNull PsiElement endElement) { if (!(file instanceof GoFile) || editor == null || !(startElement instanceof GoBlock)) return; PsiElement brace = ((GoBlock)startElement).getRbrace(); if (brace == null) return; Template template = TemplateSettings.getInstance().getTemplateById("go_lang_add_return"); if (template == null) return; int start = brace.getTextRange().getStartOffset(); editor.getCaretModel().moveToOffset(start); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); template.setToReformat(true); TemplateManager.getInstance(project).startTemplate(editor, template, true, Collections.emptyMap(), null); } }
/** * Scrolls the editor to put the given visual line at the top of the current window. Ensures that any block inlay * elements above the given line are also visible. * * @param editor The editor to scroll * @param visualLine The visual line to place at the top of the current window * @return Returns true if the window was moved */ public static boolean scrollVisualLineToTopOfScreen(@NotNull final Editor editor, int visualLine) { final ScrollingModel scrollingModel = editor.getScrollingModel(); int inlayHeight = getHeightOfVisualLineInlays(editor, visualLine, true); int y = editor.visualLineToY(visualLine) - inlayHeight; int verticalPos = scrollingModel.getVerticalScrollOffset(); scrollingModel.scrollVertically(y); return verticalPos != scrollingModel.getVerticalScrollOffset(); }
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; }
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 scrollFullPageDown(@NotNull final Editor editor, int pages) { final Rectangle visibleArea = editor.getScrollingModel().getVisibleArea(); final int lineCount = getVisualLineCount(editor);
final ScrollingModel scrollingModel = editor.getScrollingModel(); final Rectangle visibleArea = scrollingModel.getVisibleArea(); final int caretScreenOffset = editor.visualLineToY(editor.getCaretModel().getVisualPosition().line) - visibleArea.y;
/** * Turns off the ex entry field and optionally puts the focus back to the original component */ public void deactivate(boolean refocusOwningEditor) { logger.info("deactivate"); if (!active) return; active = false; if (!ApplicationManager.getApplication().isUnitTestMode()) { if (refocusOwningEditor && parent != null) { UiHelper.requestFocus(parent); } oldGlass.removeComponentListener(adapter); oldGlass.setVisible(false); oldGlass.remove(this); oldGlass.setOpaque(wasOpaque); oldGlass.setLayout(oldLayout); if (isIncSearchEnabled(label.getText())) { entry.getDocument().removeDocumentListener(documentListener); final Editor editor = entry.getEditor(); editor.getScrollingModel().scrollVertically(verticalOffset); editor.getScrollingModel().scrollHorizontally(horizontalOffset); if (incHighlighter != null) { editor.getMarkupModel().removeHighlighter(incHighlighter); } } } parent = null; }
final ScrollingModel scrollingModel = editor.getScrollingModel(); int inlayHeight = getHeightOfVisualLineInlays(editor, visualLine, false); int y = editor.visualLineToY(visualLine);
final ScrollingModel scrollingModel = editor.getScrollingModel(); final Rectangle visibleArea = scrollingModel.getVisibleArea();
public boolean selectPreviousVisualMode(@NotNull Editor editor) { final SelectionType lastSelectionType = EditorData.getLastSelectionType(editor); if (lastSelectionType == null) { return false; } final TextRange visualMarks = VimPlugin.getMark().getVisualSelectionMarks(editor); if (visualMarks == null) { return false; } editor.getCaretModel().removeSecondaryCarets(); CommandState.getInstance(editor) .pushState(CommandState.Mode.VISUAL, lastSelectionType.toSubMode(), MappingMode.VISUAL); Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); CaretData.setVisualStart(primaryCaret, visualMarks.getStartOffset()); CaretData.setVisualEnd(primaryCaret, visualMarks.getEndOffset()); CaretData.setVisualOffset(primaryCaret, visualMarks.getEndOffset()); updateSelection(editor, primaryCaret, visualMarks.getEndOffset()); primaryCaret.moveToOffset(visualMarks.getEndOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.CENTER); return true; }
public boolean swapVisualSelections(@NotNull Editor editor) { final SelectionType lastSelectionType = EditorData.getLastSelectionType(editor); final TextRange lastVisualRange = EditorData.getLastVisualRange(editor); if (lastSelectionType == null || lastVisualRange == null) { return false; } final SelectionType selectionType = SelectionType.fromSubMode(CommandState.getInstance(editor).getSubMode()); EditorData.setLastSelectionType(editor, selectionType); editor.getCaretModel().removeSecondaryCarets(); Caret primaryCaret = editor.getCaretModel().getPrimaryCaret(); CaretData.setVisualStart(primaryCaret, lastVisualRange.getStartOffset()); CaretData.setVisualEnd(primaryCaret, lastVisualRange.getEndOffset()); CaretData.setVisualOffset(primaryCaret, lastVisualRange.getEndOffset()); CommandState.getInstance(editor).setSubMode(lastSelectionType.toSubMode()); updateSelection(editor, primaryCaret, lastVisualRange.getEndOffset()); primaryCaret.moveToOffset(lastVisualRange.getEndOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.CENTER); return true; }