use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class TextObjectActionHandler method execute.
protected final boolean execute(@NotNull Editor editor, @NotNull DataContext context, @NotNull Command cmd) {
if (CommandState.getInstance(editor).getMode() == CommandState.Mode.VISUAL) {
TextRange range = getRange(editor, context, cmd.getCount(), cmd.getRawCount(), cmd.getArgument());
if (range == null) {
return false;
}
TextRange vr = VimPlugin.getMotion().getRawVisualRange();
boolean block = (cmd.getFlags() & Command.FLAG_TEXT_BLOCK) != 0;
int newstart = block || vr.getEndOffset() >= vr.getStartOffset() ? range.getStartOffset() : range.getEndOffset();
int newend = block || vr.getEndOffset() >= vr.getStartOffset() ? range.getEndOffset() : range.getStartOffset();
if (vr.getStartOffset() == vr.getEndOffset() || block) {
VimPlugin.getMotion().moveVisualStart(newstart);
}
if (((cmd.getFlags() & Command.FLAG_MOT_LINEWISE) != 0 && (cmd.getFlags() & Command.FLAG_VISUAL_CHARACTERWISE) == 0) && CommandState.getInstance(editor).getSubMode() != CommandState.SubMode.VISUAL_LINE) {
VimPlugin.getMotion().toggleVisual(editor, 1, 0, CommandState.SubMode.VISUAL_LINE);
} else if (((cmd.getFlags() & Command.FLAG_MOT_LINEWISE) == 0 || (cmd.getFlags() & Command.FLAG_VISUAL_CHARACTERWISE) != 0) && CommandState.getInstance(editor).getSubMode() == CommandState.SubMode.VISUAL_LINE) {
VimPlugin.getMotion().toggleVisual(editor, 1, 0, CommandState.SubMode.VISUAL_CHARACTER);
}
MotionGroup.moveCaret(editor, newend);
}
return true;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchHelper method findBlockTagRange.
@Nullable
public static TextRange findBlockTagRange(@NotNull Editor editor, boolean isOuter) {
final int cursorOffset = editor.getCaretModel().getOffset();
int pos = cursorOffset;
final CharSequence sequence = editor.getDocument().getCharsSequence();
while (true) {
final Pair<TextRange, String> closingTagResult = findClosingTag(sequence, pos);
if (closingTagResult == null) {
return null;
}
final TextRange closingTagTextRange = closingTagResult.getFirst();
final String tagName = closingTagResult.getSecond();
final TextRange openingTagTextRange = findOpeningTag(sequence, closingTagTextRange.getStartOffset(), tagName);
if (openingTagTextRange != null && openingTagTextRange.getStartOffset() <= cursorOffset) {
if (isOuter) {
return new TextRange(openingTagTextRange.getStartOffset(), closingTagTextRange.getEndOffset());
} else {
return new TextRange(openingTagTextRange.getEndOffset() + 1, closingTagTextRange.getStartOffset() - 1);
}
} else {
pos = closingTagTextRange.getEndOffset() + 1;
}
}
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchHelper method findOpeningTag.
@Nullable
private static TextRange findOpeningTag(@NotNull CharSequence sequence, int position, @NotNull String tagName) {
final String tagBeginning = "<" + tagName;
final Pattern pattern = Pattern.compile(Pattern.quote(tagBeginning), Pattern.CASE_INSENSITIVE);
final Matcher matcher = pattern.matcher(sequence.subSequence(0, position));
final List<Integer> possibleBeginnings = Lists.newArrayList();
while (matcher.find()) {
possibleBeginnings.add(matcher.start());
}
final List<Integer> reversedBeginnings = Lists.reverse(possibleBeginnings);
for (int openingTagPos : reversedBeginnings) {
final int openingTagEndPos = openingTagPos + tagBeginning.length();
final int closeBracketPos = StringUtil.indexOf(sequence, '>', openingTagEndPos);
if (closeBracketPos > 0 && (closeBracketPos == openingTagEndPos || sequence.charAt(openingTagEndPos) == ' ')) {
return new TextRange(openingTagPos, closeBracketPos);
}
}
return null;
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchGroup method setLastPattern.
private void setLastPattern(@NotNull Editor editor, @NotNull String lastPattern) {
this.lastPattern = lastPattern;
VimPlugin.getRegister().storeTextInternal(editor, new TextRange(-1, -1), lastPattern, SelectionType.CHARACTER_WISE, '/', false);
VimPlugin.getHistory().addEntry(HistoryGroup.SEARCH, lastPattern);
}
use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.
the class SearchGroup method findAll.
@NotNull
private static List<TextRange> findAll(@NotNull Editor editor, @NotNull String pattern, int startLine, int endLine, boolean ignoreCase) {
final List<TextRange> results = Lists.newArrayList();
final int lineCount = EditorHelper.getLineCount(editor);
final int actualEndLine = endLine == -1 ? lineCount : endLine;
final RegExp.regmmatch_T regMatch = new RegExp.regmmatch_T();
final RegExp regExp = new RegExp();
regMatch.regprog = regExp.vim_regcomp(pattern, 1);
if (regMatch.regprog == null) {
return results;
}
regMatch.rmm_ic = ignoreCase;
int col = 0;
for (int line = startLine; line <= actualEndLine; ) {
int matchedLines = regExp.vim_regexec_multi(regMatch, editor, lineCount, line, col);
if (matchedLines > 0) {
final CharacterPosition startPos = new CharacterPosition(line + regMatch.startpos[0].lnum, regMatch.startpos[0].col);
final CharacterPosition endPos = new CharacterPosition(line + regMatch.endpos[0].lnum, regMatch.endpos[0].col);
int start = EditorHelper.characterPositionToOffset(editor, startPos);
int end = EditorHelper.characterPositionToOffset(editor, endPos);
results.add(new TextRange(start, end));
if (start != end) {
line += matchedLines - 1;
col = endPos.column;
} else {
line += matchedLines;
col = 0;
}
} else {
line++;
col = 0;
}
}
return results;
}
Aggregations