我在 Visual Studio Code 中保存了一個 json 檔案后,它會自動格式化檔案,陣列垂直顯示(通過添加換行符)。由于我有巨大的整數陣列,因此幾乎不可能快速查看檔案。例子:
{
"big_integer_array": [
12,
15,
13,
1,
5,
8,
15,
14,
12,
...
],
"this_value_is_in_line_million": true
}
我怎樣才能告訴程式,像這樣水平顯示數字陣列:
{
"big_integer_array": [12,15,13,1,5,8,15,14,12, ...],
"this_value_is_in_line_three": true
}
解決方案
- 使用擴展 esbenp.prettier-vscode
- 將 prettier.printwidth 設定為 99999999999

uj5u.com熱心網友回復:
目前無法自定義 VS Code 原生格式化程式以按照您建議的方式格式化陣列,但是,更漂亮的格式化程式會按照您的建議配置它們。Prettier 是一種廣泛使用的格式化程式,并且被絕大多數 VS Code 用戶使用。
官方 prettier 擴展的 ID 是:esbenp.prettier-vscode
重要的是要注意Prettier將為您處理具有單一型別輸入的陣列。因此,如果一個陣列全是數字,或者全是字串;如果一個陣列由混合型別、數字、物件、陣列中的陣列、字串、布林值等組成……那么更漂亮的陣列格式取決于您最初放置括號的方式。
使用 Prettier 格式化混合陣列
為了這個例子,假設在 json 檔案中有以下陣列。
{
"obj": {
"Array": [
"Apple",
"Broccoli",
"Coconut",
"Orange",
"Carrot",
{
"foo": "apple"
}
]
}
}
如果更改陣列中的括號,使陣列如下所示:
{
"obj": {
"Array": [
"Apple",
"Broccoli",
"Coconut",
"Orange",
"Carrot",
{"foo": "apple"}
]
}
}
Your basically telling prettier that you don't want to break up your embedded objects and arrays vertically, but rather, you want to keep them horizontally. Formatting the example above (using prettier) will result in your file looking like the example below:
{
"obj": {
"Array": ["Apple", "Broccoli", "Coconut", "Orange", "Carrot", { "foo": "apple" }]
}
}
However, if you format your array like this:
{
"obj": {
"Array": ["Apple", "Broccoli", "Coconut", "Orange", "Carrot", {
"foo": "apple"
}]
}
Then when you format the above example using prettier, you will produce the output below:
{
"obj": {
"Array": [
"Apple",
"Broccoli",
"Coconut",
"Orange",
"Carrot",
{
"foo": "apple"
}
]
}
}
Also note than the following settings in your VS Code's settings.json file can affect how prettier formats JSON as well:
"prettier.useTabs": true|false(says to use tab, or spaces)"prettier.tabWidth": Numeric Value(Sets tab spacing qty)"prettier.printWidth": Numeric Value(Sets line length)"prettier.bracketSpacing": True|False(Adds/Removes spacing in brackets)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/449970.html
標籤:视觉工作室代码
