Search in sources :

Example 21 with Keymap

use of com.intellij.openapi.keymap.Keymap in project intellij-community by JetBrains.

the class KeymapsTestCase method testIdsListIsConsistent.

@SuppressWarnings({ "HardCodedStringLiteral" })
public void testIdsListIsConsistent() {
    Map<String, Map<String, List<String>>> duplicates = getKnownDuplicates();
    Set<String> allMaps = new THashSet<>(ContainerUtil.map(KeymapManagerEx.getInstanceEx().getAllKeymaps(), keymap -> keymap.getName()));
    assertThat(ContainerUtil.subtract(allMaps, duplicates.keySet())).overridingErrorMessage("Modify 'known duplicates list' test data. Keymaps were added: %s", ContainerUtil.subtract(allMaps, duplicates.keySet())).isEmpty();
    assertThat(ContainerUtil.subtract(duplicates.keySet(), allMaps)).overridingErrorMessage("Modify 'known duplicates list' test data. Keymaps were removed: %s", ContainerUtil.subtract(duplicates.keySet(), allMaps)).isEmpty();
    @SuppressWarnings("MismatchedQueryAndUpdateOfCollection") Map<Keymap, List<Shortcut>> reassignedShortcuts = new FactoryMap<Keymap, List<Shortcut>>() {

        @Override
        protected Map<Keymap, List<Shortcut>> createMap() {
            return new LinkedHashMap<>();
        }

        @Nullable
        @Override
        protected List<Shortcut> create(Keymap key) {
            return new ArrayList<>();
        }
    };
    for (String name : duplicates.keySet()) {
        Keymap keymap = KeymapManagerEx.getInstanceEx().getKeymap(name);
        assertThat(keymap).overridingErrorMessage("KeyMap %s not found", name).isNotNull();
        Map<String, List<String>> duplicateIdsList = duplicates.get(name);
        Set<String> mentionedShortcuts = new THashSet<>();
        for (Map.Entry<String, List<String>> shortcutMappings : duplicateIdsList.entrySet()) {
            String shortcutString = shortcutMappings.getKey();
            if (!mentionedShortcuts.add(shortcutString)) {
                TestCase.fail("Shortcut '" + shortcutString + "' duplicate in keymap '" + keymap + "'. Please modify 'known duplicates list'");
            }
            Shortcut shortcut = parse(shortcutString);
            String[] ids = keymap.getActionIds(shortcut);
            Set<String> actualSc = new HashSet<>(Arrays.asList(ids));
            removeBoundActionIds(actualSc);
            Set<String> expectedSc = new HashSet<>(shortcutMappings.getValue());
            for (String s : actualSc) {
                if (!expectedSc.contains(s)) {
                    reassignedShortcuts.get(keymap).add(shortcut);
                }
            }
            for (String s : expectedSc) {
                if (!actualSc.contains(s)) {
                    System.out.println("Expected action '" + s + "' does not reassign shortcut " + getText(shortcut) + " in keymap " + keymap + " or is not registered");
                }
            }
        }
    }
    if (!reassignedShortcuts.isEmpty()) {
        StringBuilder message = new StringBuilder();
        for (Map.Entry<Keymap, List<Shortcut>> keymapToShortcuts : reassignedShortcuts.entrySet()) {
            Keymap keymap = keymapToShortcuts.getKey();
            message.append("The following shortcuts was reassigned in keymap ").append(keymap.getName()).append(". Please modify known duplicates list:\n");
            for (Shortcut eachShortcut : keymapToShortcuts.getValue()) {
                message.append(" { ").append(StringUtil.wrapWithDoubleQuote(getText(eachShortcut))).append(",\t").append(StringUtil.join(keymap.getActionIds(eachShortcut), s -> StringUtil.wrapWithDoubleQuote(s), ", ")).append("},\n");
            }
        }
        TestCase.fail("\n" + message.toString());
    }
}
Also used : InputEvent(java.awt.event.InputEvent) MacOSDefaultKeymap(com.intellij.openapi.keymap.impl.MacOSDefaultKeymap) java.util(java.util) Assertions.assertThat(com.intellij.testFramework.assertions.Assertions.assertThat) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) NonNls(org.jetbrains.annotations.NonNls) THashSet(gnu.trove.THashSet) Keymap(com.intellij.openapi.keymap.Keymap) ContainerUtil(com.intellij.util.containers.ContainerUtil) THashMap(gnu.trove.THashMap) ActionManager(com.intellij.openapi.actionSystem.ActionManager) ActionManagerEx(com.intellij.openapi.actionSystem.ex.ActionManagerEx) KeymapImpl(com.intellij.openapi.keymap.impl.KeymapImpl) TestCase(junit.framework.TestCase) KeymapManagerEx(com.intellij.openapi.keymap.ex.KeymapManagerEx) StringUtil(com.intellij.openapi.util.text.StringUtil) PlatformTestCase(com.intellij.testFramework.PlatformTestCase) FactoryMap(com.intellij.util.containers.FactoryMap) AnAction(com.intellij.openapi.actionSystem.AnAction) KeyEvent(java.awt.event.KeyEvent) KeymapUtil(com.intellij.openapi.keymap.KeymapUtil) KeyStrokeAdapter(com.intellij.ui.KeyStrokeAdapter) Nullable(org.jetbrains.annotations.Nullable) Shortcut(com.intellij.openapi.actionSystem.Shortcut) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) FactoryMap(com.intellij.util.containers.FactoryMap) THashSet(gnu.trove.THashSet) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) Shortcut(com.intellij.openapi.actionSystem.Shortcut) THashMap(gnu.trove.THashMap) FactoryMap(com.intellij.util.containers.FactoryMap) MacOSDefaultKeymap(com.intellij.openapi.keymap.impl.MacOSDefaultKeymap) Keymap(com.intellij.openapi.keymap.Keymap) THashSet(gnu.trove.THashSet)

