Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overriding existing Visual Studio Code commands in an extension

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:

  1. What if the original command that is "overridden" has different key bindings than the ones we defined in package.json?
  2. 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.

like image 362
ironcev Avatar asked Sep 27 '17 18:09

ironcev


1 Answers

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.

  • How do I know? I've recently spent quite a bit of time rummaging through the extensions API and, well, they aren't there. Of course it's possible I have overlooked something non-obvious.

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.

like image 179
Scott McPeak Avatar answered Oct 04 '22 03:10

Scott McPeak