Skip to content

when 表达式

Visual Studio Code sets various context keys and specific values depending on what elements are visible and active in the VS Code UI. These contexts can be used to selectively enable or disable extension commands and UI elements, such as menus and views.

For example, VS Code uses when clauses to enable or disable command keybindings, which you can see in the Default Keybindings JSON (Preferences: Open Default Keyboard Shortcuts (JSON)):

json
{ "key": "f5",  "command": "workbench.action.debug.start",
                   "when": "debuggersAvailable && !inDebugMode" },

Above, the built-in Start Debugging command has the keyboard shortcut kb(workbench.action.debug.start), which is only enabled when there is an appropriate debugger available (context key debuggersAvailable is true) and the editor isn't in debug mode (context key inDebugMode is false).

Conditional operators

A when clause can consist of a context key (for example, inDebugMode) or can use various operators to express more nuanced editor state.

Logical operators

Logical operators allow combining simple context keys or when-clause expressions that include other logical, equality, comparison, match, in/not in operators or parenthesized expressions.

OperatorSymbolExample
Not!"!editorReadonly" or "!(editorReadonly || inDebugMode)"
And&&"textInputFocus && !editorReadonly"
Or||"isLinux || isWindows"

Note on logical operator precedence: the above table lists operators in order of highest to lowest precedence. Examples:

Written asInterpreted as
!foo && bar(!foo) && bar
!foo || bar (!foo) || bar
foo || bar && baz foo || (bar && baz)
!foo && bar || baz (!foo && bar) || baz
!(foo || bar) && baz (remains same) !(foo || bar) && baz

Equality operators

You can check for equality of a context key's value against a specified value. Note that the right-hand side is a value and not interpreted as a context key, meaning it is not looked up in the context.

OperatorSymbolExample
Equality=="editorLangId == typescript" or "editorLangId == 'typescript'"
Inequality!="resourceExtname != .js" or "resourceExtname != '.js'"

Notes:

  • If the value on the right-hand side is a string containing whitespace, it must be wrapped in single-quotes - "resourceFilename == 'My New File.md'".
  • === has the same behavior as ==, and !== has the same behavior as !=

Comparison operators

You can compare a context key's value against a number. Note that left- and right-hand side of the operator must be separated by whitespace - foo < 1, but not foo<1.

OperatorSymbolsExample
Greater than>, >="gitOpenRepositoryCount >= 1" but not "gitOpenRepositoryCount>=1"
Less than<, <="workspaceFolderCount < 2" but not "workspaceFolderCount<2"

Match operator

(previous name: key-value pair match operator)

OperatorSymbolExample
Matches=~"resourceScheme =~ /^untitled$|^file$/"

There is a match operator (=~) for when clauses. The expression key =~ regularExpressionLiteral treats the right-hand side as a regular expression literal to match against the left-hand side. For example, to contribute context menu items for all Docker files, one could use:

json
   "when": "resourceFilename =~ /docker/"

Notes:

  • The right-hand side of the =~ operator follows the same rules as regular expression literals (reference) in JavaScript, except characters need to follow escaping rules both of JSON strings and regular expressions. For example, a regular expression literal to match a substring file:// would be /file:\/\// in JavaScript but /file:\\/\\// in a when clause because a backslash needs to be escaped in a JSON string and a slash needs to be escaped in the regular expression pattern.
  • There does not exist an operator !=~, but you can negate the match expression - !(foo =~ /baz/).

Regular expression flags

It is possible to use flags with the regular expression literals. For example, resourceFilename =~ /json/i or myContextKey =~ /baz/si.

Supported flags: i, s, m, u.

Ignored flags: g, y.

'in' and 'not in' conditional operators

