gpt4 book ai didi

c++ - C++ 中结构数组的 clang 格式奇怪缩进

转载 作者:行者123 更新时间:2023-11-30 04:52:29 26 4
gpt4 key购买 nike

我正在尝试使用 clang-format(在 VS 代码中)来格式化我的 C++ 文件并将其配置为我喜欢的样式。对于结构数组(对于 getopts),它会添加大量额外的空格并弄乱大括号:

我将在此查询的末尾附加我的 .clang-format

这是我希望数组显示的方式:

int main()
{
const struct option longopts[]=
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}
};
}

这是它实际出现的样子:

int main()
{
const struct option longopts[] =
{
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0}};
}

我的 .clang 格式文件包含:

BasedOnStyle: LLVM
IndentWidth: 2
AlignAfterOpenBracket: Align
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortFunctionsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
BinPackParameters: true
BreakBeforeBraces: Custom
BraceWrapping:
AfterClass: true
AfterControlStatement: true
AfterEnum: true
AfterFunction: true
AfterNamespace: true
AfterObjCDeclaration: true
AfterStruct: true
AfterUnion: true
AfterExternBlock: true
BeforeCatch: true
BeforeElse: true
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BreakConstructorInitializers: AfterColon
ColumnLimit: 0
ConstructorInitializerAllOnOneLineOrOnePerLine: false
IndentCaseLabels: true
KeepEmptyLinesAtTheStartOfBlocks: true
NamespaceIndentation: All
PointerAlignment: Right
SortIncludes: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesInContainerLiterals: false
SpacesInAngles: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseTab: Never

欢迎任何解决方案!

最佳答案

我不确定是否可以实现您想要的,但有时最简单的方法是略微修改源文件以支持 clang-format 实用程序。首先,您需要在格式文件中添加一个 ContinuationIndentWidth: 2 选项。然后在数组的最后一项之后添加一个逗号:

{nullptr, 0, nullptr, 0}, // <---

最后将第一个花括号移动到与数组名称相同的行上。生成的文件将如下所示:

int main()
{
const struct option longopts[] = {
{"log-file", required_argument, 0, LOGFILE},
{"log-level", required_argument, 0, LOGLEVEL},
{nullptr, 0, nullptr, 0},
};
}

运行 clang-format 将保持原样。在 LLVM 快照构建 LLVM-9.0.0-r351376-win64.exe 的 clang 格式上进行测试。

关于c++ - C++ 中结构数组的 clang 格式奇怪缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54348532/

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