/** * This gets the last column the caret was in. * * @param caret The caret to get the last column for * @return Returns the last column as set by {@link #setLastColumn} or the current caret column */ public static int getLastColumn(@NotNull Caret caret) { Integer col = caret.getUserData(LAST_COLUMN); if (col == null) { return caret.getVisualPosition().column; } else { return col; } }
public int moveCaretToLineEndOffset(@NotNull Editor editor, @NotNull Caret caret, int cntForward, boolean allowPastEnd) { int line = EditorHelper.normalizeVisualLine(editor, caret.getVisualPosition().line + cntForward); if (line < 0) { return 0; } else { return moveCaretToLineEnd(editor, EditorHelper.visualLineToLogicalLine(editor, line), allowPastEnd); } }
public int moveCaretToLineStartSkipLeadingOffset(@NotNull Editor editor, @NotNull Caret caret, int linesOffset) { int line = EditorHelper.normalizeVisualLine(editor, caret.getVisualPosition().line + linesOffset); return moveCaretToLineStartSkipLeading(editor, EditorHelper.visualLineToLogicalLine(editor, line)); }
public int moveCaretToLineEnd(@NotNull Editor editor, @NotNull Caret caret) { final VisualPosition visualPosition = caret.getVisualPosition(); final int lastVisualLineColumn = EditorUtil.getLastVisualLineColumnNumber(editor, visualPosition.line); final VisualPosition visualEndOfLine = new VisualPosition(visualPosition.line, lastVisualLineColumn, true); return moveCaretToLineEnd(editor, editor.visualToLogicalPosition(visualEndOfLine).line, true); }
public int moveCaretToLineEndSkipLeadingOffset(@NotNull Editor editor, @NotNull Caret caret, int linesOffset) { int line = EditorHelper.visualLineToLogicalLine(editor, EditorHelper.normalizeVisualLine(editor, caret.getVisualPosition().line + linesOffset)); int start = EditorHelper.getLineStartOffset(editor, line); int end = EditorHelper.getLineEndOffset(editor, line, true); CharSequence chars = editor.getDocument().getCharsSequence(); int pos = start; for (int offset = end; offset > start; offset--) { if (offset >= chars.length()) { break; } if (!Character.isWhitespace(chars.charAt(offset))) { pos = offset; break; } } return pos; }
public int moveCaretVertical(@NotNull Editor editor, @NotNull Caret caret, int count) { VisualPosition pos = caret.getVisualPosition(); if ((pos.line == 0 && count < 0) || (pos.line >= EditorHelper.getVisualLineCount(editor) - 1 && count > 0)) { return -1; } else { int col = CaretData.getLastColumn(caret); int line = EditorHelper.normalizeVisualLine(editor, pos.line + count); VisualPosition newPos = new VisualPosition(line, EditorHelper .normalizeVisualColumn(editor, line, col, CommandState.inInsertMode(editor))); return EditorHelper.visualPositionToOffset(editor, newPos); } }
/** * Inserts the character above/below the cursor at the cursor location * * @param editor The editor to insert into * @param caret The caret to insert after * @param dir 1 for getting from line below cursor, -1 for getting from line above cursor * @return true if able to get the character and insert it, false if not */ public boolean insertCharacterAroundCursor(@NotNull Editor editor, @NotNull Caret caret, int dir) { boolean res = false; VisualPosition vp = caret.getVisualPosition(); vp = new VisualPosition(vp.line + dir, vp.column); int len = EditorHelper.getLineLength(editor, EditorHelper.visualLineToLogicalLine(editor, vp.line)); if (vp.column < len) { int offset = EditorHelper.visualPositionToOffset(editor, vp); char ch = editor.getDocument().getCharsSequence().charAt(offset); editor.getDocument().insertString(caret.getOffset(), Character.toString(ch)); MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretHorizontal(editor, caret, 1, true)); res = true; } return res; }
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(); } } }
/** * Inserts a new line above the caret position * * @param editor The editor to insert into * @param caret The caret to insert above * @param col The column to indent to */ private void insertNewLineAbove(@NotNull Editor editor, @NotNull Caret caret, int col) { if (editor.isOneLineMode()) return; if (caret.getVisualPosition().line == 0) { MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineStart(editor, caret)); CaretData.setWasInFirstLine(caret, true); } else { MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1)); MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret)); } EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT); insertText(editor, caret, "\n" + StringUtil.repeat(" ", col)); if (CaretData.wasInFirstLine(caret)) { MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1)); CaretData.setWasInFirstLine(caret, false); } }
for (Caret caret : editor.getCaretModel().getAllCarets()) { if (repeatLines > 0) { final int visualLine = caret.getVisualPosition().line; final int logicalLine = caret.getLogicalPosition().line; final int position = editor.logicalPositionToOffset(new LogicalPosition(logicalLine, repeatColumn));
/** * Begin insert before the current line by creating a new blank line above the current line * for all carets * * @param editor The editor to insert into */ public void insertNewLineAbove(@NotNull final Editor editor, @NotNull DataContext context) { if (editor.isOneLineMode()) return; for (Caret caret : editor.getCaretModel().getAllCarets()) { if (caret.getVisualPosition().line == 0) { MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineStart(editor, caret)); CaretData.setWasInFirstLine(caret, true); } else { MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1)); MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLineEnd(editor, caret)); } } initInsert(editor, context, CommandState.Mode.INSERT); runEnterAction(editor, context); for (Caret caret : editor.getCaretModel().getAllCarets()) { if (CaretData.wasInFirstLine(caret)) { CaretData.setWasInFirstLine(caret, false); MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretVertical(editor, caret, -1)); } } }
CaretData.setLastColumn(editor, caret, caret.getVisualPosition().column);