The in operator for when clauses allows for a dynamic lookup of a context key's value within another context key's value. For example, if you wanted to add a context menu command to folders that contain a certain type of file (or something that can't be statically known), you can now use the in operator to achieve it. You can use the not in operator to check the opposite condition.

OperatorSymbolExample
Inin"resourceFilename in supportedFolders"
Not innot in"resourceFilename not in supportedFolders"

First, determine which folders should support the command, and add the folder names to an array. Then, use the setContext command to turn the array into a context key:

ts
vscode.commands.executeCommand('setContext', 'ext.supportedFolders', [ 'test', 'foo', 'bar' ]);

// or

// Note in this case (using an object), the value doesn't matter, it is based on the existence of the key in the object
// The value must be of a simple type
vscode.commands.executeCommand('setContext', 'ext.supportedFolders', { 'test': true, 'foo': 'anything', 'bar': false });

Then, in the package.json you could add a menu contribution for the explorer/context menu:

json
// Note, this assumes you have already defined a command called ext.doSpecial
"menus": {
  "explorer/context": [
    {
      "command": "ext.doSpecial",
      "when": "explorerResourceIsFolder && resourceFilename in ext.supportedFolders"
    }
  ]
}

In that example, we are taking the value of resourceFilename (which is the name of the folder in this case) and checking for its existence in the value of ext.supportedFolders. If it exists, the menu will be shown. This powerful operator should allow for richer conditional and dynamic contributions that support when clauses, for example menus, views, etc.

Available context keys

Below are some of the available context keys, which evaluate to Boolean true/false.

The list here isn't exhaustive and you can find other when clause contexts by searching and filtering in the Keyboard Shortcuts editor (Preferences: Open Keyboard Shortcuts) or reviewing the Default Keybindings JSON file (Preferences: Open Default Keyboard Shortcuts (JSON)). You can also identify context keys you are interested in using the Inspect Context Keys utility.

Context nameTrue when
Editor contexts
editorFocusAn editor has focus, either the text or a widget.
editorTextFocusThe text in an editor has focus (cursor is blinking).
textInputFocusAny editor has focus (regular editor, debug REPL, etc.).
inputFocusAny text input area has focus (editors or text boxes).
editorTabMovesFocusWhether kbstyle(Tab) will move focus out of the editor.
editorHasSelectionText is selected in the editor.
editorHasMultipleSelectionsMultiple regions of text are selected (multiple cursors).
editorReadonlyThe editor is read only.
editorLangIdTrue when the editor's associated language ID matches.
Example: "editorLangId == typescript".
isInDiffEditorThe active editor is a difference editor.
isInEmbeddedEditorTrue when the focus is inside an embedded editor.
Operating system contexts
isLinuxTrue when the OS is Linux.
isMacTrue when the OS is macOS.
isWindowsTrue when the OS is Windows.
isWebTrue when accessing the editor from the Web.
List contexts
listFocusA list has focus.
listSupportsMultiselectA list supports multi select.
listHasSelectionOrFocusA list has selection or focus.
listDoubleSelectionA list has a selection of 2 elements.
listMultiSelectionA list has a selection of multiple elements.
Mode contexts
inSnippetModeThe editor is in snippet mode.
inQuickOpenThe Quick Open dropdown has focus.
Resource contexts
resourceSchemeTrue when the resource Uri scheme matches.
Example: "resourceScheme == file"
resourceFilenameTrue when the Explorer or editor filename matches.
Example: "resourceFilename == gulpfile.js"
resourceExtnameTrue when the Explorer or editor filename extension matches.
Example: "resourceExtname == .js"
resourceDirnameTrue when the Explorer or editor's resource absolute folder path matches.
Example: "resourceDirname == /users/alice/project/src"
resourcePathTrue when the Explorer or editor's resource absolute path matches.
Example: "resourcePath == /users/alice/project/gulpfile.js"
resourceLangIdTrue when the Explorer or editor title language ID matches.
Example: "resourceLangId == markdown"
isFileSystemResourceTrue when the Explorer or editor file is a file system resource that can be handled from a file system provider.
resourceSetTrue when an Explorer or editor file is set.
resourceThe full Uri of the Explorer or editor file.
Explorer contexts
explorerViewletVisibleTrue if Explorer view is visible.
explorerViewletFocusTrue if Explorer view has keyboard focus.
filesExplorerFocusTrue if File Explorer section has keyboard focus.
openEditorsFocusTrue if OPEN EDITORS section has keyboard focus.
explorerResourceIsFolderTrue if a folder is selected in the Explorer.
Editor widget contexts
findWidgetVisibleEditor Find widget is visible.
suggestWidgetVisibleSuggestion widget (IntelliSense) is visible.
suggestWidgetMultipleSuggestionsMultiple suggestions are displayed.
renameInputVisibleRename input text box is visible.
referenceSearchVisiblePeek References peek window is open.
inReferenceSearchEditorThe Peek References peek window editor has focus.
config.editor.stablePeekKeep peek editors open (controlled by editor.stablePeek setting).
codeActionMenuVisibleCode Action menu is visible.
parameterHintsVisibleParameter hints are visible (controlled by editor.parameterHints.enabled setting).
parameterHintsMultipleSignaturesMultiple parameter hints are displayed.
Debugger contexts
debuggersAvailableAn appropriate debugger extension is available.
inDebugModeA debug session is running.
debugStateActive debugger state.
Possible values are inactive, initializing, stopped, running.
debugTypeTrue when debug type matches.
Example: "debugType == 'node'".
inDebugReplFocus is in the Debug Console REPL.
Integrated terminal contexts
terminalFocusAn integrated terminal has focus.
terminalIsOpenAn integrated terminal is opened.
Timeline view contexts
timelineFollowActiveEditorTrue if the Timeline view is following the active editor.
Timeline view item contexts
timelineItemTrue when the timeline item's context value matches.
Example: "timelineItem =~ /git:file:commit\\b/".
Extension contexts
extensionTrue when the extension's ID matches.
Example: "extension == eamodio.gitlens".
extensionStatusTrue when the extension is installed.
Example: "extensionStatus == installed".
extensionHasConfigurationTrue if the extension has configuration.
Global UI contexts
notificationFocusNotification has keyboard focus.
notificationCenterVisibleNotification Center is visible at the bottom right of VS Code.
notificationToastsVisibleNotification toast is visible at the bottom right of VS Code.
searchViewletVisibleSearch view is open.
sideBarVisibleSide Bar is displayed.
sideBarFocusSide Bar has focus.
panelFocusPanel has focus.
inZenModeWindow is in Zen Mode.
isCenteredLayoutEditor is in centered layout mode.
workbenchStateCan be empty, folder (1 folder), or workspace.
workspaceFolderCountCount of workspace folders.
replaceActiveSearch view Replace text box is open.
viewFor view/title and view/item/context, the view to display the command in.
Example: "view == myViewsExplorerID".
viewItemFor view/item/context, the contextValue from the tree item.
Example: "viewItem == someContextValue".
webviewIdFor webview/context, the webview ID to display the command in.
Example: "webviewId == catCoding".
isFullscreenTrue when window is in fullscreen.
focusedViewThe identifier of the currently focused view.
canNavigateBackTrue if it is possible to navigate back.
canNavigateForwardTrue if it is possible to navigate forward.
canNavigateToLastEditLocationTrue if it is possible to navigate to the last edit location.
Global Editor UI contexts
textCompareEditorVisibleAt least one diff (compare) editor is visible.
textCompareEditorActiveA diff (compare) editor is active.
editorIsOpenTrue if one editor is open.
groupEditorsCountNumber of editors in a group.
activeEditorGroupEmptyTrue if the active editor group has no editors.
activeEditorGroupIndexA number starting from 1 reflecting the position of an editor group in the editor grid.
The group with index 1 will be the first in the top-left corner.
activeEditorGroupLastWill be true for the last editor group in the editor grid.
multipleEditorGroupsTrue when multiple editor groups are present.
activeEditorThe identifier of the active editor in a group.
activeEditorIsDirtyTrue when the active editor in a group is dirty.
activeEditorIsNotPreviewTrue when the active editor in a group is not in preview mode.
activeEditorIsPinnedTrue when the active editor in a group is pinned.
inSearchEditorTrue when focus is inside a search editor.
activeWebviewPanelIdThe id of the currently active webview panel.
activeCustomEditorIdThe id of the currently active custom editor.
Configuration settings contexts
config.editor.minimap.enabledTrue when the setting editor.minimap.enabled is true.

Note: You can use any user or workspace setting that evaluates to a boolean here with the prefix "config.".

Visible/focused view when clause context

You can have a when clause that checks if a specific View is visible or focused.

Context nameTrue when
view.${viewId}.visibleTrue when specific view is visible.
Example: "view.workbench.explorer.fileView.visible"
focusedViewTrue when specific view is focused.
Example: "focusedView == 'workbench.explorer.fileView'"

View identifiers:

  • workbench.explorer.fileView - File Explorer
  • workbench.explorer.openEditorsView - Open Editors
  • outline - Outline view
  • timeline - Timeline view
  • workbench.scm - Source Control
  • workbench.scm.repositories - Source Control Repositories
  • workbench.debug.variablesView - Variables
  • workbench.debug.watchExpressionsView - Watch
  • workbench.debug.callStackView - Call Stack
  • workbench.debug.loadedScriptsView - Loaded Scripts
  • workbench.debug.breakPointsView - Breakpoints
  • workbench.debug.disassemblyView - Disassembly
  • workbench.views.extensions.installed - Installed extensions
  • extensions.recommendedList - Recommended extensions
  • workbench.panel.markers.view - Problems
  • workbench.panel.output - Output
  • workbench.panel.repl.view - Debug Console
  • terminal - Integrated Terminal
  • workbench.panel.comments - Comments

Visible view container when clause context

You can have a when clause that checks if a specific View Container is visible

Context keyTrue when
activeViewletTrue when view container is visible in the sidebar.
Example: "activeViewlet == 'workbench.view.explorer'"
activePanelTrue when view container is visible in the panel.
Example: "activePanel == 'workbench.panel.output'"
activeAuxiliaryTrue when view container is visible in the secondary sidebar.
Example: "activeAuxiliary == 'workbench.view.debug'"

View container identifiers:

  • workbench.view.explorer - File Explorer
  • workbench.view.search - Search
  • workbench.view.scm - Source Control
  • workbench.view.debug - Run
  • workbench.view.extensions - Extensions
  • workbench.panel.markers - Problems
  • workbench.panel.output - Output
  • workbench.panel.repl - Debug Console
  • terminal - Integrated Terminal
  • workbench.panel.comments - Comments

If you want a when clause that is enabled only when a specific view container has focus, use sideBarFocus or panelFocus or auxiliaryBarFocus in combination with activeViewlet or activePanel or activeAuxiliary context keys.

For example, the when clause below is true only when the File Explorer has focus:

json
"sideBarFocus && activeViewlet == 'workbench.view.explorer'"

Check a setting in a when clause

In a when clause, you can reference a configuration (setting) value by prefixing it with config., for example config.editor.tabCompletion or config.breadcrumbs.enabled.

Add a custom when clause context

If you are authoring your own VS Code extension and need to enable/disable commands, menus, or views using a when clause context and none of the existing keys suit your needs, you can add your own context key with the setContext command.

The first example below sets the key myExtension.showMyCommand to true, which you can use in enablement of commands or with the when property. The second example stores a value that you could use with a when clause to check if the number of cool open things is greater than 2.

js
vscode.commands.executeCommand('setContext', 'myExtension.showMyCommand', true);

vscode.commands.executeCommand('setContext', 'myExtension.numberOfCoolOpenThings', 4);

Inspect Context Keys utility

If you would like to see all currently active context keys at runtime, you can use the Developer: Inspect Context Keys command from the Command Palette (kb(workbench.action.showCommands)). Inspect Context Keys will display context keys and their values in the VS Code Developer Tools Console tab (Help > Toggle Developer Tools).

When you run Developer: Inspect Context Keys, your cursor will highlight elements in the VS Code UI and when you click on an element, the current context keys and their states will be output as an object to the Console.

Inspect Context Keys output

The list of active context keys is extensive and may contain custom context keys from extensions you have installed.

Note: Some context keys are for VS Code internal use and may change in the future.

评论区
评论区空空如也
发送评论
名字
0 / 20
邮箱
0 / 100
评论内容
0 / 140
由于是非实名评论,所以不提供删除功能。如果你需要删除你发送的评论,或者是其他人的评论对你造成了困扰,请 发邮件给我 。同时评论区会使用 AI + 人工的方式进行审核,以达到合规要求。

© thebestxt.cc
辽ICP备16009524号-8
本站所有文章版权所有,转载请注明出处