当用户在资源管理器/上下文中右键单击文件时,如何获取 vscode 扩展中的文件名或路径?

问题描述 投票:0回答:2

我的扩展在资源管理器树中创建一个上下文菜单:

"contributes": {
        "commands": [
            ...
            {
                "command": "myextension.mycommand",
                "title": "Run my command"
            }
        ],
        "menus": {
            "explorer/context": [{
                "when": "resourceLangId == python",
                "command": "myextension.mycommand",
                "group": "MyGroup"
          }]
        }
    }

extension.ts

export function activate(context: vscode.ExtensionContext) {
context.subscriptions.push(vscode.commands.registerCommand('myextension.mycommand', () => {
    //How to get the filename or file path here?
}));

我想获取运行命令时右键单击上下文菜单的文件的文件名或文件路径。你能告诉我怎么做吗? 非常感谢!

visual-studio-code vscode-extensions
2个回答
18
投票

回调将接收带有

vscode.Uri
对象的参数:

vscode.commands.registerCommand('myextension.mycommand', (uri:vscode.Uri) => {
    console.log(uri.fsPath);
});

0
投票

这只适用于鼠标右键。 键盘快捷键如何工作?执行快捷键时,参数“uri”为空

© www.soinside.com 2019 - 2024. All rights reserved.