visual-studio-codevscode-keybinding

Can I use when clauses for individual command arguments in VS Code runCommands keybindings?


I'm trying to create a custom command in VS Code to hide/show the side bar and the activity bar at the same time, while also passing the focus to the activity bar if its shown or back to the text editor if it's hidden. Is there an available context key to achieve that?

At keybindings.json:

{
    "key": "ctrl+b",
    "command": "runCommands",
    "args": {
        "commands": [
            "workbench.action.toggleSidebarVisibility",
            "workbench.action.toggleActivityBarVisibility",
            // {    
            //     "command": "workbench.action.focusActivityBar",
            //     "when": "sideBarVisible" //does not work
            // }
        ]
    }
}

I also tried to manipulate the focus with command "workbench.action.nextSideBarView".


Solution

  • If you want to use a when clause in an individual command in runCommands, you want feature-request Allow separate when clauses for each command in a keybinding with multiple commands #184288. I suggest that you give that issue ticket a thumbs up to show support for it. You can also subscribe to it to get notified about discussion and progress. Please avoid making noisy comments there like ones that just consist of "+1" / "bump".

    For now, you'll just need to split out your command into separate keybindings with top-level when clauses (it will mean repeating yourself more, but that's just how it'll have be right now I think).