Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 674
aviit Avatar asked Aug 22 '18 07:08

aviit


People also ask

How do you reference a file in VS Code?

You can use the Find All References command to find where particular code elements are referenced throughout your codebase. The Find All References command is available on the context (right-click) menu of the element you want to find references to. Or, if you are a keyboard user, press Shift + F12.


1 Answers

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

vscode.commands.registerCommand('myextension.mycommand', (uri:vscode.Uri) => {
    console.log(uri.fsPath);
});
like image 135
Gama11 Avatar answered Oct 29 '22 20:10

Gama11