Search in sources :

Example 1 with Argument

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

the class KeyHandler method handleCommandNode.

private void handleCommandNode(@NotNull Editor editor, @NotNull DataContext context, @NotNull CommandNode node) {
    // If all does well we are ready to process this command
    state = State.READY;
    // Did we just get the completed sequence for a motion command argument?
    if (currentArg == Argument.Type.MOTION) {
        // We have been expecting a motion argument - is this one?
        if (node.getCmdType() == Command.Type.MOTION) {
            // Create the motion command and add it to the stack
            Command cmd = new Command(count, node.getActionId(), node.getAction(), node.getCmdType(), node.getFlags());
            cmd.setKeys(keys);
            currentCmd.push(cmd);
        } else if (node.getCmdType() == Command.Type.RESET) {
            currentCmd.clear();
            Command cmd = new Command(1, node.getActionId(), node.getAction(), node.getCmdType(), node.getFlags());
            cmd.setKeys(keys);
            currentCmd.push(cmd);
        } else {
            // Oops - this wasn't a motion command. The user goofed and typed something else
            state = State.BAD_COMMAND;
        }
    } else if (currentArg == Argument.Type.EX_STRING && (node.getFlags() & Command.FLAG_COMPLETE_EX) != 0) {
        String text = VimPlugin.getProcess().endSearchCommand(editor, context);
        Argument arg = new Argument(text);
        Command cmd = currentCmd.peek();
        cmd.setArgument(arg);
        CommandState.getInstance(editor).popState();
    } else // The user entered a valid command that doesn't take any arguments
    {
        // Create the command and add it to the stack
        Command cmd = new Command(count, node.getActionId(), node.getAction(), node.getCmdType(), node.getFlags());
        cmd.setKeys(keys);
        currentCmd.push(cmd);
        // programmer made a typo or forgot to add the action to the plugin.xml file
        if (cmd.getAction() == null) {
            state = State.ERROR;
        }
    }
}
Also used : Argument(com.maddyhome.idea.vim.command.Argument) Command(com.maddyhome.idea.vim.command.Command)

Example 2 with Argument

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

the class KeyHandler method executeCommand.

private void executeCommand(@NotNull Editor editor, @NotNull KeyStroke key, @NotNull DataContext context, @NotNull CommandState editorState) {
    // Let's go through the command stack and merge it all into one command. At this time there should never
    // be more than two commands on the stack - one is the actual command and the other would be a motion
    // command argument needed by the first command
    Command cmd = currentCmd.pop();
    while (currentCmd.size() > 0) {
        Command top = currentCmd.pop();
        top.setArgument(new Argument(cmd));
        cmd = top;
    }
    // If we have a command and a motion command argument, both could possibly have their own counts. We
    // need to adjust the counts so the motion gets the product of both counts and the count associated with
    // the command gets reset. Example 3c2w (change 2 words, three times) becomes c6w (change 6 words)
    final Argument arg = cmd.getArgument();
    if (arg != null && arg.getType() == Argument.Type.MOTION) {
        final Command mot = arg.getMotion();
        // the motion gets the product of both.
        if (mot != null) {
            int cnt = cmd.getRawCount() == 0 && mot.getRawCount() == 0 ? 0 : cmd.getCount() * mot.getCount();
            mot.setCount(cnt);
        }
        cmd.setCount(0);
    }
    // If we were in "operator pending" mode, reset back to normal mode.
    if (editorState.getMappingMode() == MappingMode.OP_PENDING) {
        editorState.popState();
    }
    // Save off the command we are about to execute
    editorState.setCommand(cmd);
    lastWasBS = ((cmd.getFlags() & Command.FLAG_IS_BACKSPACE) != 0);
    Project project = editor.getProject();
    if (cmd.getType().isRead() || project == null || EditorHelper.canEdit(project, editor)) {
        if (ApplicationManager.getApplication().isDispatchThread()) {
            Runnable action = new ActionRunner(editor, context, cmd, key);
            String name = cmd.getAction().getTemplatePresentation().getText();
            name = name != null ? "Vim " + name : "";
            if (cmd.getType().isWrite()) {
                RunnableHelper.runWriteCommand(project, action, name, action);
            } else {
                RunnableHelper.runReadCommand(project, action, name, action);
            }
        }
    } else {
        VimPlugin.indicateError();
        reset(editor);
    }
}
Also used : Project(com.intellij.openapi.project.Project) Argument(com.maddyhome.idea.vim.command.Argument) Command(com.maddyhome.idea.vim.command.Command)

Example 3 with Argument

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

the class KeyHandler method handleCharArgument.

private void handleCharArgument(@NotNull KeyStroke key, char chKey) {
    // Some special keys can be handled as character arguments - let's check for them here.
    if (chKey == 0) {
        switch(key.getKeyCode()) {
            case KeyEvent.VK_TAB:
                chKey = '\t';
                break;
            case KeyEvent.VK_ENTER:
                chKey = '\n';
                break;
        }
    }
    if (chKey != 0) {
        // Create the character argument, add it to the current command, and signal we are ready to process
        // the command
        Argument arg = new Argument(chKey);
        Command cmd = currentCmd.peek();
        cmd.setArgument(arg);
        state = State.READY;
    } else {
        // Oops - this isn't a valid character argument
        state = State.BAD_COMMAND;
    }
}
Also used : Argument(com.maddyhome.idea.vim.command.Argument) Command(com.maddyhome.idea.vim.command.Command)

Aggregations

Argument (com.maddyhome.idea.vim.command.Argument)3 Command (com.maddyhome.idea.vim.command.Command)3 Project (com.intellij.openapi.project.Project)1