Example 22 with Keymap

use of com.intellij.openapi.keymap.Keymap in project intellij-community by JetBrains.

the class AntTargetNodeDescriptor method addShortcutText.

public static boolean addShortcutText(String actionId, CompositeAppearance appearance) {
    Keymap activeKeymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut[] shortcuts = activeKeymap.getShortcuts(actionId);
    if (shortcuts != null && shortcuts.length > 0) {
        appearance.getEnding().addText(" (" + KeymapUtil.getShortcutText(shortcuts[0]) + ")", SimpleTextAttributes.GRAY_ATTRIBUTES);
        return true;
    } else
        return false;
}
Also used : Shortcut(com.intellij.openapi.actionSystem.Shortcut) Keymap(com.intellij.openapi.keymap.Keymap)

Example 23 with Keymap

use of com.intellij.openapi.keymap.Keymap in project android by JetBrains.

the class EditorFixture method invokeAction.

/** Invokes {@code editorAction} via its (first) keyboard shortcut in the active keymap. */
public EditorFixture invokeAction(@NotNull EditorAction editorAction) {
    AnAction anAction = ActionManager.getInstance().getAction(editorAction.id);
    assertTrue(editorAction.id + " is not enabled", anAction.getTemplatePresentation().isEnabled());
    Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    Shortcut shortcut = keymap.getShortcuts(editorAction.id)[0];
    if (shortcut instanceof KeyboardShortcut) {
        KeyboardShortcut cs = (KeyboardShortcut) shortcut;
        KeyStroke firstKeyStroke = cs.getFirstKeyStroke();
        Component component = getFocusedEditor();
        if (component != null) {
            ComponentDriver driver = new ComponentDriver(robot);
            driver.pressAndReleaseKey(component, firstKeyStroke.getKeyCode(), new int[] { firstKeyStroke.getModifiers() });
            KeyStroke secondKeyStroke = cs.getSecondKeyStroke();
            if (secondKeyStroke != null) {
                driver.pressAndReleaseKey(component, secondKeyStroke.getKeyCode(), new int[] { secondKeyStroke.getModifiers() });
            }
        } else {
            fail("Editor not focused for action");
        }
    } else {
        fail("Unsupported shortcut type " + shortcut.getClass().getName());
    }
    return this;
}
Also used : ComponentDriver(org.fest.swing.driver.ComponentDriver) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) Shortcut(com.intellij.openapi.actionSystem.Shortcut) KeyboardShortcut(com.intellij.openapi.actionSystem.KeyboardShortcut) ThemeEditorComponent(com.android.tools.idea.editors.theme.ThemeEditorComponent) AnAction(com.intellij.openapi.actionSystem.AnAction) Keymap(com.intellij.openapi.keymap.Keymap)

