【问题标题】:How to access workspace state from VS Code commands?如何从 VS Code 命令访问工作区状态?
【发布时间】:2020-07-26 18:05:05
【问题描述】:

我是 VS Code 扩展构建的新手,遇到了问题。我无法访问我在 ExtensionContext 的 workspaceState 属性中创建和存储的对象。

export async function activate(context: vscode.ExtensionContext) {

    // Some code

    const linksToDisplay = await initalizeLinks(context.workspaceState); // Initializes and saves all objects 

    // Some other code

    let visualizer = vscode.commands.registerCommand('codePlumber.visualize', displayLinksCommand);

    context.subscriptions.push(visualizer);
}

function displayLinksCommand() {
    // How can I access workspace state here?
}

基本上我在启动时为我的扩展创建对象,然后我希望我的命令与这些创建的对象进行交互。

我尝试将 context.workspaceState 作为参数传递给 registerCommand 函数,例如

let visualizer = vscode.commands.registerCommand('codePlumber.visualize', displayLinksCommand, context.workspaceState);

但这没有用。

我有几个问题:

有没有办法从我的命令中访问 workspaceState?如果是这样,那段代码是什么样的?

我在 ExtensionContext 中保存对象的方法是否错误?如果是这样,我可以使用其他替代方法吗?

【问题讨论】:

    标签: typescript visual-studio-code vscode-extensions


    【解决方案1】:

    据我所知,扩展上下文仅在 activate 方法中可用,但您可以使用闭包并在那里使用上下文。这是一个例子:

    context.subscriptions.push(commands.registerCommand("codePlumber.visualize",
            () => {
                // use the context here.
            }),
        );
    

    【讨论】:

      猜你喜欢
      • 2022-06-21
      • 2020-11-17
      • 1970-01-01
      • 1970-01-01
      • 2015-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多