How can I make VSCode file explorer menu items context sensitive to the file selected?

Viewed 376

Description

I am working on an extension for Visual Studio Code and I am contributing some commands to the File Explorer menu. I want my commands to be visible based on the file that is selected in the File Explorer as well as some settings that the user has to configure.

So far I have my commands being contributed to the menu, and they are enabled based on a custom context that I have created. I have also created a function that can determine whether the menu items should be enabled given the path of the file selected (note that the enablement logic is more complex than just having a regex match on the file name/path). The problem is that right now the context value is set only once during the activate. I need the value to be set dynamically using my utility function each time the selection is changed in the explorer (or whenever the menu is created - whichever is easier). I have not been able to find a way to listen for changes in selection in the File Explorer. I know the vscode.Window namespace has several listener functions, but they all seem to be for the editor or when changing the state of views

Is this possible?

Code

activate function:

vscode.commands.executeCommand('setContext', 'myContext', true); // not calling my utility function because I don't have the selected file path available

package.json:

"explorer/context": [
    {
        "command": "my.command",
        "when": "myContext"
    },
1 Answers
Related