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);
}
Aggregations