Search in sources :

Example 11 with TextRange

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

the class SearchGroup method findNext.

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

Example 12 with TextRange

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

the class ChangeGroup method deleteJoinNLines.

/**
   * This does the actual joining of the lines
   *
   * @param editor    The editor to join the lines in
   * @param startLine The starting logical line
   * @param count     The number of lines to join including startLine
   * @param spaces    If true the joined lines will have one space between them and any leading space on the second line
   *                  will be removed. If false, only the newline is removed to join the lines.
   * @return true if able to join the lines, false if not
   */
private boolean deleteJoinNLines(@NotNull Editor editor, int startLine, int count, boolean spaces) {
    // start my moving the cursor to the very end of the first line
    MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretToLineEnd(editor, startLine, true));
    for (int i = 1; i < count; i++) {
        int start = VimPlugin.getMotion().moveCaretToLineEnd(editor);
        int trailingWhitespaceStart = VimPlugin.getMotion().moveCaretToLineEndSkipLeading(editor);
        boolean hasTrailingWhitespace = start != trailingWhitespaceStart + 1;
        MotionGroup.moveCaret(editor, start);
        int offset;
        if (spaces) {
            offset = VimPlugin.getMotion().moveCaretToLineStartSkipLeadingOffset(editor, 1);
        } else {
            offset = VimPlugin.getMotion().moveCaretToLineStartOffset(editor);
        }
        deleteText(editor, new TextRange(editor.getCaretModel().getOffset(), offset), null);
        if (spaces && !hasTrailingWhitespace) {
            insertText(editor, start, " ");
            MotionGroup.moveCaret(editor, VimPlugin.getMotion().moveCaretHorizontal(editor, -1, false));
        }
    }
    return true;
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 13 with TextRange

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

the class ChangeGroup method deleteMotion.

/**
   * Delete all text moved over by the supplied motion command argument.
   *
   * @param editor   The editor to delete the text from
   * @param context  The data context
   * @param count    The number of times to repeat the deletion
   * @param rawCount The actual count entered by the user
   * @param argument The motion command
   * @param isChange if from a change
   * @return true if able to delete the text, false if not
   */
public boolean deleteMotion(@NotNull Editor editor, DataContext context, int count, int rawCount, @NotNull final Argument argument, boolean isChange) {
    final TextRange range = getDeleteMotionRange(editor, context, count, rawCount, argument);
    if (range == null) {
        return (EditorHelper.getFileSize(editor) == 0);
    }
    // Delete motion commands that are not linewise become linewise if all the following are true:
    // 1) The range is across multiple lines
    // 2) There is only whitespace before the start of the range
    // 3) There is only whitespace after the end of the range
    final Command motion = argument.getMotion();
    if (motion == null) {
        return false;
    }
    if (!isChange && (motion.getFlags() & Command.FLAG_MOT_LINEWISE) == 0) {
        LogicalPosition start = editor.offsetToLogicalPosition(range.getStartOffset());
        LogicalPosition end = editor.offsetToLogicalPosition(range.getEndOffset());
        if (start.line != end.line) {
            if (!SearchHelper.anyNonWhitespace(editor, range.getStartOffset(), -1) && !SearchHelper.anyNonWhitespace(editor, range.getEndOffset(), 1)) {
                int flags = motion.getFlags();
                flags &= ~Command.FLAG_MOT_EXCLUSIVE;
                flags &= ~Command.FLAG_MOT_INCLUSIVE;
                flags |= Command.FLAG_MOT_LINEWISE;
                motion.setFlags(flags);
            }
        }
    }
    return deleteRange(editor, range, SelectionType.fromCommandFlags(motion.getFlags()), isChange);
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 14 with TextRange

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

the class ChangeGroup method changeNumber.

public boolean changeNumber(@NotNull final Editor editor, final int count) {
    final BoundListOption nf = (BoundListOption) Options.getInstance().getOption("nrformats");
    final boolean alpha = nf.contains("alpha");
    final boolean hex = nf.contains("hex");
    final boolean octal = nf.contains("octal");
    final TextRange range = SearchHelper.findNumberUnderCursor(editor, alpha, hex, octal);
    if (range == null) {
        logger.debug("no number on line");
        return false;
    } else {
        String text = EditorHelper.getText(editor, range);
        if (logger.isDebugEnabled()) {
            logger.debug("found range " + range);
            logger.debug("text=" + text);
        }
        String number = text;
        if (text.length() == 0) {
            return false;
        }
        char ch = text.charAt(0);
        if (hex && text.toLowerCase().startsWith("0x")) {
            for (int i = text.length() - 1; i >= 2; i--) {
                int index = "abcdefABCDEF".indexOf(text.charAt(i));
                if (index >= 0) {
                    lastLower = index < 6;
                    break;
                }
            }
            int num = (int) Long.parseLong(text.substring(2), 16);
            num += count;
            number = Integer.toHexString(num);
            number = StringHelper.rightJustify(number, text.length() - 2, '0');
            if (!lastLower) {
                number = number.toUpperCase();
            }
            number = text.substring(0, 2) + number;
        } else if (octal && text.startsWith("0") && text.length() > 1) {
            int num = (int) Long.parseLong(text, 8);
            num += count;
            number = Integer.toOctalString(num);
            number = "0" + StringHelper.rightJustify(number, text.length() - 1, '0');
        } else if (alpha && Character.isLetter(ch)) {
            ch += count;
            if (Character.isLetter(ch)) {
                number = "" + ch;
            }
        } else if (ch == '-' || Character.isDigit(ch)) {
            boolean pad = ch == '0';
            int len = text.length();
            if (ch == '-' && text.charAt(1) == '0') {
                pad = true;
                len--;
            }
            int num = Integer.parseInt(text);
            num += count;
            number = Integer.toString(num);
            if (!octal && pad) {
                boolean neg = false;
                if (number.charAt(0) == '-') {
                    neg = true;
                    number = number.substring(1);
                }
                number = StringHelper.rightJustify(number, len, '0');
                if (neg) {
                    number = "-" + number;
                }
            }
        }
        if (!text.equals(number)) {
            replaceText(editor, range.getStartOffset(), range.getEndOffset(), number);
            editor.getCaretModel().moveToOffset(range.getStartOffset() + number.length() - 1);
        }
        return true;
    }
}
Also used : BoundListOption(com.maddyhome.idea.vim.option.BoundListOption) TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 15 with TextRange

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

the class ChangeGroup method indentLines.

public void indentLines(@NotNull Editor editor, @NotNull DataContext context, int lines, int dir) {
    int start = editor.getCaretModel().getOffset();
    int end = VimPlugin.getMotion().moveCaretToLineEndOffset(editor, lines - 1, false);
    indentRange(editor, context, new TextRange(start, end), 1, dir);
}
Also used : TextRange(com.maddyhome.idea.vim.common.TextRange)

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