Search in sources :

Example 1 with MappingMode

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

the class KeyGroup method registerAction.

/**
   * @deprecated Inherit your action from {@link com.maddyhome.idea.vim.action.VimCommandAction} instead.
   */
@Deprecated
private void registerAction(@NotNull Set<MappingMode> mappingModes, @NotNull String actName, @NotNull Command.Type cmdType, int cmdFlags, @NotNull KeyStroke[] keys, @NotNull Argument.Type argType) {
    for (MappingMode mappingMode : mappingModes) {
        Node node = getKeyRoot(mappingMode);
        final int len = keys.length;
        // Add a child for each keystroke in the shortcut for this action
        for (int i = 0; i < len; i++) {
            if (node instanceof ParentNode) {
                final ParentNode base = (ParentNode) node;
                node = addNode(base, actName, cmdType, cmdFlags, keys[i], argType, i == len - 1);
            }
        }
    }
}
Also used : MappingMode(com.maddyhome.idea.vim.command.MappingMode)

Example 2 with MappingMode

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

the class KeyGroup method putKeyMapping.

public void putKeyMapping(@NotNull Set<MappingMode> modes, @NotNull List<KeyStroke> fromKeys, @Nullable List<KeyStroke> toKeys, @Nullable VimExtensionHandler extensionHandler, boolean recursive) {
    for (MappingMode mode : modes) {
        final KeyMapping mapping = getKeyMapping(mode);
        mapping.put(EnumSet.of(mode), fromKeys, toKeys, extensionHandler, recursive);
    }
    final int oldSize = requiredShortcutKeys.size();
    for (KeyStroke key : fromKeys) {
        if (key.getKeyChar() == KeyEvent.CHAR_UNDEFINED) {
            requiredShortcutKeys.add(key);
        }
    }
    if (requiredShortcutKeys.size() != oldSize) {
        for (Editor editor : EditorFactory.getInstance().getAllEditors()) {
            unregisterShortcutKeys(editor);
            registerRequiredShortcutKeys(editor);
        }
    }
}
Also used : MappingMode(com.maddyhome.idea.vim.command.MappingMode) Editor(com.intellij.openapi.editor.Editor)

Example 3 with MappingMode

use of com.maddyhome.idea.vim.command.MappingMode 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 4 with MappingMode

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

the class KeyGroup method getKeyMappingRows.

@NotNull
private static List<MappingInfo> getKeyMappingRows(@NotNull Set<MappingMode> modes) {
    final Map<ImmutableList<KeyStroke>, Set<MappingMode>> actualModes = new HashMap<ImmutableList<KeyStroke>, Set<MappingMode>>();
    for (MappingMode mode : modes) {
        final KeyMapping mapping = VimPlugin.getKey().getKeyMapping(mode);
        for (List<KeyStroke> fromKeys : mapping) {
            final ImmutableList<KeyStroke> key = ImmutableList.copyOf(fromKeys);
            final Set<MappingMode> value = actualModes.get(key);
            final Set<MappingMode> newValue;
            if (value != null) {
                newValue = new HashSet<MappingMode>(value);
                newValue.add(mode);
            } else {
                newValue = EnumSet.of(mode);
            }
            actualModes.put(key, newValue);
        }
    }
    final List<MappingInfo> rows = new ArrayList<MappingInfo>();
    for (Map.Entry<ImmutableList<KeyStroke>, Set<MappingMode>> entry : actualModes.entrySet()) {
        final ArrayList<KeyStroke> fromKeys = new ArrayList<KeyStroke>(entry.getKey());
        final Set<MappingMode> mappingModes = entry.getValue();
        if (!mappingModes.isEmpty()) {
            final MappingMode mode = mappingModes.iterator().next();
            final KeyMapping mapping = VimPlugin.getKey().getKeyMapping(mode);
            final MappingInfo mappingInfo = mapping.get(fromKeys);
            if (mappingInfo != null) {
                rows.add(new MappingInfo(mappingModes, mappingInfo.getFromKeys(), mappingInfo.getToKeys(), mappingInfo.getExtensionHandler(), mappingInfo.isRecursive()));
            }
        }
    }
    Collections.sort(rows);
    return rows;
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) MappingMode(com.maddyhome.idea.vim.command.MappingMode) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

MappingMode (com.maddyhome.idea.vim.command.MappingMode)4 ImmutableList (com.google.common.collect.ImmutableList)1 AnActionEvent (com.intellij.openapi.actionSystem.AnActionEvent)1 Application (com.intellij.openapi.application.Application)1 Editor (com.intellij.openapi.editor.Editor)1 CommandState (com.maddyhome.idea.vim.command.CommandState)1 VimExtensionHandler (com.maddyhome.idea.vim.extension.VimExtensionHandler)1 ActionEvent (java.awt.event.ActionEvent)1 ActionListener (java.awt.event.ActionListener)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 NotNull (org.jetbrains.annotations.NotNull)1