How to replace default action by my own
I have my own action for optimizing imports, and I want to replace the default action by just installing the plugin, so it should remove keyboard shortcut of the default one. I do not want any manual configuration.
Is it possible and how?
Please sign in to leave a comment.
You can't do that all of the time - it's possible to tweak a writable keymap but all default keymap schemes are read-only.
Denis
Thank you, I found the way to do it dynamically. And it somehow works even with default scheme. :)
Keymap keyMap = getKeyMap();
Shortcut[] shortcuts = keyMap.getShortcuts("OptimizeImports");
for (Shortcut shortcut : shortcuts) {
keyMap.removeShortcut("OptimizeImports", shortcut);
keyMap.addShortcut("EclipseFormatterPluginOptimizeImportsAction", shortcut);
}
private Keymap getKeyMap() {
Keymap keymap = KeymapManagerImpl.getInstance().getActiveKeymap().getParent();
if (keymap == null) {
keymap = KeymapManagerImpl.getInstance().getActiveKeymap();
}
return keymap;
}