Search in sources :

Example 1 with VimExtensionHandler

use of com.maddyhome.idea.vim.extension.VimExtensionHandler in project ideavim by JetBrains.

the class KeyHandler method handleKeyMapping.

private boolean handleKeyMapping(@NotNull final Editor editor, @NotNull final KeyStroke key, @NotNull final DataContext context) {
    final CommandState commandState = CommandState.getInstance(editor);
    commandState.stopMappingTimer();
    final List<KeyStroke> mappingKeys = commandState.getMappingKeys();
    final List<KeyStroke> fromKeys = new ArrayList<KeyStroke>(mappingKeys);
    fromKeys.add(key);
    final MappingMode mappingMode = commandState.getMappingMode();
    if (MappingMode.NVO.contains(mappingMode) && (state != State.NEW_COMMAND || currentArg != Argument.Type.NONE)) {
        return false;
    }
    final KeyMapping mapping = VimPlugin.getKey().getKeyMapping(mappingMode);
    final MappingInfo currentMappingInfo = mapping.get(fromKeys);
    final MappingInfo prevMappingInfo = mapping.get(mappingKeys);
    final MappingInfo mappingInfo = currentMappingInfo != null ? currentMappingInfo : prevMappingInfo;
    final Application application = ApplicationManager.getApplication();
    if (mapping.isPrefix(fromKeys)) {
        mappingKeys.add(key);
        if (!application.isUnitTestMode() && Options.getInstance().isSet(Options.TIMEOUT)) {
            commandState.startMappingTimer(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent actionEvent) {
                    mappingKeys.clear();
                    for (KeyStroke keyStroke : fromKeys) {
                        handleKey(editor, keyStroke, new EditorDataContext(editor), false);
                    }
                }
            });
        }
        return true;
    } else if (mappingInfo != null) {
        mappingKeys.clear();
        final Runnable handleMappedKeys = new Runnable() {

            @Override
            public void run() {
                if (editor.isDisposed()) {
                    return;
                }
                final List<KeyStroke> toKeys = mappingInfo.getToKeys();
                final VimExtensionHandler extensionHandler = mappingInfo.getExtensionHandler();
                final EditorDataContext currentContext = new EditorDataContext(editor);
                if (toKeys != null) {
                    final boolean fromIsPrefix = isPrefix(mappingInfo.getFromKeys(), toKeys);
                    boolean first = true;
                    for (KeyStroke keyStroke : toKeys) {
                        final boolean recursive = mappingInfo.isRecursive() && !(first && fromIsPrefix);
                        handleKey(editor, keyStroke, currentContext, recursive);
                        first = false;
                    }
                } else if (extensionHandler != null) {
                    RunnableHelper.runWriteCommand(editor.getProject(), new Runnable() {

                        @Override
                        public void run() {
                            extensionHandler.execute(editor, context);
                        }
                    }, "Vim " + extensionHandler.getClass().getSimpleName(), null);
                }
                if (prevMappingInfo != null) {
                    handleKey(editor, key, currentContext);
                }
            }
        };
        if (application.isUnitTestMode()) {
            handleMappedKeys.run();
        } else {
            application.invokeLater(handleMappedKeys);
        }
        return true;
    } else {
        final List<KeyStroke> unhandledKeys = new ArrayList<KeyStroke>(mappingKeys);
        mappingKeys.clear();
        for (KeyStroke keyStroke : unhandledKeys) {
            handleKey(editor, keyStroke, context, false);
        }
        return false;
    }
}
Also used : ActionEvent(java.awt.event.ActionEvent) AnActionEvent(com.intellij.openapi.actionSystem.AnActionEvent) VimExtensionHandler(com.maddyhome.idea.vim.extension.VimExtensionHandler) ArrayList(java.util.ArrayList) MappingMode(com.maddyhome.idea.vim.command.MappingMode) ActionListener(java.awt.event.ActionListener) CommandState(com.maddyhome.idea.vim.command.CommandState) ArrayList(java.util.ArrayList) List(java.util.List) Application(com.intellij.openapi.application.Application)

Example 2 with VimExtensionHandler

use of com.maddyhome.idea.vim.extension.VimExtensionHandler in project ideavim by JetBrains.

the class KeyGroup method showKeyMappings.

public boolean showKeyMappings(@NotNull Set<MappingMode> modes, @NotNull Editor editor) {
    final List<MappingInfo> rows = getKeyMappingRows(modes);
    final StringBuilder builder = new StringBuilder();
    for (MappingInfo row : rows) {
        builder.append(leftJustify(getModesStringCode(row.getMappingModes()), 2, ' '));
        builder.append(" ");
        builder.append(leftJustify(toKeyNotation(row.getFromKeys()), 11, ' '));
        builder.append(" ");
        builder.append(row.isRecursive() ? " " : "*");
        builder.append(" ");
        final List<KeyStroke> toKeys = row.getToKeys();
        final VimExtensionHandler extensionHandler = row.getExtensionHandler();
        if (toKeys != null) {
            builder.append(toKeyNotation(toKeys));
        } else if (extensionHandler != null) {
            builder.append("call ");
            builder.append(extensionHandler.getClass().getCanonicalName());
        } else {
            builder.append("<Unknown>");
        }
        builder.append("\n");
    }
    ExOutputModel.getInstance(editor).output(builder.toString());
    return true;
}
Also used : VimExtensionHandler(com.maddyhome.idea.vim.extension.VimExtensionHandler)

Aggregations

VimExtensionHandler (com.maddyhome.idea.vim.extension.VimExtensionHandler)2 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Application (com.intellij.openapi.application.Application)1 CommandState (com.maddyhome.idea.vim.command.CommandState)1 MappingMode (com.maddyhome.idea.vim.command.MappingMode)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1