Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a vscode extension create an explorer context menu for a specific file?

I'm writing a extension for vscode. I just want my context menu just show on when I right click on my file (ex: my_special_name_1.py). So I added this contribution point to package.json:

"contributes": {
    ...,
    "commands": [
        {
            "command": "command.hello",
            "title": "Hello my file"
        },
        ...
    ],
    "menus": {
                "explorer/context": [
                    {
                        "when": "resourceLangId == python",
                        "command": "command.hello"
                    }
                ]
            },
    ...
}

But this will show my command "Hello my file" on all .py files. How to let it just be shown only on my files (ex: my_special_name_1.py, my_special_name_2.py, ...)? Thanks!

like image 477
aviit Avatar asked Nov 22 '25 12:11

aviit


1 Answers

You can match the file name against a regex by using the =~ operator:

{
    "when": "resourceLangId == python && resourceFilename =~ /my_special_name_[0-9]+\\.py/",
    "command": "command.hello"
}

like image 77
Gama11 Avatar answered Nov 25 '25 10:11

Gama11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!