use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class MotionGroup method updateSelection.
private void updateSelection(@NotNull Editor editor, int offset) {
visualEnd = offset;
visualOffset = offset;
int start = visualStart;
int end = visualEnd;
final CommandState.SubMode subMode = CommandState.getInstance(editor).getSubMode();
if (subMode == CommandState.SubMode.VISUAL_CHARACTER) {
if (start > end) {
int t = start;
start = end;
end = t;
}
final BoundStringOption opt = (BoundStringOption) Options.getInstance().getOption("selection");
int lineEnd = EditorHelper.getLineEndForOffset(editor, end);
final int adj = opt.getValue().equals("exclusive") || end == lineEnd ? 0 : 1;
end = Math.min(EditorHelper.getFileSize(editor), end + adj);
editor.getSelectionModel().setSelection(start, end);
} else if (subMode == CommandState.SubMode.VISUAL_LINE) {
if (start > end) {
int t = start;
start = end;
end = t;
}
start = EditorHelper.getLineStartForOffset(editor, start);
end = EditorHelper.getLineEndForOffset(editor, end);
editor.getSelectionModel().setSelection(start, end);
} else if (subMode == CommandState.SubMode.VISUAL_BLOCK) {
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 (EditorData.getLastColumn(editor) >= MotionGroup.LAST_COLUMN) {
caret.setSelection(caret.getSelectionStart(), lineEndOffset);
}
if (!EditorHelper.isLineEmpty(editor, line, false)) {
caret.moveToOffset(caret.getSelectionEnd() - 1);
}
}
editor.getCaretModel().moveToOffset(end);
}
VimPlugin.getMark().setVisualSelectionMarks(editor, new TextRange(start, end));
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class VisualOperatorActionHandler method execute.
protected final boolean execute(@NotNull final Editor editor, @NotNull DataContext context, @NotNull Command cmd) {
if (logger.isDebugEnabled())
logger.debug("execute, cmd=" + cmd);
TextRange range;
if (CommandState.getInstance(editor).getMode() == CommandState.Mode.VISUAL) {
range = VimPlugin.getMotion().getVisualRange(editor);
if (logger.isDebugEnabled())
logger.debug("range=" + range);
}
VisualStartFinishRunnable runnable = new VisualStartFinishRunnable(editor, cmd);
range = runnable.start();
assert range != null : "Range must be not null for visual operator action " + getClass();
final boolean res = execute(editor, context, cmd, range);
runnable.setRes(res);
runnable.finish();
return res;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchHelper method findClosingTag.
@Nullable
private static Pair<TextRange, String> findClosingTag(@NotNull CharSequence sequence, int pos) {
int closeBracketPos = pos;
int openBracketPos;
while (closeBracketPos < sequence.length()) {
closeBracketPos = StringUtil.indexOf(sequence, '>', closeBracketPos);
if (closeBracketPos < 0) {
return null;
}
openBracketPos = closeBracketPos - 1;
while (openBracketPos >= 0) {
openBracketPos = StringUtil.lastIndexOf(sequence, '<', 0, openBracketPos);
if (openBracketPos >= 0 && openBracketPos + 1 < sequence.length() && sequence.charAt(openBracketPos + 1) == '/') {
final String tagName = String.valueOf(sequence.subSequence(openBracketPos + "</".length(), closeBracketPos));
if (tagName.length() > 0 && tagName.charAt(0) != ' ') {
TextRange textRange = new TextRange(openBracketPos, closeBracketPos);
return Pair.create(textRange, tagName);
}
}
openBracketPos--;
}
closeBracketPos++;
}
return null;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class MotionGroup method selectPreviousVisualMode.
public boolean selectPreviousVisualMode(@NotNull Editor editor) {
final SelectionType lastSelectionType = EditorData.getLastSelectionType(editor);
if (lastSelectionType == null) {
return false;
}
final TextRange visualMarks = VimPlugin.getMark().getVisualSelectionMarks(editor);
if (visualMarks == null) {
return false;
}
CommandState.getInstance(editor).pushState(CommandState.Mode.VISUAL, lastSelectionType.toSubMode(), MappingMode.VISUAL);
visualStart = visualMarks.getStartOffset();
visualEnd = visualMarks.getEndOffset();
visualOffset = visualEnd;
updateSelection(editor, visualEnd);
editor.getCaretModel().moveToOffset(visualOffset);
editor.getScrollingModel().scrollToCaret(ScrollType.CENTER);
return true;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class RegisterGroup method storeTextInternal.
public boolean storeTextInternal(@NotNull Editor editor, @NotNull TextRange range, @NotNull String text, @NotNull SelectionType type, char register, boolean isDelete) {
// Null register doesn't get saved
if (lastRegister == '_')
return true;
int start = range.getStartOffset();
int end = range.getEndOffset();
// Normalize the start and end
if (start > end) {
int t = start;
start = end;
end = t;
}
if (type == SelectionType.LINE_WISE && text.length() > 0 && text.charAt(text.length() - 1) != '\n') {
text = text + '\n';
}
// If this is an uppercase register, we need to append the text to the corresponding lowercase register
if (Character.isUpperCase(register)) {
char lreg = Character.toLowerCase(register);
Register r = registers.get(new Character(lreg));
// Append the text if the lowercase register existed
if (r != null) {
r.addText(text);
} else // Set the text if the lowercase register didn't exist yet
{
registers.put(lreg, new Register(lreg, type, text));
if (logger.isDebugEnabled())
logger.debug("register '" + register + "' contains: \"" + text + "\"");
}
} else if (CLIPBOARD_REGISTERS.contains(register)) {
ClipboardHandler.setClipboardText(text);
} else // Put the text in the specified register
{
registers.put(register, new Register(register, type, text));
if (logger.isDebugEnabled())
logger.debug("register '" + register + "' contains: \"" + text + "\"");
}
// Also add it to the default register if the default wasn't specified
if (register != defaultRegister && ".:/".indexOf(register) == -1) {
registers.put(defaultRegister, new Register(defaultRegister, type, text));
if (logger.isDebugEnabled())
logger.debug("register '" + register + "' contains: \"" + text + "\"");
}
if (isDelete) {
boolean smallInlineDeletion = type == SelectionType.CHARACTER_WISE && editor.offsetToLogicalPosition(start).line == editor.offsetToLogicalPosition(end).line;
// Deletes go into numbered registers only if text is smaller than a line, register is used or it's a special case
if (!smallInlineDeletion || register != defaultRegister || isSmallDeletionSpecialCase(editor)) {
// Old 1 goes to 2, etc. Old 8 to 9, old 9 is lost
for (char d = '8'; d >= '1'; d--) {
Register t = registers.get(d);
if (t != null) {
t.rename((char) (d + 1));
registers.put((char) (d + 1), t);
}
}
registers.put('1', new Register('1', type, text));
}
// Deletes smaller than one line and without specified register go the the "-" register
if (smallInlineDeletion && register == defaultRegister) {
registers.put('-', new Register('-', type, text));
}
} else // Yanks also go to register 0 if the default register was used
if (register == defaultRegister) {
registers.put('0', new Register('0', type, text));
if (logger.isDebugEnabled())
logger.debug("register '" + '0' + "' contains: \"" + text + "\"");
}
if (start != -1) {
VimPlugin.getMark().setChangeMarks(editor, new TextRange(start, Math.max(end - 1, 0)));
}
return true;
}
Aggregations