仅在编辑器具有HTML文件时,我才尝试显示我的视图贡献。根据{{3}},我可以使用when
子句。我发现this issue on GitHub是基于文件类型条件的示例。它用于命令而非视图贡献。
我正在尝试使用以下配置,当我打开HTML文件时,视图贡献是隐藏的,但没有显示。如果我删除了when子句,则视图贡献会很好,但始终会显示。
"activationEvents": [
"onLanguage:html"
],
"contributes": {
"views": {
"explorer": [
{
"id": "documentOutline",
"name": "Document Outline",
"when": "editorLangId == html"
}
]
}
}
答案 0 :(得分:2)
根据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
不是上下文变量。