protected int getRangeLine(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, boolean lastZero) { if (line == LAST_LINE) line = EditorHelper.getLineCount(editor) - 1; else line = caret.getLogicalPosition().line; return line; }
/** * Gets the previous last column (set by {@link com.maddyhome.idea.vim.handler.VisualOperatorActionHandler.VisualStartFinishRunnable}). */ public static int getPreviousLastColumn(@NotNull Caret caret) { @Nullable Integer ret = caret.getUserData(PREV_LAST_COLUMN); if (ret == null) { return caret.getLogicalPosition().column; } else { return ret; } }
public int moveCaretToLineStartSkipLeading(@NotNull Editor editor, @NotNull Caret caret) { int logicalLine = caret.getLogicalPosition().line; return moveCaretToLineStartSkipLeading(editor, logicalLine); }
public int moveCaretToLineStart(@NotNull Editor editor, @NotNull Caret caret) { int logicalLine = caret.getLogicalPosition().line; return moveCaretToLineStart(editor, logicalLine); }
public int moveCaretToLineScreenStartSkipLeading(@NotNull Editor editor, @NotNull Caret caret) { final int col = EditorHelper.getVisualColumnAtLeftOfScreen(editor); final int logicalLine = caret.getLogicalPosition().line; return EditorHelper.getLeadingCharacterOffset(editor, logicalLine, col); }
public int moveCaretHorizontal(@NotNull Editor editor, @NotNull Caret caret, int count, boolean allowPastEnd) { int oldOffset = caret.getOffset(); int offset = EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, oldOffset + count, allowPastEnd); if (offset == oldOffset) { return -1; } else { return offset; } }
private static int findNextParagraphLine(@NotNull Editor editor, @NotNull Caret caret, int count, boolean allowBlanks) { int line = caret.getLogicalPosition().line; int maxline = EditorHelper.getLineCount(editor); int dir = count > 0 ? 1 : -1; boolean skipLines = count > 1; count = Math.abs(count); int total = count; for (; count > 0 && line >= 0; count--) { line = findNextParagraphLine(editor, line, dir, allowBlanks, skipLines); } if (total == 1 && line < 0) { line = dir > 0 ? maxline - 1 : 0; } else if (total > 1 && count == 0 && line < 0) { line = dir > 0 ? maxline - 1 : 0; } return line; }
@Override protected int getRangeLine(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, boolean lastZero) { int line = caret.getLogicalPosition().line; int offset = -1; for (int i = 0; i < patterns.size(); i++) { final String pattern = patterns.get(i); final int flag = flags.get(i); offset = VimPlugin.getSearch().search(editor, pattern, getSearchOffset(editor, line, flag, lastZero), 1, flag); if (offset == -1) break; line = editor.offsetToLogicalPosition(offset).line; } return offset != -1 ? line : -1; }
public int moveCaretToColumn(@NotNull Editor editor, @NotNull Caret caret, int count, boolean allowEnd) { int line = caret.getLogicalPosition().line; int pos = EditorHelper.normalizeColumn(editor, line, count, allowEnd); return editor.logicalPositionToOffset(new LogicalPosition(line, pos)); }
/** * Joins count lines together starting at the cursor. No count or a count of one still joins two lines. * * @param editor The editor to join the lines in * @param caret The caret in the first line to be joined. * @param count The number of lines to join * @param spaces If true the joined lines will have one space between them and any leading space on the second line * will be removed. If false, only the newline is removed to join the lines. * @return true if able to join the lines, false if not */ public boolean deleteJoinLines(@NotNull Editor editor, @NotNull Caret caret, int count, boolean spaces) { if (count < 2) count = 2; int lline = caret.getLogicalPosition().line; int total = EditorHelper.getLineCount(editor); //noinspection SimplifiableIfStatement if (lline + count > total) { return false; } return deleteJoinNLines(editor, caret, lline, count, spaces); }
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, int count, int rawCount, @Nullable Argument argument) { boolean result = true; for (Caret caret : editor.getCaretModel().getAllCarets()) { final int line = caret.getLogicalPosition().line; if (!VimPlugin.getSearch().searchAndReplace(editor, caret, new LineRange(line, line), "s", "//~/")) { result = false; } } return result; } }
public static int findSection(@NotNull Editor editor, @NotNull Caret caret, char type, int dir, int count) { CharSequence chars = editor.getDocument().getCharsSequence(); int line = caret.getLogicalPosition().line + dir; int maxline = EditorHelper.getLineCount(editor); int res = -1; while (line > 0 && line < maxline && count > 0) { int offset = EditorHelper.getLineStartOffset(editor, line); char ch = chars.charAt(offset); if (ch == type || ch == '\u000C') { res = offset; count--; } line += dir; } if (res == -1) { res = dir < 0 ? 0 : chars.length() - 1; } return res; }
private void processRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context) { startLine = defaultLine == -1 ? caret.getLogicalPosition().line : defaultLine; endLine = startLine; boolean lastZero = false; for (Range range : ranges) { startLine = endLine; endLine = range.getLine(editor, caret, context, lastZero); if (range.isMove()) MotionGroup.moveCaret(editor, caret, VimPlugin.getMotion().moveCaretToLine(editor, endLine)); lastZero = endLine < 0; ++count; } if (count == 1) startLine = endLine; count = 0; }
public void autoIndentLines(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int count) { final int startLine = caret.getLogicalPosition().line; final int endLine = startLine + count - 1; if (endLine <= EditorHelper.getLineCount(editor)) { final TextRange range = new TextRange(caret.getOffset(), editor.getDocument().getLineEndOffset(endLine)); autoIndentRange(editor, caret, context, range); } }
/** * Delete count characters and then enter insert mode * * @param editor The editor to change * @param caret The caret to be moved * @param count The number of characters to change * @return true if able to delete count characters, false if not */ public boolean changeCharacters(@NotNull Editor editor, @NotNull Caret caret, int count) { int len = EditorHelper.getLineLength(editor); int col = caret.getLogicalPosition().column; if (col + count >= len) { return changeEndOfLine(editor, caret, 1); } boolean res = deleteCharacter(editor, caret, count, true); if (res) { EditorData.setChangeSwitchMode(editor, CommandState.Mode.INSERT); } return res; }
public void autoIndentRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, @NotNull TextRange range) { final int startOffset = EditorHelper.getLineStartForOffset(editor, range.getStartOffset()); final int endOffset = EditorHelper.getLineEndForOffset(editor, range.getEndOffset()); editor.getSelectionModel().setSelection(startOffset, endOffset); KeyHandler.executeAction("AutoIndentLines", context); final int firstLine = editor.offsetToLogicalPosition(Math.min(startOffset, endOffset)).line; final int newOffset = VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, firstLine); MotionGroup.moveCaret(editor, caret, newOffset); restoreCursor(editor, caret, caret.getLogicalPosition().line); }
/** * Deletes count character after the caret from the editor * * @param editor The editor to remove characters from * @param caret The caret on which the operation is performed * @param count The numbers of characters to delete. * @return true if able to delete, false if not */ public boolean deleteCharacter(@NotNull Editor editor, @NotNull Caret caret, int count, boolean isChange) { final int endOffset = VimPlugin.getMotion().moveCaretHorizontal(editor, caret, count, true); if (endOffset != -1) { final boolean res = deleteText(editor, new TextRange(caret.getOffset(), endOffset), SelectionType.CHARACTER_WISE); final int pos = caret.getOffset(); final int norm = EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, pos, isChange); if (norm != pos) { MotionGroup.moveCaret(editor, caret, norm); } return res; } return false; }
public boolean deleteCharacter(@NotNull Editor editor, int count, boolean isChange) { final int caretCount = editor.getCaretModel().getCaretCount(); final List<Integer> startOffsets = Lists.newArrayListWithCapacity(caretCount); final List<Integer> endOffsets = Lists.newArrayListWithCapacity(caretCount); final List<Caret> carets = EditorHelper.getOrderedCaretsList(editor, count > 0 ? CaretOrder.DECREASING_OFFSET : CaretOrder.INCREASING_OFFSET); boolean result = true; for (int i = 0; i < caretCount; i++) { final Caret caret = carets.get(i); final int endOffset = VimPlugin.getMotion().moveCaretHorizontal(editor, caret, count, true); if (endOffset == -1) { result = false; continue; } final int startOffset = caret.getOffset(); startOffsets.add(startOffset); endOffsets.add(endOffset); result = deleteText(editor, new TextRange(startOffset, endOffset), SelectionType.CHARACTER_WISE); final int normalizeOffset = EditorHelper.normalizeOffset(editor, caret.getLogicalPosition().line, startOffset, isChange); if (normalizeOffset != startOffset) MotionGroup.moveCaret(editor, caret, normalizeOffset); } if (caretCount > 1 && result) { final TextRange range = new TextRange(ArrayUtil.toIntArray(startOffsets), ArrayUtil.toIntArray(endOffsets)); VimPlugin.getRegister().storeText(editor, range, range.isMultiple() ? SelectionType.LINE_WISE : SelectionType.CHARACTER_WISE, true); } return result; }
private int getStartOffset(@NotNull Editor editor, @NotNull Caret caret, SelectionType type, boolean beforeCursor) { if (beforeCursor) { return type == SelectionType.LINE_WISE ? VimPlugin.getMotion().moveCaretToLineStart(editor, caret) : caret.getOffset(); } int startOffset; if (type == SelectionType.LINE_WISE) { startOffset = Math.min(editor.getDocument().getTextLength(), VimPlugin.getMotion().moveCaretToLineEnd(editor, caret) + 1); if (startOffset > 0 && startOffset == editor.getDocument().getTextLength() && editor.getDocument().getCharsSequence().charAt(startOffset - 1) != '\n') { editor.getDocument().insertString(startOffset, "\n"); startOffset++; } } else { startOffset = caret.getOffset(); if (!EditorHelper.isLineEmpty(editor, caret.getLogicalPosition().line, false)) { startOffset++; } } if (startOffset > 0 && startOffset > editor.getDocument().getTextLength()) return startOffset - 1; return startOffset; }
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)); }