VS code API 文档不清楚如何读取 json 可定制值

Mak*_*gan 0 api json typescript visual-studio-code vscode-extensions

我已经阅读了在线文档和 VS code API 的源代码,但我仍然不清楚如何读取 JSON 用户设置和工作空间设置。我尝试寻找示例,但找不到仅显示如何执行此操作的内容。

本质上我想要一个名为“最大值”的设置值。然后我想从 TS scrypt 读取最大值。

该文档引导我在 activate 函数中编写以下两行:

const config = vscode.workspace.getConfiguration('extension', vscode.window.activeTextEditor.document.uri);
vscode.window.showInformationMessage(config.has('configuration').toString());
Run Code Online (Sandbox Code Playgroud)

在我添加的 package.json 文件中,在贡献下:

    "configuration": {
        "maximum":
        {
            "type": ["integer"],
            "default": 40,
            "description": "The level of alignment, how far the titles will extend horizontally"
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是我的输出是 false 。

我尝试了多个不同的输出,唯一一次我得到正确的结果是当第一个函数的参数是“启动”而第二个参数是“配置”时。

我无法仅从文档中了解参数需要是什么。也不应该在哪里指定该值。

Haa*_*Leo 5

您获得False输出是因为 package.json 中的配置关键字仅指示扩展的配置贡献点。
我建议您采用 vscode 示例来满足您的需求:

"contributes": {
    "configuration": {
        "type": "object",
        "title": "This is my config",
        "properties": {
            "myExtensionName.maximum": {
                "type": "integer",
                "default": 40,
                "description": "The level of alignment, how far the titles will extend horizontally."
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

现在,如果您运行以下命令,您应该会返回true

 const config = vscode.workspace.getConfiguration('myExtensionName');
 vscode.window.showInformationMessage(config.has('maximum').toString());
Run Code Online (Sandbox Code Playgroud)

我认为你不能单独获取工作区和用户设置,因为vscode的模式是工作区设置覆盖用户设置。
来自文档

VS Code 提供了两种不同的设置范围:

  • 用户设置 - 全局应用到您打开的任何 VS Code 实例的设置。
  • 工作区设置 - 存储在工作区内部的设置,仅在工作区打开时应用。

工作区设置覆盖用户设置。