Example #1
0
 @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;
 }
Example #2
0
  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;
    }
  }