我刚刚开始使用 Visual Studio Code。在我的公司,他们使用 Whitesmiths 缩进样式,但我找不到在 VSC 上设置它的方法。 我用 C 编程,所以我安装了 C/C++ 插件。在此,“缩进:大括号”选项看起来像是适合我的选项,但它似乎不起作用。
我的 json 看起来像这样:
"C_Cpp.vcFormat.indent.braces": true,
"editor.detectIndentation": false,
"editor.tabSize": 3,
我将 detector.Indentation 设置为 false,因为我不希望它干扰 indent.braces。 这是用户 json。我没有修改工作区或文件夹设置。
但是,我的代码仍然是这样的
void main()
{
if(condition)
{
do something
}
else
{
whatever
}
}
虽然它应该看起来像这样
void main()
{
if(condition)
{
do something
}
else
{
whatever
}
}
我错过了什么?有没有办法在不使用插件的情况下从 VSC 设置中进行设置?
PFE32 是一个古老的编辑器,会以这种风格缩进。更新的 Code::Blocks 编辑器也可以处理它。使用 Astyle 重新格式化为该样式。
顺便说一句,这是与原始怀特史密斯工具链一起使用的样式。它是第一个商用 C 编译器。这就是上帝用C写的风格。它并不难看。对于语言的巴科斯-诺尔定义来说,它是正确的、恰当的。很漂亮,跟我一样。 ;-D
这篇文章是针对 C# 的,但它对我使用 Visual Studio C++ 格式化程序有用:
我的完整 .json 文件:
{
"[c]": {
"editor.defaultFormatter": "ms-vscode.cpptools"
},
"editor.autoClosingQuotes": "never",
"editor.autoClosingBrackets": "never",
"workbench.list.automaticKeyboardNavigation": false,
"workbench.settings.openDefaultKeybindings": true,
"update.enableWindowsBackgroundUpdates": false,
"update.mode": "manual",
"C_Cpp.formatting": "vcFormat",
"C_Cpp.vcFormat.indent.braces": true
}
更新:
从版本 1.82 开始,这似乎不再起作用,特别是“C_Cpp.vcFormat.indent.braces”:似乎不再有任何影响。看看通用格式化程序规范(https://code.visualstudio.com/api/language-extensions/language-configuration-guide)似乎没有任何方法可以在大括号处发生缩进,只在它之后上线。
所以现在最好的选择是
"editor.autoIndent": "keep"
这或多或少让你负责自己的缩进,但至少可以防止编辑器添加不需要的缩进。