Home > Mobile >  Is there anyway to check inactive terminalEditors in when arg in keybindings.json in VScode?
Is there anyway to check inactive terminalEditors in when arg in keybindings.json in VScode?

Time:10-05

I want to createTerminalEditor if there is no terminalEditor opened yet.

Note: I'm talking about terminalEditor and not terminal.

So, I'm looking for a when arg which says something like editorAlreadyExists != terminalEditor, just like there is activeEditor which aceepts string of terminalEditor.

Is there anyway to achieve this?

Here is activeEditor example for reference, but I want to check if terminalEditor exists in all the already opened editors, not just a activeEditor.

  {
    "key": "ctrl `",
    "command": "workbench.action.createTerminalEditor",
    "when": "activeEditor != terminalEditor"
  },

CodePudding user response:

I see that there is a when clause for:

terminalEditorFocus: true/false

which doesn't look like it would help except for the fact that it isn't in the list (via Developer: Inspect Context Keys) at all when there is no terminal editor open. So I thought maybe there was a keybinding when clause that could exploit this. But I tried all kinds of thing, like whether it was null or undefined or the empty string or neither true nor false, etc. but nothing worked. I think if the key terminalEditorFocus doesn't exist, then nothing at all is delivered to the keybinding resolver and it always fails.

You could file an issue asking for a specific terminalEditorExists sort of when clause.


There will be another way. There is a presently experimental api to access all the open tabs. See proposed api. So you could write an extension that checks all the open tabs and fires the workbench.action.createTerminalEditor command if none are a terminalEditor. It works right now in the Insiders Build, but when it will go final I don't know - it seems pretty solid now. Issue: Tab Model API.

const tabs = vscode.window.tabs;
const openTerminalEditor = tabs.some(tab => tab.viewId === 'terminalEditor');  // true/false

Then you could either set your own context key with setContext or run the command.

  • Related