Example 24 with Keymap

use of com.intellij.openapi.keymap.Keymap in project intellij-community by JetBrains.

the class ConsoleViewImpl method registerActionHandler.

private static void registerActionHandler(@NotNull Editor editor, @NotNull String actionId, @NotNull AnAction action) {
    final Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    final Shortcut[] shortcuts = keymap.getShortcuts(actionId);
    action.registerCustomShortcutSet(new CustomShortcutSet(shortcuts), editor.getContentComponent());
}
Also used : Keymap(com.intellij.openapi.keymap.Keymap)

Example 25 with Keymap

use of com.intellij.openapi.keymap.Keymap in project intellij-community by JetBrains.

the class JavaVariableInplaceIntroducer method getAdvertisementText.

@Nullable
private static String getAdvertisementText(final PsiDeclarationStatement declaration, final PsiType type, final boolean hasTypeSuggestion) {
    final VariablesProcessor processor = ReassignVariableUtil.findVariablesOfType(declaration, type);
    final Keymap keymap = KeymapManager.getInstance().getActiveKeymap();
    if (processor.size() > 0) {
        final Shortcut[] shortcuts = keymap.getShortcuts("IntroduceVariable");
        if (shortcuts.length > 0) {
            return "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to reassign existing variable";
        }
    }
    if (hasTypeSuggestion) {
        final Shortcut[] shortcuts = keymap.getShortcuts("PreviousTemplateVariable");
        if (shortcuts.length > 0) {
            return "Press " + KeymapUtil.getShortcutText(shortcuts[0]) + " to change type";
        }
    }
    return null;
}
Also used : VariablesProcessor(com.intellij.psi.scope.processor.VariablesProcessor) Shortcut(com.intellij.openapi.actionSystem.Shortcut) Keymap(com.intellij.openapi.keymap.Keymap) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

Keymap (com.intellij.openapi.keymap.Keymap)47 Shortcut (com.intellij.openapi.actionSystem.Shortcut)14 KeymapManagerEx (com.intellij.openapi.keymap.ex.KeymapManagerEx)10 NotNull (org.jetbrains.annotations.NotNull)10 KeyboardShortcut (com.intellij.openapi.actionSystem.KeyboardShortcut)7 Nullable (org.jetbrains.annotations.Nullable)7 AnAction (com.intellij.openapi.actionSystem.AnAction)6 KeymapManager (com.intellij.openapi.keymap.KeymapManager)5 ActionManager (com.intellij.openapi.actionSystem.ActionManager)4 MacOSDefaultKeymap (com.intellij.openapi.keymap.impl.MacOSDefaultKeymap)4 Pair (com.intellij.openapi.util.Pair)4 FactoryMap (com.intellij.util.containers.FactoryMap)4 THashMap (gnu.trove.THashMap)4 THashSet (gnu.trove.THashSet)4 DefaultKeymap (com.intellij.openapi.keymap.impl.DefaultKeymap)2 KeymapImpl (com.intellij.openapi.keymap.impl.KeymapImpl)2 KeymapManagerImpl (com.intellij.openapi.keymap.impl.KeymapManagerImpl)2 ToolWindow (com.intellij.openapi.wm.ToolWindow)2 KeyEvent (java.awt.event.KeyEvent)2 NonNls (org.jetbrains.annotations.NonNls)2