gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:27:27 25 4
gpt4 key购买 nike

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

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

VS Code 配置或 TypeScript 是否有办法实现这一点?

最佳答案

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

这是一个将 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 - 使用节点模块动态生成架构文件的扩展。

关于intellisense - 在 VS Code 中,如何根据给定目录中的文件获取 JSON 智能感知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40032498/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com