@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); } }
/** * 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; }
int inlayHeight = getHeightOfVisualLineInlays(editor, visualLine, false); int y = editor.visualLineToY(visualLine); int verticalPos = scrollingModel.getVerticalScrollOffset(); int height = inlayHeight + editor.getLineHeight(); Rectangle visibleArea = scrollingModel.getVisibleArea(); scrollingModel.scrollVertically(topY - topLineInlayHeight); return verticalPos != scrollingModel.getVerticalScrollOffset();
/** * 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(); }
/** * 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 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; }
if (isIncSearchEnabled(label)) { entry.getDocument().addDocumentListener(documentListener); verticalOffset = editor.getScrollingModel().getVerticalScrollOffset(); horizontalOffset = editor.getScrollingModel().getHorizontalScrollOffset();
/** * 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; }
private static void scrollColumnToLeftOfScreen(@NotNull Editor editor, int column) { editor.getScrollingModel().scrollHorizontally(column * EditorHelper.getColumnWidth(editor)); }
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)); }
final Rectangle visibleArea = scrollingModel.getVisibleArea(); final int caretScreenOffset = editor.visualLineToY(editor.getCaretModel().getVisualPosition().line) - visibleArea.y; scrollingModel.scrollVertically(yVisualLine - caretScreenOffset - inlayOffset);
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; }
/** * 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 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; }
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); }
private void navigateToElement(TextRange[] textRange) { if(editor != null && textRange[0] != null) { editor.getCaretModel().moveToOffset(textRange[0].getStartOffset() + 1); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } }
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); }
@Override protected void run(@NotNull Result result) throws Throwable { StringBuffer textBuf = new StringBuffer(); textBuf.append("\n"); textBuf.append(methodCreated.getText()); editor.getDocument().insertString(insertPos, textBuf); final int endPos = insertPos + textBuf.length(); CodeStyleManager.getInstance(project).reformatText(phpClass.getContainingFile(), insertPos, endPos); PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); Method insertedMethod = phpClass.findMethodByName(functionName); if(insertedMethod != null) { editor.getCaretModel().moveToOffset(insertedMethod.getTextRange().getStartOffset()); editor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE); } }
/** * 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; }
/** * Scroll to the error specified by the given tree path, or do nothing * if no error is specified. * * @param treePath the tree path to scroll to. */ private void scrollToError(final TreePath treePath) { final DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent(); if (treeNode == null || !(treeNode.getUserObject() instanceof ResultTreeNode)) { return; } final ResultTreeNode nodeInfo = (ResultTreeNode) treeNode.getUserObject(); if (nodeInfo.getFile() == null || nodeInfo.getProblem() == null) { return; // no problem here :-) } final VirtualFile virtualFile = nodeInfo.getFile().getVirtualFile(); if (virtualFile == null || !virtualFile.exists()) { return; } final FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); final FileEditor[] editor = fileEditorManager.openFile( virtualFile, true); if (editor.length > 0 && editor[0] instanceof TextEditor) { final LogicalPosition problemPos = new LogicalPosition( Math.max(lineFor(nodeInfo) - 1, 0), Math.max(columnFor(nodeInfo), 0)); final Editor textEditor = ((TextEditor) editor[0]).getEditor(); textEditor.getCaretModel().moveToLogicalPosition(problemPos); textEditor.getScrollingModel().scrollToCaret(ScrollType.CENTER); } }