VS Code 扩展可以从其他程序获取 URI 请求吗?

Iva*_*anM 4 visual-studio-code vscode-extensions

I found information on the official docs that onUri can be used as an activation event. So, my extension can be activated from, say, a WPF program written in C# by calling the URI such as vscode://myextension/arg1=foo&arg2=bar. But nowhere does it say how I can get the arguments that were passed with the URI request. Or even just get a raw string of it.

My question is, can it be done and if not, is there any other way to make a VS Code extension interact with another program?

Gam*_*a11 6

是的,您可以使用vscode.window.registerUriHandler()以下方法:

注册一个能够处理系统范围uris的uri 处理程序。如果打开了多个窗口,最上面的窗口将处理 uri。uri 处理程序的范围仅限于它所贡献的扩展;它只能处理指向扩展本身的 uri。uri 必须遵守以下规则:

  • uri-scheme 必须是产品名称;

  • uri-authority 必须是扩展 ID(例如my.extension);

  • uri-path、-query 和 -fragment 部分是任意的。例如,如果my.extension扩展注册了 uri 处理程序,则仅允许处理带有前缀 的 uri product-name://my.extension

扩展在其整个激活生命周期中只能注册一个 uri 处理程序。

  • onUri注意:当即将处理指向当前扩展的 uri 时,会触发一个激活事件。

用法非常简单:

vscode.window.registerUriHandler({
    handleUri(uri:vscode.Uri) {
        // do something with the URI
    }
});
Run Code Online (Sandbox Code Playgroud)