Search in sources :

Example 1 with BindKeysHelper

use of org.python.pydev.shared_ui.bindings.BindKeysHelper in project Pydev by fabioz.

the class InteractiveConsoleCommand method syncCommands.

/**
 * Makes sure that the commands are always updated (to be called in the UI-thread). Not thread safe.
 */
private static void syncCommands() {
    IWorkbench workbench;
    try {
        workbench = PlatformUI.getWorkbench();
    } catch (Throwable e) {
        // Log.log(e); -- don't even log (if we're in a state we can't use it, there's no point in doing anything).
        return;
    }
    ICommandService commandService = workbench.getService(ICommandService.class);
    // Note: hardcoding that we want to deal with the PyDev editor category.
    Category pydevCommandsCategory = commandService.getCategory("org.python.pydev.ui.category.source");
    if (!pydevCommandsCategory.isDefined()) {
        Log.log("Expecting org.python.pydev.ui.category.source to be a a defined commands category.");
        return;
    }
    // First we have to remove bindings which would conflict.
    final Set<KeySequence> removeKeySequences = new HashSet<>();
    List<InteractiveConsoleCommand> existingCommands = InteractiveConsoleCommand.loadExistingCommands();
    for (InteractiveConsoleCommand interactiveConsoleCommand : existingCommands) {
        try {
            removeKeySequences.add(KeyBindingHelper.getKeySequence(interactiveConsoleCommand.keybinding));
        } catch (Exception e) {
            Log.log("Error resolving: " + interactiveConsoleCommand.keybinding, e);
        }
    }
    BindKeysHelper bindKeysHelper = new BindKeysHelper(PyEdit.PYDEV_EDITOR_KEYBINDINGS_CONTEXT_ID);
    bindKeysHelper.removeUserBindingsWithFilter(new IFilter() {

        @Override
        public boolean removeBinding(Binding binding) {
            TriggerSequence triggerSequence = binding.getTriggerSequence();
            if (removeKeySequences.contains(triggerSequence)) {
                return true;
            }
            ParameterizedCommand command = binding.getParameterizedCommand();
            if (command == null) {
                return false;
            }
            String id = command.getId();
            if (id.startsWith(USER_COMMAND_PREFIX)) {
                return true;
            }
            return false;
        }
    });
    Map<String, InteractiveCommandCustomHandler> commandIdToHandler = new HashMap<>();
    // Now, define the commands and the bindings for the user-commands.
    int i = 0;
    for (InteractiveConsoleCommand interactiveConsoleCommand : existingCommands) {
        try {
            String commandId = USER_COMMAND_PREFIX + i;
            Command cmd = commandService.getCommand(commandId);
            if (!cmd.isDefined()) {
                cmd.define(interactiveConsoleCommand.name, interactiveConsoleCommand.name, pydevCommandsCategory);
            }
            InteractiveCommandCustomHandler handler = createHandler(interactiveConsoleCommand);
            commandIdToHandler.put(commandId, handler);
            cmd.setHandler(handler);
            KeySequence keySequence;
            try {
                if (interactiveConsoleCommand.keybinding == null || interactiveConsoleCommand.keybinding.length() == 0) {
                    continue;
                }
                keySequence = KeyBindingHelper.getKeySequence(interactiveConsoleCommand.keybinding);
            } catch (IllegalArgumentException | ParseException e) {
                Log.log("Error resolving: " + interactiveConsoleCommand.keybinding, e);
                continue;
            }
            bindKeysHelper.addUserBindings(keySequence, new ParameterizedCommand(cmd, null));
        } catch (Exception e) {
            Log.log(e);
        }
        i++;
    }
    // Unbind any command we may have previously created.
    for (; i < 100; i++) {
        Command cmd = commandService.getCommand(USER_COMMAND_PREFIX + i);
        if (cmd.isDefined()) {
            cmd.undefine();
        }
    }
    bindKeysHelper.saveIfChanged();
    setCommandIdToHandler(commandIdToHandler);
}
Also used : TriggerSequence(org.eclipse.jface.bindings.TriggerSequence) Category(org.eclipse.core.commands.Category) HashMap(java.util.HashMap) ICommandService(org.eclipse.ui.commands.ICommandService) HashSet(java.util.HashSet) Binding(org.eclipse.jface.bindings.Binding) BindKeysHelper(org.python.pydev.shared_ui.bindings.BindKeysHelper) BadLocationException(org.eclipse.jface.text.BadLocationException) ExecutionException(org.eclipse.core.commands.ExecutionException) ParseException(org.eclipse.jface.bindings.keys.ParseException) IWorkbench(org.eclipse.ui.IWorkbench) IFilter(org.python.pydev.shared_ui.bindings.BindKeysHelper.IFilter) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand) Command(org.eclipse.core.commands.Command) ParseException(org.eclipse.jface.bindings.keys.ParseException) KeySequence(org.eclipse.jface.bindings.keys.KeySequence) ParameterizedCommand(org.eclipse.core.commands.ParameterizedCommand)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Category (org.eclipse.core.commands.Category)1 Command (org.eclipse.core.commands.Command)1 ExecutionException (org.eclipse.core.commands.ExecutionException)1 ParameterizedCommand (org.eclipse.core.commands.ParameterizedCommand)1 Binding (org.eclipse.jface.bindings.Binding)1 TriggerSequence (org.eclipse.jface.bindings.TriggerSequence)1 KeySequence (org.eclipse.jface.bindings.keys.KeySequence)1 ParseException (org.eclipse.jface.bindings.keys.ParseException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IWorkbench (org.eclipse.ui.IWorkbench)1 ICommandService (org.eclipse.ui.commands.ICommandService)1 BindKeysHelper (org.python.pydev.shared_ui.bindings.BindKeysHelper)1 IFilter (org.python.pydev.shared_ui.bindings.BindKeysHelper.IFilter)1