public List<Integer> getOrderedLines(@NotNull Editor editor, @NotNull DataContext context, @NotNull CaretOrder caretOrder) { final ArrayList<Integer> lines = new ArrayList<>(editor.getCaretModel().getCaretCount()); for (Caret caret : EditorHelper.getOrderedCaretsList(editor, caretOrder)) { final int line = getLine(editor, caret, context); lines.add(line); } return lines; }
public void displayHexInfo(@NotNull Editor editor) { int offset = editor.getCaretModel().getOffset(); char ch = editor.getDocument().getCharsSequence().charAt(offset); VimPlugin.showMessage(Long.toHexString(ch)); }
private void deinitLineNumbers(@NotNull Editor editor) { editor.getCaretModel().removeCaretListener(myLineNumbersCaretListener); EditorData.setEditorGroup(editor, false); editor.getGutter().closeAllAnnotations(); final Project project = editor.getProject(); if (project == null || project.isDisposed()) return; editor.getSettings().setLineNumbersShown(EditorData.isLineNumbersShown(editor)); }
/** * Gets the offset of the end of the line containing the supplied offset * * @param editor The editor * @param offset The offset within the line * @return The offset of the line end */ public static int getLineEndForOffset(@NotNull final Editor editor, final int offset) { LogicalPosition pos = editor.offsetToLogicalPosition(normalizeOffset(editor, offset)); return editor.getDocument().getLineEndOffset(pos.line); }
@NotNull public static RangeHighlighter highlightMatch(@NotNull Editor editor, int start, int end) { TextAttributes color = editor.getColorsScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); return editor.getMarkupModel().addRangeHighlighter(start, end, HighlighterLayer.ADDITIONAL_SYNTAX + 1, color, HighlighterTargetArea.EXACT_RANGE); }
private static void resetCursor(@NotNull Editor editor, boolean insert) { Document doc = editor.getDocument(); VirtualFile vf = FileDocumentManager.getInstance().getFile(doc); if (vf != null) { resetCursor(vf, editor.getProject(), insert); } else { editor.getSettings().setBlockCursor(!insert); } }
@Override public void execute(@NotNull Editor editor, char c, @NotNull DataContext dataContext) { if (myOriginalHandler != null) myOriginalHandler.execute(editor, c, dataContext); if (c != 'e') return; Project project = editor.getProject(); if (project == null) return; int offset = editor.getCaretModel().getOffset(); if (offset < 4) return; TextRange from = TextRange.from(offset - 4, 4); String text = editor.getDocument().getText(from); if ("case".equals(text)) { PsiDocumentManager.getInstance(project).commitDocument(editor.getDocument()); ApplicationManager.getApplication().runWriteAction(() -> { if (project.isDisposed()) return; PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); if (file == null) return; CodeStyleManager.getInstance(project).adjustLineIndent(file, from); }); } } }
/*** * @see :help visualmode() */ @Nullable public static SelectionType getLastSelectionType(@NotNull Editor editor) { return editor.getDocument().getUserData(LAST_SELECTION_TYPE); }
private void processMouseReleased(@NotNull Editor editor, @NotNull CommandState.SubMode mode, int startOff, int endOff) { if (ExEntryPanel.getInstance().isActive()) { ExEntryPanel.getInstance().deactivate(false); } ExOutputModel.getInstance(editor).clear(); if (CommandState.getInstance(editor).getMode() == CommandState.Mode.VISUAL) { CommandState.getInstance(editor).popState(); } int start = editor.getSelectionModel().getSelectionStart(); int end = editor.getSelectionModel().getSelectionEnd(); if (start == end) return; if (mode == CommandState.SubMode.VISUAL_LINE) { end--; endOff--; } if (end == startOff || end == endOff) { int t = start; start = end; end = t; if (mode == CommandState.SubMode.VISUAL_CHARACTER) { start--; } } MotionGroup.moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), start); toggleVisual(editor, 1, 0, mode); MotionGroup.moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), end); KeyHandler.getInstance().reset(editor); }
public static void moveCaret(@NotNull Editor editor, @NotNull Caret caret, int offset, boolean forceKeepVisual) { if (offset >= 0 && offset <= editor.getDocument().getTextLength()) { final boolean keepVisual = forceKeepVisual || keepVisual(editor); if (caret.getOffset() != offset) { caret.moveToOffset(offset); CaretData.setLastColumn(editor, caret, caret.getVisualPosition().column); if (caret == editor.getCaretModel().getPrimaryCaret()) { scrollCaretIntoView(editor); } } if (keepVisual) { VimPlugin.getMotion().updateSelection(editor, caret, offset); } else { editor.getSelectionModel().removeSelection(); } } }
private void updateBlockSelection(@NotNull Editor editor, int offset) { EditorData.setVisualBlockEnd(editor, offset); EditorData.setVisualBlockOffset(editor, offset); int start = EditorData.getVisualBlockStart(editor); int end = EditorData.getVisualBlockEnd(editor); LogicalPosition blockStart = editor.offsetToLogicalPosition(start); LogicalPosition blockEnd = editor.offsetToLogicalPosition(end); if (blockStart.column < blockEnd.column) { blockEnd = new LogicalPosition(blockEnd.line, blockEnd.column + 1); } else { blockStart = new LogicalPosition(blockStart.line, blockStart.column + 1); } editor.getSelectionModel().setBlockSelection(blockStart, blockEnd); for (Caret caret : editor.getCaretModel().getAllCarets()) { int line = caret.getLogicalPosition().line; int lineEndOffset = EditorHelper.getLineEndOffset(editor, line, true); if (CaretData.getLastColumn(editor.getCaretModel().getPrimaryCaret()) >= MotionGroup.LAST_COLUMN) { caret.setSelection(caret.getSelectionStart(), lineEndOffset); } if (!EditorHelper.isLineEmpty(editor, line, false)) { caret.moveToOffset(caret.getSelectionEnd() - 1); } } editor.getCaretModel().getPrimaryCaret().moveToOffset(end); VimPlugin.getMark().setVisualSelectionMarks(editor, new TextRange(start, end)); }
/** * This processes all "regular" keystrokes entered while in insert/replace mode * * @param editor The editor the character was typed into * @param context The data context * @param key The user entered keystroke * @return true if this was a regular character, false if not */ public boolean processKey(@NotNull final Editor editor, @NotNull final DataContext context, @NotNull final KeyStroke key) { if (logger.isDebugEnabled()) { logger.debug("processKey(" + key + ")"); } if (key.getKeyChar() != KeyEvent.CHAR_UNDEFINED) { final Document doc = editor.getDocument(); CommandProcessor.getInstance().executeCommand(editor.getProject(), () -> ApplicationManager.getApplication().runWriteAction( () -> KeyHandler.getInstance().getOriginalHandler().execute( editor, key.getKeyChar(), context)), "", doc, UndoConfirmationPolicy.DEFAULT, doc); return true; } return false; }
@Override protected void textChanged(DocumentEvent e) { final Editor editor = entry.getEditor(); final boolean forwards = !label.getText().equals("?"); if (incHighlighter != null) { editor.getMarkupModel().removeHighlighter(incHighlighter); } final String pattern = entry.getText(); final TextRange range = SearchGroup.findNext(editor, pattern, editor.getCaretModel().getOffset(), true, forwards); if (range != null) { final TextAttributes color = editor.getColorsScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES); incHighlighter = SearchGroup.highlightMatch(editor, range.getStartOffset(), range.getEndOffset()); incHighlighter.setErrorStripeMarkColor(color.getBackgroundColor()); incHighlighter.setErrorStripeTooltip(pattern); MotionGroup.scrollPositionIntoView(editor, editor.offsetToVisualPosition(range.getStartOffset()), true); } } };
private void initLineNumbers(@NotNull final Editor editor) { editor.getCaretModel().addCaretListener(myLineNumbersCaretListener); EditorData.setEditorGroup(editor, true); final EditorSettings settings = editor.getSettings(); EditorData.setLineNumbersShown(editor, settings.isLineNumbersShown()); updateLineNumbers(editor); }
int start = editor.getSelectionModel().getSelectionStart(); int end = editor.getSelectionModel().getSelectionEnd(); editor.getSelectionModel().setSelection(start, Math.max(start, end - 1)); final CaretModel caretModel = editor.getCaretModel(); if (CommandState.getInstance(editor).getSubMode() != CommandState.SubMode.NONE) { caretModel.removeSecondaryCarets(); break; case VISUAL_LINE: caretModel.moveToLogicalPosition(editor.xyToLogicalPosition(event.getPoint())); break;
@NotNull private LineRange getLineRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, @NotNull ExCommand cmd) { final LineRange range = cmd.getLineRange(editor, caret, context); final LineRange normalizedRange; // Something like "30,20sort" gets converted to "20,30sort" if (range.getEndLine() < range.getStartLine()) { normalizedRange = new LineRange(range.getEndLine(), range.getStartLine()); } else { normalizedRange = range; } // If we don't have a range, we either have "sort", a selection, or a block if (normalizedRange.getEndLine() - normalizedRange.getStartLine() == 0) { // If we have a selection. final SelectionModel selectionModel = editor.getSelectionModel(); if (selectionModel.hasSelection()) { final int start = selectionModel.getSelectionStart(); final int end = selectionModel.getSelectionEnd(); final int startLine = editor.offsetToLogicalPosition(start).line; final int endLine = editor.offsetToLogicalPosition(end).line; return new LineRange(startLine, endLine); } // If we have a generic selection, i.e. "sort" entire document else { return new LineRange(0, editor.getDocument().getLineCount() - 1); } } return normalizedRange; }
public void selectionChanged(@NotNull SelectionEvent selectionEvent) { final Editor editor = selectionEvent.getEditor(); final Document document = editor.getDocument(); if (myMakingChanges || (document instanceof DocumentEx && ((DocumentEx) document).isInEventsHandling())) { return; } myMakingChanges = true; try { final com.intellij.openapi.util.TextRange newRange = selectionEvent.getNewRange(); for (Editor e : EditorFactory.getInstance().getEditors(document)) { if (!e.equals(editor)) { e.getSelectionModel().setSelection(newRange.getStartOffset(), newRange.getEndOffset()); } } } finally { myMakingChanges = false; } } }
int start = editor.getDocument().getLineStartOffset(line1); int end = editor.getDocument().getLineEndOffset(line2); if (do_ask) { RangeHighlighter hl = highlightConfirm(editor, startoff, endoff); MotionGroup.scrollPositionIntoView(editor, editor.offsetToVisualPosition(startoff), true); MotionGroup.moveCaret(editor, caret, start); final ReplaceConfirmationChoice choice = confirmChoice(editor, match); editor.getMarkupModel().removeHighlighter(hl); switch (choice) { case SUBSTITUTE_THIS: editor.getDocument().replaceString(startoff, endoff, match); lastMatch = startoff; newpos = EditorHelper.offsetToCharacterPosition(editor, newend); editor.offsetToLogicalPosition( lastMatch).line));
@Nullable private Editor selectEditor(@NotNull Editor editor, @NotNull VirtualFile file) { return VimPlugin.getFile().selectEditor(editor.getProject(), file); }
public void addEditorSelectionListener(@NotNull Editor editor, @NotNull SelectionListener listener) { editor.getSelectionModel().addSelectionListener(listener); }