public int moveCaretHorizontalWrap(@NotNull Editor editor, @NotNull Caret caret, int count) { // FIX - allows cursor over newlines int oldOffset = caret.getOffset(); int offset = Math.min(Math.max(0, caret.getOffset() + count), EditorHelper.getFileSize(editor)); if (offset == oldOffset) { return -1; } else { return offset; } }
@NotNull public TextRange getVisualRange(@NotNull Caret caret) { return new TextRange(caret.getSelectionStart(), caret.getSelectionEnd()); }
int col = caret.getLogicalPosition().column; int len = EditorHelper.getLineLength(editor); int offset = caret.getOffset(); if (len - col < count) { return false; caret.moveToOffset(offset + 1); insertText(editor, caret, space); int slen = space.length(); slen++; caret.moveToOffset(offset + slen);
/** * Gets the visual block end for the caret. */ public static int getVisualEnd(@NotNull Caret caret) { Integer visualEnd = caret.getUserData(VISUAL_END); if (visualEnd == null) { return caret.getOffset(); } else { return visualEnd; } }
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; } }
CharSequence chars = editor.getDocument().getCharsSequence(); int max = EditorHelper.getFileSize(editor); int offset = caret.getOffset(); int ssel = caret.getSelectionStart(); int esel = caret.getSelectionEnd(); if (Math.abs(esel - ssel) > 1) { int start;
/** * Add markers with offsets for caret and selection */ private void addCaretInfo(@NotNull Caret caret, @NotNull List<Pair<Integer, String>> macroses) { macroses.add(Pair.create(caret.getOffset(), "<caret>")); if (caret.hasSelection()) { macroses.add(Pair.create(caret.getSelectionStart(), "<selection>")); macroses.add(Pair.create(caret.getSelectionEnd(), "</selection>")); } }
/** * Inserts text into the document * * @param editor The editor to insert into * @param caret The caret to start insertion in * @param str The text to insert */ public void insertText(@NotNull Editor editor, @NotNull Caret caret, @NotNull String str) { int start = caret.getOffset(); editor.getDocument().insertString(start, str); caret.moveToOffset(start + str.length()); VimPlugin.getMark().setMark(editor, MarkGroup.MARK_CHANGE_POS, start); }
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)); }
if (preSetQuery == null) { Caret caret = editor.getCaretModel().getPrimaryCaret(); analyticsEvent = caret.hasSelection() ? CONTENT_FROM_SELECT_ACTION : CONTENT_FROM_CARET_ACTION; if (caret.hasSelection()) { query = caret.getSelectedText(); } else if (nonNull(psiFile)) { String languageId = psiFile.getLanguage().getID(); if (isSupported(languageId)) { PsiElement cypherStatement = getCypherStatementAtOffset(psiFile, caret.getOffset()); if (nonNull(cypherStatement)) { query = cypherStatement.getText();
/** * 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; }
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)); if (pad.length() > 0) { final int offset = editor.getDocument().getLineEndOffset(logicalLine + i); caret.moveToOffset(offset); insertText(editor, caret, pad); caret.moveToOffset(VimPlugin.getMotion().moveCaretToLineEnd(editor, logicalLine + i, true)); repeatInsertText(editor, context, started ? (i == 0 ? count : count + 1) : count); caret.moveToVisualPosition(new VisualPosition(visualLine + i, repeatColumn)); repeatInsertText(editor, context, started ? (i == 0 ? count : count + 1) : count);
caret.moveToOffset(range.getStartOffset() + number.length() - 1);
protected void doTestWorldSelector() { initWithFileSmartWithoutErrors(); List<Integer> offsets = getAndRemoveCarets(); List<Pair<Integer, String>> macroses = new ArrayList<>(); Caret currentCaret = getEditor().getCaretModel().getCurrentCaret(); for (Integer offset : offsets) { currentCaret.moveToOffset(offset); new SelectWordHandler(null).execute(getEditor(), currentCaret, getEditorDataContext()); addCaretInfo(currentCaret, macroses); currentCaret.removeSelection(); } UsefulTestCase.assertSameLinesWithFile(getTestResultsFilePath(), getEditorTextWithMacroses(macroses)); }
if (len > from) { LogicalPosition spos = new LogicalPosition(l, from); caret.moveToOffset(editor.logicalPositionToOffset(spos)); insertText(editor, caret, indent); CaretData.setLastColumn(editor, caret, caret.getVisualPosition().column);
public void resetVisual(@NotNull final Editor editor, final boolean removeSelection) { final boolean wasVisualBlock = CommandState.inVisualBlockMode(editor); final SelectionType selectionType = SelectionType.fromSubMode(CommandState.getInstance(editor).getSubMode()); EditorData.setLastSelectionType(editor, selectionType); final TextRange visualMarks = VimPlugin.getMark().getVisualSelectionMarks(editor); if (visualMarks != null) { EditorData.setLastVisualRange(editor, visualMarks); } if (removeSelection) { if (!EditorData.isKeepingVisualOperatorAction(editor)) { for (Caret caret : editor.getCaretModel().getAllCarets()) { caret.removeSelection(); } } if (wasVisualBlock) { editor.getCaretModel().removeSecondaryCarets(); } } CommandState.getInstance(editor).setSubMode(CommandState.SubMode.NONE); }
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); } }
boolean isOuter) { CharSequence chars = editor.getDocument().getCharsSequence(); int pos = caret.getOffset(); int start = caret.getSelectionStart(); int end = caret.getSelectionEnd(); if (start != end) { pos = Math.min(start, end);
/** * Gets the visual block start for the caret. */ public static int getVisualStart(@NotNull Caret caret) { Integer visualStart = caret.getUserData(VISUAL_START); if (visualStart == null) { return caret.getOffset(); } else { return visualStart; } }