覆盖扩展中的现有 Visual Studio Code 命令 [英] Overriding existing Visual Studio Code commands in an extension

查看:19
本文介绍了覆盖扩展中的现有 Visual Studio Code 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以覆盖现有的 VS Code 命令,例如editor.action.clipboardPasteAction?通过覆盖,我的意思是注册我自己的命令,每次应该调用原始命令时都会自动调用该命令.

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.

例如,editor.action.clipboardPasteAction 在按下 Ctrl+V 时被调用(或其他一些快捷键,取决于键绑定),但也当它在 eg 的代码中被显式调用时通过调用各种扩展

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?

我试图自己弄清楚,但无法真正找到提供完整功能的任何东西.我找到的最接近的解决方案是在例如剪贴板历史 扩展.这个扩展试图通过覆盖密钥来实现重载"package.json 中粘贴操作的绑定:

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"
}

然后在 clipboard.paste 命令中调用 editor.action.clipboardPasteAction,如上所示.

and then calling the editor.action.clipboardPasteAction within the clipboard.paste command as shown above.

这种方法的问题是双重的:

The problem with this approach is twofold:

  1. 如果覆盖"的原始命令的键绑定与我们在 package.json 中定义的键绑定不同怎么办?
  2. 如果原始命令不是通过键盘快捷键调用,而是通过例如通过使用 commands.executeCommand() 或通过命令面板的一些扩展.
  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.

推荐答案

在 VSCode 1.37.1(当前为 2019-08-30)中,答案是:不可能拦截命令,也不枚举键绑定.

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.

  • 我怎么知道?我最近花了很多时间翻阅 扩展 API 和,好吧,他们不在那里.当然,我可能忽略了一些不明显的东西.
  • 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.

但是监听命令的一个功能是问题#1431,仅在过去两个月内实施,据称可在当前的 insiders 中使用a> 发布(我自己还没有证实).但是,请注意,计划中的功能不允许拦截(在取消的意义上)命令.

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 changes to editors such as the selection or contents.

你可以列举扩展,听任何他们发出的事件,调用他们选择发布的任何方法,并检查他们的package.json 文件.

You can enumerate extensions, listen to any events they emit, call any methods they choose to publish, and examine their package.json files.

这篇关于覆盖扩展中的现有 Visual Studio Code 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