Exemple #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;
 }
Exemple #2
0
  public boolean executeFilter(@NotNull Editor editor, @NotNull TextRange range, String command)
      throws IOException {
    if (logger.isDebugEnabled()) logger.debug("command=" + command);
    CharSequence chars = editor.getDocument().getCharsSequence();
    StringReader car =
        new StringReader(
            chars.subSequence(range.getStartOffset(), range.getEndOffset()).toString());
    StringWriter sw = new StringWriter();

    logger.debug("about to create filter");
    Process filter = Runtime.getRuntime().exec(command);
    logger.debug("filter created");
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(filter.getOutputStream()));
    logger.debug("sending text");
    copy(car, writer);
    writer.close();
    logger.debug("sent");

    BufferedReader reader = new BufferedReader(new InputStreamReader(filter.getInputStream()));
    logger.debug("getting result");
    copy(reader, sw);
    sw.close();
    logger.debug("received");

    editor.getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), sw.toString());

    lastCommand = command;

    return true;
  }
Exemple #3
0
  public int searchWord(@NotNull Editor editor, int count, boolean whole, int dir) {
    TextRange range = SearchHelper.findWordUnderCursor(editor);
    if (range == null) {
      return -1;
    }

    StringBuilder pattern = new StringBuilder();
    if (whole) {
      pattern.append("\\<");
    }
    pattern.append(EditorHelper.getText(editor, range.getStartOffset(), range.getEndOffset()));
    if (whole) {
      pattern.append("\\>");
    }

    MotionGroup.moveCaret(editor, range.getStartOffset());

    lastSearch = pattern.toString();
    setLastPattern(editor, lastSearch);
    lastOffset = "";
    lastDir = dir;

    searchHighlight(true);

    return findItOffset(editor, editor.getCaretModel().getOffset(), count, lastDir, true);
  }
  public boolean execute(Editor editor, DataContext context, ExCommand cmd) throws ExException {
    TextRange range = cmd.getTextRange(editor, context, false);

    ParseResult pr = CommandParser.getInstance().parse(cmd.getArgument());
    int line = pr.getRanges().getFirstLine(editor, context);
    int offset = CommandGroups.getInstance().getMotion().moveCaretToLineStart(editor, line + 1);

    String text = EditorHelper.getText(editor, range.getStartOffset(), range.getEndOffset());
    CommandGroups.getInstance()
        .getCopy()
        .putText(editor, context, offset, text, Command.FLAG_MOT_LINEWISE, 1, true, false, 0);

    return true;
  }
Exemple #5
0
 private static int distance(@NotNull TextRange range, int pos, boolean forwards, int size) {
   final int start = range.getStartOffset();
   if (start <= pos) {
     return forwards ? size - pos + start : pos - start;
   } else {
     return forwards ? start - pos : pos + size - start;
   }
 }
Exemple #6
0
  private static void highlightSearchLines(
      @NotNull Editor editor,
      @NotNull String pattern,
      int startLine,
      int endLine,
      boolean ignoreCase) {
    final TextAttributes color =
        editor.getColorsScheme().getAttributes(EditorColors.SEARCH_RESULT_ATTRIBUTES);
    Collection<RangeHighlighter> highlighters = EditorData.getLastHighlights(editor);
    if (highlighters == null) {
      highlighters = new ArrayList<RangeHighlighter>();
      EditorData.setLastHighlights(editor, highlighters);
    }

    for (TextRange range : findAll(editor, pattern, startLine, endLine, ignoreCase)) {
      final RangeHighlighter highlighter =
          highlightMatch(editor, range.getStartOffset(), range.getEndOffset());
      highlighter.setErrorStripeMarkColor(color.getBackgroundColor());
      highlighter.setErrorStripeTooltip(pattern);
      highlighters.add(highlighter);
    }
  }
Exemple #7
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;
    }
  }