根据contributes.views 文档,when 取决于 context 值。
根据 npm 扩展,它还可以包含配置设置。
"views": {
"explorer": [{
"id": "npm",
"name": "%view.name%",
"when": "npm:showScriptExplorer || config.npm.enableScriptExplorer"
}]
},
npm 扩展在激活函数中设置上下文值
if (await hasPackageJson()) {
vscode.commands.executeCommand('setContext', 'npm:showScriptExplorer', true);
}
您可以尝试以下方法:
在您的扩展程序中定义一个事件处理程序,以便在编辑器更改时调用 (onDidChangeActiveTextEditor)。
在事件处理程序中,根据文档的 langugeId 设置上下文值。
编辑
它适用于以下代码
const setContext = () => {
vscode.commands.executeCommand('setContext', 'documentOutline:fileIsHTML',
vscode.window.activeTextEditor.document.languageId == 'html'); };
vscode.window.onDidChangeActiveTextEditor(setContext, null, context.subscriptions);
setContext();
而在package.json
"activationEvents": [
"onLanguage:html"
],
"contributes": {
"views": {
"explorer": [
{
"id": "documentOutline",
"name": "Document Outline",
"when": "documentOutline:fileIsHTML"
}
]
}
}
或者看看这个问题conditional visibility for custom views 它可能更简单。请参阅 Weinands 示例:在 == 比较中添加引号。
"activationEvents": [
"onLanguage:html"
],
"contributes": {
"views": {
"explorer": [
{
"id": "documentOutline",
"name": "Document Outline",
"when": "editorLangId == 'html'"
}
]
}
}
编辑
试过了,还是不行,可能editorLangId不是上下文变量。