使用“vscode.open"来自 TreeDataProvider 的命令 [英] Use "vscode.open" command from TreeDataProvider

查看:14
本文介绍了使用“vscode.open"来自 TreeDataProvider 的命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创作 vscode 扩展时,如何从自定义树视图项打开文件?这是一个示例树视图项:

How do you open a file from a custom tree view item when authoring a vscode extension? Here's a sample tree view item:

import { URI } from 'vscode-uri';
import { TreeDataProvider, ProviderResult, TreeItem } from 'vscode';

export class CustomTreeProvider implements TreeDataProvider<TreeItem>{

    public getChildren(element?: TreeItem): ProviderResult<TreeItem[]> {
        if (!element) {
            return [{
                id: 'some-unique-id',
                label: 'some-unique-label',
                command: {
                    command: 'vscode.open',
                    title: 'Open',
                    arguments: [URI.file('path/to/file']
                }
            }];
        }
    }

}

但是,每当我单击该树视图项时,它都会失败并在控制台中显示以下消息:

However, whenever I click on that tree view item, it fails with this message in the console:

[renderer7] [error] 运行贡献的命令:'vscode.open' 失败.非法参数资源"-要打开的资源:错误:运行贡献的命令:vscode.open"失败.非法参数 'resource' - 要打开的资源

[renderer7] [error] Running the contributed command: 'vscode.open' failed. Illegal argument 'resource' - Resource to open: Error: Running the contributed command: 'vscode.open' failed. Illegal argument 'resource' - Resource to open

我有一个解决方法,我可以注册自己的自定义命令来打开文件,但我更愿意使用内置的vscode.open"如果可能的话,命令.

I have a workaround where I can register my own custom command to open the file, but I'd rather use the built-in "vscode.open" command if possible.

推荐答案

问题是 vscode.open 命令不喜欢 Uri 生成的 Uricode>vscode-uri 包.改用 vscode.Uri.

The problem is that the vscode.open command doesn't like the Uri generated by the vscode-uri package. Use vscode.Uri instead.

import { TreeDataProvider, ProviderResult, TreeItem, Uri } from 'vscode';

export class CustomTreeProvider implements TreeDataProvider<TreeItem>{

    public getChildren(element?: TreeItem): ProviderResult<TreeItem[]> {
        if (!element) {
            return [{
                id: 'some-unique-id',
                label: 'some-unique-label',
                command: {
                    command: 'vscode.open',
                    title: 'Open',
                    arguments: [Uri.file('path/to/file']
                }
            }];
        }
    }

}

这篇关于使用“vscode.open"来自 TreeDataProvider 的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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