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

edt*_*edt 5 intellisense visual-studio-code

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

我的目标是在 JSON 文件中键入给定“类型”属性的值时获得智能感知。智能感知应包含 /templates/ 目录中所有把手文件名的列表。无论是自动解决方案,还是我提供值列表的解决方案都会很棒。

有没有办法在 VS Code 配置或 TypeScript 中实现这一点?

Ste*_*fen 6

您可以将 JSON 架构添加到目录settings.json中的工作区.vscode

这是一个向 package.json 添加智能感知以进行ghooks设置的示例:

"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"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
],
Run Code Online (Sandbox Code Playgroud)

要获取特定属性值的智能感知,您可以查看此文档:https://github.com/json-schema/json-schema/wiki/anyOf,-allOf,-oneOf,-not#oneof

$schema对于您的用例,动态生成包含所有可能的模板名称的 JSON 模式并通过属性或配置将模式包含在 JSON 文件中也可能很有用:

"json.schemas": [
    {
        "fileMatch": [
            "/json-files/*.json"
        ],
        "url": "./templates.schema.json"
    }
],
Run Code Online (Sandbox Code Playgroud)

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