use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchGroup method findNext.
@Nullable
public static TextRange findNext(@NotNull Editor editor, @NotNull String pattern, final int offset, boolean ignoreCase, final boolean forwards) {
final List<TextRange> results = findAll(editor, pattern, 0, -1, shouldIgnoreCase(pattern, ignoreCase));
if (results.isEmpty()) {
return null;
}
final int size = EditorHelper.getFileSize(editor);
final TextRange max = Collections.max(results, new Comparator<TextRange>() {
@Override
public int compare(TextRange r1, TextRange r2) {
final int d1 = distance(r1, offset, forwards, size);
final int d2 = distance(r2, offset, forwards, size);
if (d1 < 0 && d2 >= 0) {
return Integer.MAX_VALUE;
}
return d2 - d1;
}
});
if (!Options.getInstance().isSet("wrapscan")) {
final int start = max.getStartOffset();
if (forwards && start < offset || start >= offset) {
return null;
}
}
return max;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method deleteJoinNLines.
/**
* This does the actual joining of the lines
*
* @param editor The editor to join the lines in
* @param startLine The starting logical line
* @param count The number of lines to join including startLine
* @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
*/
private boolean deleteJoinNLines(@NotNull Editor editor, int startLine, int count, boolean spaces) {
// start my moving the cursor to the very end of the first line
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineEnd(editor, startLine, true));
for (int i = 1; i < count; i++) {
int start = VimPlugin.getMotion().moveCaretToLineEnd(editor);
int trailingWhitespaceStart = VimPlugin.getMotion().moveCaretToLineEndSkipLeading(editor);
boolean hasTrailingWhitespace = start != trailingWhitespaceStart + 1;
MotionGroup.moveCaret(editor, start);
int offset;
if (spaces) {
offset = VimPlugin.getMotion().moveCaretToLineStartSkipLeadingOffset(editor, 1);
} else {
offset = VimPlugin.getMotion().moveCaretToLineStartOffset(editor);
}
deleteText(editor, new TextRange(editor.getCaretModel().getOffset(), offset), null);
if (spaces && !hasTrailingWhitespace) {
insertText(editor, start, " ");
MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretHorizontal(editor, -1, false));
}
}
return true;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method deleteMotion.
/**
* Delete all text moved over by the supplied motion command argument.
*
* @param editor The editor to delete the text from
* @param context The data context
* @param count The number of times to repeat the deletion
* @param rawCount The actual count entered by the user
* @param argument The motion command
* @param isChange if from a change
* @return true if able to delete the text, false if not
*/
public boolean deleteMotion(@NotNull Editor editor, DataContext context, int count, int rawCount, @NotNull final Argument argument, boolean isChange) {
final TextRange range = getDeleteMotionRange(editor, context, count, rawCount, argument);
if (range == null) {
return (EditorHelper.getFileSize(editor) == 0);
}
// Delete motion commands that are not linewise become linewise if all the following are true:
// 1) The range is across multiple lines
// 2) There is only whitespace before the start of the range
// 3) There is only whitespace after the end of the range
final Command motion = argument.getMotion();
if (motion == null) {
return false;
}
if (!isChange && (motion.getFlags() & Command.FLAG_MOT_LINEWISE) == 0) {
LogicalPosition start = editor.offsetToLogicalPosition(range.getStartOffset());
LogicalPosition end = editor.offsetToLogicalPosition(range.getEndOffset());
if (start.line != end.line) {
if (!SearchHelper.anyNonWhitespace(editor, range.getStartOffset(), -1) && !SearchHelper.anyNonWhitespace(editor, range.getEndOffset(), 1)) {
int flags = motion.getFlags();
flags &= ~Command.FLAG_MOT_EXCLUSIVE;
flags &= ~Command.FLAG_MOT_INCLUSIVE;
flags |= Command.FLAG_MOT_LINEWISE;
motion.setFlags(flags);
}
}
}
return deleteRange(editor, range, SelectionType.fromCommandFlags(motion.getFlags()), isChange);
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method changeNumber.
public boolean changeNumber(@NotNull final Editor editor, final int count) {
final BoundListOption nf = (BoundListOption) Options.getInstance().getOption("nrformats");
final boolean alpha = nf.contains("alpha");
final boolean hex = nf.contains("hex");
final boolean octal = nf.contains("octal");
final TextRange range = SearchHelper.findNumberUnderCursor(editor, alpha, hex, octal);
if (range == null) {
logger.debug("no number on line");
return false;
} else {
String text = EditorHelper.getText(editor, range);
if (logger.isDebugEnabled()) {
logger.debug("found range " + range);
logger.debug("text=" + text);
}
String number = text;
if (text.length() == 0) {
return false;
}
char ch = text.charAt(0);
if (hex && text.toLowerCase().startsWith("0x")) {
for (int i = text.length() - 1; i >= 2; i--) {
int index = "abcdefABCDEF".indexOf(text.charAt(i));
if (index >= 0) {
lastLower = index < 6;
break;
}
}
int num = (int) Long.parseLong(text.substring(2), 16);
num += count;
number = Integer.toHexString(num);
number = StringHelper.rightJustify(number, text.length() - 2, '0');
if (!lastLower) {
number = number.toUpperCase();
}
number = text.substring(0, 2) + number;
} else if (octal && text.startsWith("0") && text.length() > 1) {
int num = (int) Long.parseLong(text, 8);
num += count;
number = Integer.toOctalString(num);
number = "0" + StringHelper.rightJustify(number, text.length() - 1, '0');
} else if (alpha && Character.isLetter(ch)) {
ch += count;
if (Character.isLetter(ch)) {
number = "" + ch;
}
} else if (ch == '-' || Character.isDigit(ch)) {
boolean pad = ch == '0';
int len = text.length();
if (ch == '-' && text.charAt(1) == '0') {
pad = true;
len--;
}
int num = Integer.parseInt(text);
num += count;
number = Integer.toString(num);
if (!octal && pad) {
boolean neg = false;
if (number.charAt(0) == '-') {
neg = true;
number = number.substring(1);
}
number = StringHelper.rightJustify(number, len, '0');
if (neg) {
number = "-" + number;
}
}
}
if (!text.equals(number)) {
replaceText(editor, range.getStartOffset(), range.getEndOffset(), number);
editor.getCaretModel().moveToOffset(range.getStartOffset() + number.length() - 1);
}
return true;
}
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class ChangeGroup method indentLines.
public void indentLines(@NotNull Editor editor, @NotNull DataContext context, int lines, int dir) {
int start = editor.getCaretModel().getOffset();
int end = VimPlugin.getMotion().moveCaretToLineEndOffset(editor, lines - 1, false);
indentRange(editor, context, new TextRange(start, end), 1, dir);
}
Aggregations