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

故事扮演 提交于 2020-01-24 13:11:38

问题


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!


回答1:


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"
}



来源:https://stackoverflow.com/questions/52382136/can-a-vscode-extension-create-an-explorer-context-menu-for-a-specific-file

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