How to get file name or path in vscode extension when user right click on file in explorer/context?

落爺英雄遲暮 提交于 2019-12-14 04:18:31

问题


My extension creates a context menu in the explorer tree:

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

In 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?
}));

I want to get the filename or file path of the file I've right click on the context menu when run my command. Can you tell me how? Thank you very much!


回答1:


The callback will receive an argument with a vscode.Uri object:

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


来源:https://stackoverflow.com/questions/51961457/how-to-get-file-name-or-path-in-vscode-extension-when-user-right-click-on-file-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!