/** * Counts number of lines in the visual block. * <p> * The result includes empty and short lines which does not have explicit start position (caret). * * @param editor The editor the block was selected in * @param range The range corresponding to the selected block * @return total number of lines */ private static int getLinesCountInVisualBlock(@NotNull Editor editor, @NotNull TextRange range) { final int[] startOffsets = range.getStartOffsets(); if (startOffsets.length == 0) return 0; final LogicalPosition firstStart = editor.offsetToLogicalPosition(startOffsets[0]); final LogicalPosition lastStart = editor.offsetToLogicalPosition(startOffsets[range.size() - 1]); return lastStart.line - firstStart.line + 1; }
/** * Joins all the lines selected by the current visual selection. * * @param editor The editor to join the lines in * @param caret The caret to be moved after joining * @param range The range of the visual selection * @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 deleteJoinRange(@NotNull Editor editor, @NotNull Caret caret, @NotNull TextRange range, boolean spaces) { int startLine = editor.offsetToLogicalPosition(range.getStartOffset()).line; int endLine = editor.offsetToLogicalPosition(range.getEndOffset()).line; int count = endLine - startLine + 1; if (count < 2) count = 2; return deleteJoinNLines(editor, caret, startLine, count, spaces); }
@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; }
/** * 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); }
public static int normalizeOffset(@NotNull final Editor editor, int offset, final boolean allowEnd) { if (offset <= 0) { offset = 0; } final int textLength = editor.getDocument().getTextLength(); if (offset > textLength) { offset = textLength; } final int line = editor.offsetToLogicalPosition(offset).line; return normalizeOffset(editor, line, offset, allowEnd); }
/** * Gets the offset of the start of the line containing the supplied offset * * @param editor The editor * @param offset The offset within the line * @return The offset of the line start */ public static int getLineStartForOffset(@NotNull final Editor editor, final int offset) { LogicalPosition pos = editor.offsetToLogicalPosition(normalizeOffset(editor, offset)); return editor.getDocument().getLineStartOffset(pos.line); }
/** * Gets the number of characters on the specified logical line. This will be different than the number of visual * characters if there are "real" tabs in the line. * * @param editor The editor * @param line The logical line within the file * @return The number of characters in the specified line */ public static int getLineLength(@NotNull final Editor editor, final int line) { if (getLineCount(editor) == 0) { return 0; } else { return Math.max(0, editor.offsetToLogicalPosition(editor.getDocument().getLineEndOffset(line)).column); } }
private int doIndent(@NotNull Editor editor, @NotNull Caret caret, @NotNull DataContext context, int startOffset, int endOffset) { final int startLine = editor.offsetToLogicalPosition(startOffset).line; final int endLine = editor.offsetToLogicalPosition(endOffset - 1).line; final int startLineOffset = editor.getDocument().getLineStartOffset(startLine); final int endLineOffset = editor.getDocument().getLineEndOffset(endLine); VimPlugin.getChange().autoIndentRange(editor, caret, context, new TextRange(startLineOffset, endLineOffset)); return EditorHelper.getLineEndOffset(editor, endLine, true); }
private void addJump(@NotNull Editor editor, int offset, boolean reset) { final VirtualFile vf = EditorData.getVirtualFile(editor); if (vf == null) { return; } LogicalPosition lp = editor.offsetToLogicalPosition(offset); Jump jump = new Jump(lp.line, lp.column, vf.getPath()); final String filename = jump.getFilename(); for (int i = 0; i < jumps.size(); i++) { Jump j = jumps.get(i); if (filename != null && filename.equals(j.getFilename()) && j.getLogicalLine() == jump.getLogicalLine()) { jumps.remove(i); break; } } jumps.add(jump); if (reset) { jumpSpot = -1; } else { jumpSpot++; } if (jumps.size() > SAVE_JUMP_COUNT) { jumps.remove(0); } }
/** * Delete count lines and then enter insert mode * * @param editor The editor to change * @param caret The caret on the line to be changed * @param count The number of lines to change * @return true if able to delete count lines, false if not */ public boolean changeLine(@NotNull Editor editor, @NotNull Caret caret, int count) { final LogicalPosition pos = editor.offsetToLogicalPosition(caret.getOffset()); final boolean insertBelow = pos.line + count >= EditorHelper.getLineCount(editor); final LogicalPosition lp = editor.offsetToLogicalPosition( VimPlugin.getMotion().moveCaretToLineStartSkipLeading(editor, caret)); boolean res = deleteLine(editor, caret, count); if (res) { if (insertBelow) { insertNewLineBelow(editor, caret, lp.column); } else { insertNewLineAbove(editor, caret, lp.column); } } 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); }
/** * This updates all the marks for a file whenever text is inserted into the file. If the line that contains a mark * that is after the start of the insertion point, shift the mark by the number of new lines added. * * @param editor The editor that was updated * @param marks The editor's marks * @param insStartOff The insertion point * @param insLength The length of the insertion */ public static void updateMarkFromInsert(@Nullable Editor editor, @Nullable HashMap<Character, Mark> marks, int insStartOff, int insLength) { if (marks != null && marks.size() > 0 && editor != null) { int insEndOff = insStartOff + insLength; LogicalPosition insStart = editor.offsetToLogicalPosition(insStartOff); LogicalPosition insEnd = editor.offsetToLogicalPosition(insEndOff); if (logger.isDebugEnabled()) logger.debug("mark insert. insStart = " + insStart + ", insEnd = " + insEnd); int lines = insEnd.line - insStart.line; if (lines == 0) return; for (Mark mark : marks.values()) { if (logger.isDebugEnabled()) logger.debug("mark = " + mark); // Shift the mark if the insertion began on a line prior to the marked line. if (insStart.line < mark.getLogicalLine()) { mark.setLogicalLine(mark.getLogicalLine() + lines); if (logger.isDebugEnabled()) logger.debug("Shifting mark by " + lines + " lines"); } } } }
line = editor.offsetToLogicalPosition(pos).line;
public boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull ExCommand cmd) throws ExException { LineRange range = cmd.getLineRange(editor, context); CharSequence chars = editor.getDocument().getCharsSequence(); for (int l = range.getStartLine(); l <= range.getEndLine(); l++) { int start = editor.getDocument().getLineStartOffset(l); int end = editor.getDocument().getLineEndOffset(l); if (logger.isDebugEnabled()) { logger.debug("Line " + l + ", start offset=" + start + ", end offset=" + end); for (int i = start; i <= end; i++) { logger.debug("Offset " + i + ", char=" + chars.charAt(i) + ", lp=" + editor.offsetToLogicalPosition(i) + ", vp=" + editor.offsetToVisualPosition(i)); } } } return true; }
int selectionStartLine = editor.offsetToLogicalPosition(selectionStart).line; int selectionEndLine = editor.offsetToLogicalPosition(selectionEnd).line; int caretLine = editor.offsetToLogicalPosition(caretOffset).line; int currentVisualStartLine = editor.offsetToLogicalPosition(CaretData.getVisualStart(caret)).line; int currentVisualEndLine = editor.offsetToLogicalPosition(CaretData.getVisualEnd(caret)).line; if (selectionStartLine == caretLine) { if (currentVisualStartLine != selectionEndLine) {
@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; }
protected boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull Command cmd) { final Argument argument = cmd.getArgument(); if (argument == null) { return false; } TextRange range = MotionGroup .getMotionRange(editor, editor.getCaretModel().getPrimaryCaret(), context, cmd.getCount(), cmd.getRawCount(), argument, false); if (range == null) { return false; } LogicalPosition current = editor.getCaretModel().getLogicalPosition(); LogicalPosition start = editor.offsetToLogicalPosition(range.getStartOffset()); LogicalPosition end = editor.offsetToLogicalPosition(range.getEndOffset()); if (current.line != start.line) { MotionGroup.moveCaret(editor, editor.getCaretModel().getPrimaryCaret(), range.getStartOffset()); } int count; if (start.line < end.line) { count = end.line - start.line + 1; } else { count = 1; } Command command = new Command(count, null, null, Command.Type.UNDEFINED, 0); VimPlugin.getProcess().startFilterCommand(editor, context, command); return true; } }
int sl = editor.offsetToLogicalPosition(soff).line; int el = editor.offsetToLogicalPosition(eoff).line; VimPlugin.getSearch().highlightSearchLines(editor, false, sl, el); hls = EditorData.getLastHighlights(editor);
LogicalPosition sp = editor.offsetToLogicalPosition(start); int endLine = sp.line + lines - 1; int res;
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)); }