Search in sources :

Example 41 with TextRange

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;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 42 with TextRange

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;
        }
    }
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 43 with TextRange

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;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) TextRange(com.maddyhome.idea.vim.common.TextRange) Nullable(org.jetbrains.annotations.Nullable)

Example 44 with TextRange

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);
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 45 with TextRange

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;
}
Also used : RegExp(com.maddyhome.idea.vim.regexp.RegExp) CharacterPosition(com.maddyhome.idea.vim.common.CharacterPosition) TextRange(com.maddyhome.idea.vim.common.TextRange) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

TextRange (com.maddyhome.idea.vim.common.TextRange)46 Nullable (org.jetbrains.annotations.Nullable)8 LogicalPosition (com.intellij.openapi.editor.LogicalPosition)2 CharacterPosition (com.maddyhome.idea.vim.common.CharacterPosition)2 Register (com.maddyhome.idea.vim.common.Register)2 RegExp (com.maddyhome.idea.vim.regexp.RegExp)2 Document (com.intellij.openapi.editor.Document)1 RangeHighlighter (com.intellij.openapi.editor.markup.RangeHighlighter)1 TextAttributes (com.intellij.openapi.editor.markup.TextAttributes)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 CodeStyleSettings (com.intellij.psi.codeStyle.CodeStyleSettings)1 MotionEditorAction (com.maddyhome.idea.vim.action.motion.MotionEditorAction)1 TextObjectAction (com.maddyhome.idea.vim.action.motion.TextObjectAction)1 Command (com.maddyhome.idea.vim.command.Command)1 CommandState (com.maddyhome.idea.vim.command.CommandState)1 SelectionType (com.maddyhome.idea.vim.command.SelectionType)1 Mark (com.maddyhome.idea.vim.common.Mark)1 SearchHelper (com.maddyhome.idea.vim.helper.SearchHelper)1