英文:
What JSON formatter does Visual Studio Code use to format JSON?
问题
-
Visual Studio Code内置了JSON工具,包括一个格式化程序:https://code.visualstudio.com/docs/languages/json#_formatting
-
它是否可用作Visual Studio Code之外的命令行工具?
-
它与
jq
有什么不同?
英文:
Visual Studio Code has built-in JSON tools, including a formatter: https://code.visualstudio.com/docs/languages/json#_formatting
- What underlying JSON formatter does Visual Studio Code use?
- Is it available to use as a command line tool outside of Visual Studio Code?
- How does it differ from
jq
?
答案1
得分: 1
以下是翻译的内容:
这是查找你要找的信息的方法:
- 查找你感兴趣的扩展的源代码。在这种情况下,它是内置的 VS Code 扩展,所以请参阅 https://github.com/microsoft/vscode/tree/main/extensions。在那里,你会看到 json-language-features 子目录。
- 打开 https://github.dev/microsoft/vscode
- 打开搜索视图并展开搜索详细信息
- 在“要包括的文件”中,输入“extensions/json-language-features/**/*.ts”。
- 启用正则表达式模式,在搜索字段中输入
\bformat\b
。
- 了解在 jsonServer.ts 中有一个
onFormat
函数,它调用languageService.format
,其中 languageService 来自getLanguageService
,后者来自vscode-json-languageservice
包。 - 查看 microsoft/vscode-json-languageservice/jsonLanguageService.ts 并了解
getLanguageService
返回一个包含format: (document: TextDocument, range: Range, options: FormattingOptions) => format(document, options, range),
的对象,其中format
来自import { format } from './utils/format';
。 - 查看 microsoft/vscode-json-languageservice/utils/format.ts 并了解
format
调用formatJSON
,其中来自import { format as formatJSON, Range as JSONCRange } from 'jsonc-parser';
。 - 查看 microsoft/vscode-json-languageservice/package.json 的
"dependencies"
字段,并看到它依赖于 thejsonc-parser
package。
所以来回答你的问题:
> 1. Visual Studio Code 使用什么底层的 JSON 格式化程序?
https://www.npmjs.com/package/jsonc-parser
> 2. 它可以作为 Visual Studio Code 之外的命令行工具使用吗?
可以。以与 microsoft/vscode-json-languageservice 使用它相同的方式使用它。将其添加为 JS 项目的依赖,就像你会依赖于任何其他 NPM 包一样。
> 3. 它与 jq 有什么不同?https://stedolan.github.io/jq/
首先,它支持带注释的 JSON,而 jq 不支持(参见 https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json 和 https://stackoverflow.com/q/33352930/11107541)。
英文:
Here's how to find the info you're looking for
- Find the source code for the extension you're interested in. In this case, it's a builtin VS Code extension, so see https://github.com/microsoft/vscode/tree/main/extensions. There, you'll see the json-language-features subdirectory.
- Open https://github.dev/microsoft/vscode
- Open the Search view and expand the Search Details
- In "File to include", put "extensions/json-language-features/**/*.ts".
- Enable regex mode and in the search field, put
\bformat\b
.
- Learn that in jsonServer.ts, there's an
onFormat
function that makes a call tolanguageService.format
, where languageService comes fromgetLanguageService
, which comes from thevscode-json-languageservice
package. - Look at microsoft/vscode-json-languageservice/jsonLanguageService.ts and learn that
getLanguageService
returns an object containingformat: (document: TextDocument, range: Range, options: FormattingOptions) => format(document, options, range),
, whereformat
comes fromimport { format } from './utils/format';
. - Look at microsoft/vscode-json-languageservice/utils/format.ts and learn that
format
callsformatJSON
, where that comes fromimport { format as formatJSON, Range as JSONCRange } from 'jsonc-parser';
. - Look at microsoft/vscode-json-languageservice/package.json's
"dependencies"
field, and see that it depends on thejsonc-parser
package.
So to answer your questions:
> 1. What underlying JSON formatter does Visual Studio Code use?
https://www.npmjs.com/package/jsonc-parser
> 2. Is it available to use as a command line tool outside of Visual Studio Code?
Yes. Use it in the same way that microsoft/vscode-json-languageservice uses it. Add it as a dependency of a JS project in the same way you'd depend on any other NPM package.
> 3. How does it differ from jq? https://stedolan.github.io/jq/
Well, for one thing, it supports JSON with comments, which jq doesn't (see https://github.com/stedolan/jq/wiki/FAQ#processing-not-quite-valid-json and https://stackoverflow.com/q/33352930/11107541).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论