vscode-which-key icon indicating copy to clipboard operation
vscode-which-key copied to clipboard

Unable to create when condition for pin/unpin editor

Open Dimfred opened this issue 4 years ago • 4 comments

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

Dimfred avatar Jul 21 '21 13:07 Dimfred

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.

stevenguh avatar Jul 22 '21 03:07 stevenguh

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.

Dimfred avatar Jul 22 '21 19:07 Dimfred

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

stevenguh avatar Aug 01 '21 18:08 stevenguh

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.

Dimfred avatar Aug 02 '21 07:08 Dimfred