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:
package.json
?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.
In VSCode 1.37.1 (current as of 2019-08-30), the answer is no: it is not possible to intercept commands, nor enumerate keybindings.
But a feature to listen to commands is Issue #1431, which has been implemented just within the past two months, and is supposedly available in the current insiders release (I have not confirmed that myself). However, note that the planned feature does not allow one to intercept (in the sense of cancel) a command.
Consequently, even when that feature lands, it may be necessary to use it in combination with some of the other monitoring capabilities, depending on the desired functionality:
You can watch tasks, i.e., external process invocations.
You can watch changes to editors such as the selection or contents.
You can enumerate extensions, listen to any events they emit, call any methods they choose to publish, and examine their package.json
files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With