Search in sources :

Example 1 with BoundListOption

use of com.maddyhome.idea.vim.option.BoundListOption 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)

Aggregations

TextRange (com.maddyhome.idea.vim.common.TextRange)1 BoundListOption (com.maddyhome.idea.vim.option.BoundListOption)1