Is it possible to override an existing VS Code command like e.g. editor.action.clipboardPasteAction
? By override, I mean to register my own command that will automatically be called every time when the original one was supposed to be called.
For example, the editor.action.clipboardPasteAction
is called when the Ctrl+V
is pressed (or some other shortcut, depending on the key bindings), but also when it is invoked explicitly in code of e.g. various extensions by calling
commands.executeCommand("editor.action.clipboardPasteAction");
Is it possible to "intercept" the command call in our own extension, replace it with our own functionality and then optionally either proceed with the execution of the original command or signal that the execution should be suspended?
I've tried to figure it out on my own, but couldn't really find anything that provides the complete functionality. The closest solution that I found is the one used in e.g. Clipboard History extension. This extension tries to achieve the "overloading" by overriding the key bindings for the Paste Action in its package.json
:
{
"command": "clipboard.paste",
"key": "ctrl+v",
"mac": "cmd+v",
"when": "editorTextFocus"
}
and then calling the editor.action.clipboardPasteAction
within the clipboard.paste
command as shown above.
The problem with this approach is twofold:
- What if the original command that is "overridden" has different key bindings than the ones we defined in
package.json
? - What if the original command is not called via keyboard shortcuts but instead e.g. via some extension by using
commands.executeCommand()
or via Command Palette.
The first issue could be avoided if there is a way to dynamically (during the registration of our extension) we can get the key bindings of the original command and then register our command with the same key bindings. I am not sure if this is possible either.