VS Code - find panel/view context name for shortcut 'when' expression

大城市里の小女人 提交于 2021-02-20 04:08:54

问题


I'm trying to add some shortcuts to my VS Code instance and I want to scope them using the when expression to avoid conflicts.

Alas, I'm struggling to find the right context name to use in the expression to achieve what I want.

I've searched the documentation and found that a number of them are listed here: https://code.visualstudio.com/docs/getstarted/keybindings#_contexts

But, as the documentation says:

The list above isn't exhaustive and you may see some when contexts for specific VS Code UI in the Default Keyboard Shortcuts.

Of course, the context to which I want to scope my shortcuts is not on the list ;)

Thus, I was wondering if there was a way to find the context name of a specific panel/view in the VS Code UI.

To illustrate this, suppose I'd like to make a shortcut active only when the panel that lists the available NPM scripts is focused... What context name should be used to achieve that?


回答1:


I tried this - not expecting it to work - but it seems to:

"when": "focusedView == npm"

Similar info is active/focussed view context clauses.

In general, you can:

  1. Open the developer tools
  2. Choose Inspect Context Keys in the VS Code Command Palette
  3. Click on an element in the vscode window
  4. Look in the tools console and expand the last item therein to show all the current context (when clauses).

But when I did that I didn't find anything helpful and focusedView = "" was there, so that's not helpful.

so I just tried

{
  "key": "alt+i",
  "command": "workbench.action.terminal.sendSequence",
  "args": { "text": "howdy" },
  "when": "focusedView == npm"
},

that just prints howdy to the terminal for testing.. And the only way I could get it to work was when the npm scripts viewlet header or its contents had focus.



来源:https://stackoverflow.com/questions/62190992/vs-code-find-panel-view-context-name-for-shortcut-when-expression

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!