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

In VS Code, how can I get JSON intellisense based on the files in a given directory?

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

我的目标是在 JSON 文件中键入给定 "type" 属性 的值时获得智能感知。智能感知应在 /templates/ 目录中包含所有车把文件名的列表。自动解决方案或我提供值列表的解决方案都很棒。

在 VS 代码配置中或使用 TypeScript 是否有办法实现这一点?

您可以将 JSON 模式添加到 .vscode 目录中的工作区 settings.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 [=34] 将架构包含在您的 JSON 文件中也可能很有用=] 或通过配置:

"json.schemas": [
    {
        "fileMatch": [
            "/json-files/*.json"
        ],
        "url": "./templates.schema.json"
    }
],

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