代码之家  ›  专栏  ›  技术社区  ›  Makogan

VS代码API文档不清楚如何读取json自定义值

  •  0
  • Makogan  · 技术社区  · 7 年前

    我已经阅读了VS code API的在线文档和源代码,但我仍然不清楚如何读取JSON用户设置和workpsace设置。我试图寻找一些例子,但我找不到只显示如何做到这一点的东西。

    本质上,我希望设置中有一个称为“最大”的值。然后我想从TS scrypt读取最大值。

    文档让我在activate函数中写了以下两行:

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

    在包装中。我添加的json文件,在“贡献”下:

        "configuration": {
            "maximum":
            {
                "type": ["integer"],
                "default": 40,
                "description": "The level of alignment, how far the titles will extend horizontally"
            }
        }
    

    然而,我的输出为false。

    我尝试了多个不同的输出,唯一一次实现的是第一个函数的参数是“launch”,第二个参数是“configurations”。

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

    1 回复  |  直到 7 年前
        1
  •  4
  •   HaaLeo alireza javanmardi    7 年前

    你得到 False 作为输出,因为包中的configuration关键字。json仅表示 configuration contribution 扩展点。
    我建议您根据需要采用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."
                }
            }
        }
    }
    

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

     const config = vscode.workspace.getConfiguration('myExtensionName');
     vscode.window.showInformationMessage(config.has('maximum').toString());
    

    我认为您不能单独获得工作区和用户设置,因为vscode的模式是工作区设置覆盖用户设置。
    docs :

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

    • 用户设置-全局应用于您打开的VS代码的任何实例的设置。
    • 工作空间设置-存储在工作空间中的设置,仅在打开工作空间时应用。

    工作空间设置替代用户设置。