【问题标题】:How to persist when-clause context in VSCode extension如何在 VSCode 扩展中保留 when 子句上下文
【发布时间】:2021-04-07 09:45:12
【问题描述】:

我按照when clause context的文档添加了自定义的when子句上下文。

我设置了一个布尔上下文:

let start = vscode.commands.registerCommand('myExt.start', () => {
    vscode.commands.executeCommand('setContext', 'myExt.activated', true);
});

但是VSCode重启后,即使myExt.activatedtrue。 VSCode 算了……

"explorer/context": [
    {
        "command": "myExt.filter",
        "when": "myExt.activated"
    }
]

即使 myExt.activated 上次是 true,在 VSCode 重新启动后也不显示...

另外,使用Developer: Inspect Context Keys 命令调试上下文时,myExt.activated 不存在。

有没有办法在 VSCode 重新启动之间保留 when-clause 上下文?

【问题讨论】:

  • 使用ExtensionContextcode.visualstudio.com/api/references/…中的globalStorageUriglobalState
  • 我已经将workspaceState 用于其他用途,因为我想要工作区范围。但是我没有成功在when-clause中使用它(没有出现在上下文键中)
  • 是否有某些原因您不只是在激活扩展程序时执行setContext 命令?您现在正在通过命令执行此操作 - 只需执行 activate 函数顶部的执行部分。
  • 确实如此。但是正如你所看到的,我正在为explorer/context 和在激活方式上执行setContext 做出贡献 - 我必须注册到*onStartupFinished 只是为了执行setContext,我认为这是不必要的并且想要避免它。另一个“问题”是在激活完成之前无法访问菜单......

标签: visual-studio-code vscode-extensions


【解决方案1】:

由于答案出现在 cmets 中(@rioV8 和 @Mark),我将在此处对其进行总结:

对于存储持久化数据,有globalStateworkspaceState(来自ExtensionContext)。

为了存储持久化上下文,我们必须使用globalStateworkspaceState 来存储状态并注册到*onStartupFinished 激活事件,并立即调用setContext 并使用持久存储的状态。

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 2017-05-14
    • 1970-01-01
    • 2020-08-10
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    相关资源
    最近更新 更多