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

时间:2016-10-13 23:11:00

标签: intellisense visual-studio-code

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

我的目标是在JSON文件中键入给定“type”属性的值时获取intellisense。 intellisense应包括/ templates /目录中所有把手文件名的列表。无论是自动解决方案,还是我提供值列表的解决方案都很棒。

在VS Code配置或TypeScript中是否有办法实现这一目标?

1 个答案:

答案 0 :(得分:4)

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

这是为ghooks设置添加intellisense到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 - 使用节点模块动态生成模式文件的扩展。