用于文件浏览器的 vscode api [英] vscode api for file explorer

查看:36
本文介绍了用于文件浏览器的 vscode api的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 VSCode 扩展,需要根据用户选择的文件夹/子文件夹创建文件/删除文件.

I am onto writing a VSCode extension and need to create files / remove files based in a folder / subfolder selected by the user.

有没有办法通过visual studio code api从资源管理器树中找到选定的文件夹?可以通过活动文本编辑器或 onDidOpenTextDocument() 跟踪打开的文件.我有兴趣查找用户是否选择了一个文件夹,如果是,则查找所选文件夹的路径.

Is there a way to find selected folder from the explorer tree through visual studio code api? One can track the opened file through active text editor or onDidOpenTextDocument(). I am interested in finding if user has selected a folder and if so the path to selected folder.

例如:workspace.rootPath 提供已打开项目的根目录.同样,我如何确定用户选择了哪个子文件夹(如果有).

For example: workspace.rootPath provides a root of the opened project. Similarly, how do I identify which subfolder is selected by the user (if any).

推荐答案

我终于想通了,找了半天,发现文档里说:

I finally figured it out, I've been looking for a while and found that the documentation states that :

注意:当从(上下文)菜单调用命令时,VS Code 会尝试推断当前选定的资源,并在调用命令时将其作为参数传递.例如,资源管理器中的菜单项被传递所选资源的 URI,而编辑器中的菜单项被传递文档的 URI.vscode 扩展贡献点

Note: When a command is invoked from a (context) menu, VS Code tries to infer the currently selected resource and passes that as a parameter when invoking the command. For instance, a menu item inside the Explorer is passed the URI of the selected resource and a menu item inside an editor is passed the URI of the document. vscode extension contribution points

这让我在 github 获取选定的文件夹上解决了这个问题/错误,其中反过来又把我带到了 github 仓库stef-levesque/vscode-hexdump 来自其中我得到以下信息:

That let me to this question/bug on github Get selected file folder, which in turn led me to the github repo of stef-levesque/vscode-hexdump from which I got the following:

let disposable = Commands.registerCommand('extension.superExtension', async (fileUri) => {
    console.log(fileUri);
})

注意:在编辑器中使用上下文菜单时,该菜单将为空.

Note: when the context menu is used in the editor this will be empty.

这篇关于用于文件浏览器的 vscode api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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