Search in sources :

Example 1 with SelectionType

use of com.maddyhome.idea.vim.command.SelectionType in project ideavim by JetBrains.

the class CopyGroup method putVisualRange.

public boolean putVisualRange(@NotNull Editor editor, @NotNull DataContext context, @NotNull TextRange range, int count, boolean indent, boolean cursorAfter) {
    CommandState.SubMode subMode = CommandState.getInstance(editor).getSubMode();
    Register reg = VimPlugin.getRegister().getLastRegister();
    // Without this reset, the deleted text goes into the same register we just pasted from.
    VimPlugin.getRegister().resetRegister();
    if (reg != null) {
        final SelectionType type = reg.getType();
        if (type == SelectionType.LINE_WISE && editor.isOneLineMode()) {
            return false;
        }
        int start = range.getStartOffset();
        int end = range.getEndOffset();
        int endLine = editor.offsetToLogicalPosition(end).line;
        if (logger.isDebugEnabled()) {
            logger.debug("start=" + start);
            logger.debug("end=" + end);
        }
        if (subMode == CommandState.SubMode.VISUAL_LINE) {
            range = new TextRange(range.getStartOffset(), Math.min(range.getEndOffset() + 1, EditorHelper.getFileSize(editor)));
        }
        VimPlugin.getChange().deleteRange(editor, range, SelectionType.fromSubMode(subMode), false);
        editor.getCaretModel().moveToOffset(start);
        int pos = start;
        if (type == SelectionType.LINE_WISE) {
            if (subMode == CommandState.SubMode.VISUAL_BLOCK) {
                pos = editor.getDocument().getLineEndOffset(endLine) + 1;
            } else if (subMode != CommandState.SubMode.VISUAL_LINE) {
                editor.getDocument().insertString(start, "\n");
                pos = start + 1;
            }
        } else if (type != SelectionType.CHARACTER_WISE) {
            if (subMode == CommandState.SubMode.VISUAL_LINE) {
                editor.getDocument().insertString(start, "\n");
            }
        }
        putText(editor, context, pos, StringUtil.notNullize(reg.getText()), type, count, indent && type == SelectionType.LINE_WISE, cursorAfter, subMode);
        return true;
    }
    return false;
}
Also used : Register(com.maddyhome.idea.vim.common.Register) SelectionType(com.maddyhome.idea.vim.command.SelectionType) CommandState(com.maddyhome.idea.vim.command.CommandState) TextRange(com.maddyhome.idea.vim.common.TextRange)

Example 2 with SelectionType

use of com.maddyhome.idea.vim.command.SelectionType in project ideavim by JetBrains.

the class RegisterGroup method readData.

public void readData(@NotNull final Element element) {
    logger.debug("readData");
    final Element registersElement = element.getChild("registers");
    if (registersElement != null) {
        //noinspection unchecked
        final List<Element> registerElements = registersElement.getChildren("register");
        for (Element registerElement : registerElements) {
            final char key = registerElement.getAttributeValue("name").charAt(0);
            final Register register;
            final Element textElement = registerElement.getChild("text");
            final String typeText = registerElement.getAttributeValue("type");
            final SelectionType type = SelectionType.fromValue(Integer.parseInt(typeText));
            if (textElement != null) {
                final String text = StringHelper.getSafeXmlText(textElement);
                if (text != null) {
                    register = new Register(key, type, text);
                } else {
                    register = null;
                }
            } else {
                final Element keysElement = registerElement.getChild("keys");
                //noinspection unchecked
                final List<Element> keyElements = keysElement.getChildren("key");
                final List<KeyStroke> strokes = new ArrayList<KeyStroke>();
                for (Element keyElement : keyElements) {
                    final int code = Integer.parseInt(keyElement.getAttributeValue("code"));
                    final int modifiers = Integer.parseInt(keyElement.getAttributeValue("mods"));
                    final char c = (char) Integer.parseInt(keyElement.getAttributeValue("char"));
                    //noinspection MagicConstant
                    strokes.add(c == KeyEvent.CHAR_UNDEFINED ? KeyStroke.getKeyStroke(code, modifiers) : KeyStroke.getKeyStroke(c));
                }
                register = new Register(key, type, strokes);
            }
            registers.put(key, register);
        }
    }
}
Also used : Register(com.maddyhome.idea.vim.common.Register) SelectionType(com.maddyhome.idea.vim.command.SelectionType) Element(org.jdom.Element)

Aggregations

SelectionType (com.maddyhome.idea.vim.command.SelectionType)2 Register (com.maddyhome.idea.vim.common.Register)2 CommandState (com.maddyhome.idea.vim.command.CommandState)1 TextRange (com.maddyhome.idea.vim.common.TextRange)1 Element (org.jdom.Element)1