Search in sources :

Example 46 with TextRange

use of com.maddyhome.idea.vim.common.TextRange in project ideavim by JetBrains.

the class SearchGroup method findItOffset.

private int findItOffset(@NotNull Editor editor, int startOffset, int count, int dir, boolean noSmartCase) {
    boolean wrap = Options.getInstance().isSet("wrapscan");
    TextRange range = findIt(editor, startOffset, count, dir, noSmartCase, wrap, true, true);
    if (range == null) {
        return -1;
    }
    //highlightMatch(editor, range.getStartOffset(), range.getEndOffset());
    ParsePosition pp = new ParsePosition(0);
    int res = range.getStartOffset();
    if (lastOffset == null) {
        return -1;
    }
    if (lastOffset.length() == 0) {
        return range.getStartOffset();
    } else if (Character.isDigit(lastOffset.charAt(0)) || lastOffset.charAt(0) == '+' || lastOffset.charAt(0) == '-') {
        int lineOffset = 0;
        if (lastOffset.equals("+")) {
            lineOffset = 1;
        } else if (lastOffset.equals("-")) {
            lineOffset = -1;
        } else {
            if (lastOffset.charAt(0) == '+') {
                lastOffset = lastOffset.substring(1);
            }
            NumberFormat nf = NumberFormat.getIntegerInstance();
            pp = new ParsePosition(0);
            Number num = nf.parse(lastOffset, pp);
            if (num != null) {
                lineOffset = num.intValue();
            }
        }
        int line = editor.offsetToLogicalPosition(range.getStartOffset()).line;
        int newLine = EditorHelper.normalizeLine(editor, line + lineOffset);
        res = VimPlugin.getMotion().moveCaretToLineStart(editor, newLine);
    } else if ("ebs".indexOf(lastOffset.charAt(0)) != -1) {
        int charOffset = 0;
        if (lastOffset.length() >= 2) {
            if ("+-".indexOf(lastOffset.charAt(1)) != -1) {
                charOffset = 1;
            }
            NumberFormat nf = NumberFormat.getIntegerInstance();
            pp = new ParsePosition(lastOffset.charAt(1) == '+' ? 2 : 1);
            Number num = nf.parse(lastOffset, pp);
            if (num != null) {
                charOffset = num.intValue();
            }
        }
        int base = range.getStartOffset();
        if (lastOffset.charAt(0) == 'e') {
            base = range.getEndOffset() - 1;
        }
        res = Math.max(0, Math.min(base + charOffset, EditorHelper.getFileSize(editor) - 1));
    }
    int ppos = pp.getIndex();
    if (ppos < lastOffset.length() - 1 && lastOffset.charAt(ppos) == ';') {
        int flags;
        if (lastOffset.charAt(ppos + 1) == '/') {
            flags = Command.FLAG_SEARCH_FWD;
        } else if (lastOffset.charAt(ppos + 1) == '?') {
            flags = Command.FLAG_SEARCH_REV;
        } else {
            return res;
        }
        if (lastOffset.length() - ppos > 2) {
            ppos++;
        }
        res = search(editor, lastOffset.substring(ppos + 1), res, 1, flags);
        return res;
    } else {
        return res;
    }
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange) ParsePosition(java.text.ParsePosition) NumberFormat(java.text.NumberFormat)

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