You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VS Code contributes.menus的when字段可用变量咨询

VS Code contributes.menus: when Clause Variables & Markdown-Specific Options

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 to resourceLangId, 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 to markdown for .md files).
  • resourceScheme: The URI scheme of the file—like file for local files, untitled for unsaved drafts, or vscode-vfs for 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, like README.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:32:51