在VS Code中,如何根据给定目录中的文件获得JSON智能感知?

7
我的应用程序包括JSON文件。所有JSON文件中共同的一个属性是"type"。"type"属性的值必须是/templates/目录下任何handlebars(.hbs)文件的文件名。

我的目标是在输入JSON文件中给定"type"属性的值时获得智能感知。智能感知应该包括/templates/目录中所有handlebars文件的文件名列表。自动解决方案或提供值列表的解决方案都可以。

在VS Code配置或使用TypeScript中是否有方法实现这一目标?
1个回答

6

您可以在.vscode目录下的工作区settings.json中添加JSON模式。

以下是为ghooks设置添加智能感知到package.json的示例:

"json.schemas": [
    {
        "fileMatch": [
            "/package.json"
        ],
        "schema": {
            "type": "object",
            "properties": {
                "config": {
                    "properties": {
                        "ghooks": {
                            "type": "object",
                            "description": "Git hook configurations",
                            "properties": {
                                "applypatch-msg": {
                                    "type": "string"
                                },
                                "pre-applypatch": {
                                    "type": "string"
                                },
                                "post-applypatch": {
                                    "type": "string"
                                },
                                "pre-commit": {
                                    "type": "string"
                                },
                                "prepare-commit-msg": {
                                    "type": "string"
                                },
                                "commit-msg": {
                                    "type": "string"
                                },
                                "post-commit": {
                                    "type": "string"
                                },
                                "pre-rebase": {
                                    "type": "string"
                                },
                                "post-checkout": {
                                    "type": "string"
                                },
                                "post-merge": {
                                    "type": "string"
                                },
                                "pre-push": {
                                    "type": "string"
                                },
                                "pre-receive": {
                                    "type": "string"
                                },
                                "update": {
                                    "type": "string"
                                },
                                "post-receive": {
                                    "type": "string"
                                },
                                "post-update": {
                                    "type": "string"
                                },
                                "pre-auto-gc": {
                                    "type": "string"
                                },
                                "post-rewrite": {
                                    "type": "string"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
],

要获取特定属性值的智能感知,您可以查看此文档:https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not#oneof
对于您的用例,动态生成包含所有可能的模板名称的JSON架构并通过$schema属性或配置将架构包含在您的JSON文件中也可能很有用。
"json.schemas": [
    {
        "fileMatch": [
            "/json-files/*.json"
        ],
        "url": "./templates.schema.json"
    }
],

我不知道有没有vscode功能可以动态生成这样的模式。您可能想使用我在此扩展中使用的方法: https://github.com/skolmer/vscode-i18n-tag-schema - 一种使用节点模块动态生成模式文件的扩展。


1
那很好用。我使用这些示例创建了一个模式文件http://json-schema.org/example1.html。并且,我使用枚举来创建一个列表,其中包含我希望Intellisense显示给定属性的所有值https://spacetelescope.github.io/understanding-json-schema/reference/generic.html。 - edt

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接