VS Code contributes.menus的when字段可用变量咨询
Great question! Let's break this down into two clear sections: first, the general variables supported in when clauses, then the extra (and still applicable general) variables you can use when targeting markdown files specifically.
General Variables for when Clauses
These variables cover all kinds of contexts in VS Code, from editor state to file properties. Here are the most commonly used ones:
Editor & Text Context
editorFocus: True when any text editor has focus.editorHasSelection: True if you've selected text in the active editor.editorLangId: The language ID of the active editor's document (e.g.,javascript,python—similar toresourceLangId, but tied to the editor rather than the file resource).editorTabCount: Number of open editor tabs in the window.
File & Resource Context
resourceLangId: The language ID of the focused file (this is the one from your example, set tomarkdownfor .md files).resourceScheme: The URI scheme of the file—likefilefor local files,untitledfor unsaved drafts, orvscode-vfsfor virtual files.resourceExtname: The file extension (e.g.,.md,.js) of the focused resource.resourcePath: Full file system path of the resource.resourceBasename: The full filename (including extension, likeREADME.md).
Workspace & Window Context
workspaceFolderCount: Number of folders open in your workspace.windowFocus: True when the VS Code window is the active window on your desktop.inDebugMode: True when the debugger is running or paused.
View & UI Context
viewItem: For tree views (like the Explorer), refers to the currently selected item—you can check its properties, e.g.,viewItem == 'explorer-file'.inputFocus: True when any input field (like the command palette or a search box) has focus.
Variables for resourceLangId == markdown Scenarios
When you're targeting markdown files, all the general variables above still work, plus VS Code exposes markdown-specific variables tailored to the markdown editor and preview:
markdown.showPreview: True when the markdown preview panel is visible (even if it's not focused).markdown.previewFocus: True when the markdown preview panel has focus (instead of the source editor).markdown.editorFocus: True when the markdown source editor has focus (useful if you want to distinguish between editing and previewing).markdown.isInPreview: True if the active view is the markdown preview (rather than the source file).
Example Combinations
You can mix these with general variables for precise conditions:
- Show a menu item only when editing markdown and have text selected:
"when": "resourceLangId == markdown && editorHasSelection" - Show an item only in the markdown preview:
"when": "resourceLangId == markdown && markdown.isInPreview"
Remember, you can use logical operators (&&, ||, !) to build complex conditions that fit your exact use case.
内容的提问来源于stack exchange,提问作者original.roland

