vscode-which-key
vscode-which-key copied to clipboard
Unable to create when condition for pin/unpin editor
Hey I am trying to create the pin / unpin logic, like with the toggle project tree example in the docs, but I can't make it work. Do you have any suggestions?
{
"key": "p",
"name": "Pin / Unpin editor",
"type": "conditional",
"bindings": [
{
"key": "",
"name": "default",
"type": "command",
"command": "workbench.action.pinEditor",
},
{
"key": "when:activeEditorIsPinned",
"name": "Unpin Editor",
"type": "command",
"command": "workbench.action.unpinEditor",
},
]
}
You will need to add the following to you shortcut (keybindings.json).
{
"key": "p",
"command": "whichkey.triggerKey",
"args": {
"key": "p",
"when": "activeEditorIsPinned"
},
"when": "whichkeyVisible && activeEditorIsPinned"
}
We can also choose to bundle this as default so we don't have two separate key for pin and unpin.
Just tested this and apparently, the activeEditorIsPinned context is unset once the focus exited the editor. So which-key can't the shortcut isn't being triggered (hence, not able to pass the when condition to which-key) when the menu is in focus.
Unfortunately, this is limit of the hack we have to capture context. Either vscode keep activeEditorIsPinned set or provide a getContext API; otherwise, this is something that we can't do atm.
Ah that is very sad. But thank you for the investigation, hope there something will happen soon, would be great to have that.
EDIT:
So I had some time and tinkered a bit around and got a hack for that. It basically uses xdotool to send a key, and that key represents the two toggle states.
keybindings.json:
{
"key": "f5",
"command": "workbench.action.pinEditor",
"when": "!activeEditorIsPinned",
},
{
"key": "f5",
"command": "workbench.action.unpinEditor",
"when": "activeEditorIsPinned",
},
settings.json:
{
"key": "p",
"name": "Pin/Unpin editor",
"type": "command",
"command": "workbench.action.terminal.sendSequence",
// without cleaning
"args": { "text": "i;xdotool key F5\n"},
// with cleaning
"args": { "text": "i;xdotool key F5;xdotool key Escape;clear\n"},
},
the ;i is only to leave the normal mode of my zsh. I also bound i() { return 0 } just to not get an error in zshrc.
I would also like the command to be executed in the background without showing it in the terminal history, but not sure whether this is possible.
I am glad you are able to find a workaround.
I looked at the vscode api it doesn't seem like they provide anything to extension to know if the editor is pinned. To properly support this, we likely need to do a feature request to vscode for either
- Have programmatic access to the editor pinned state
- Add toggle command to toggle between pin and unpin
Yeah I think the second option is something they will do. If I have time I probably willl open an issue and request a unified command.