如何配置clang-format使指针数组初始化保持换行格式?
问题描述
我希望保持指针数组初始化的换行格式:
ID3D11SamplerState* samplers[] = { linear_sampler.Get(), nullptr, nullptr, nullptr, nullptr };
但clang-format会将所有元素合并为一行:
ID3D11SamplerState* samplers[] = {linear_sampler.Get(), nullptr, nullptr, nullptr, nullptr};
我的clang-format配置如下(已尝试AlignArrayOfStructures: Left,但因是指针数组而非结构体数组,无效果):
--- Language: Cpp BasedOnStyle: LLVM AccessModifierOffset: -4 AlignOperands: false AlignTrailingComments: false AllowAllArgumentsOnNextLine: false AllowAllParametersOfDeclarationOnNextLine: false AllowShortBlocksOnASingleLine: false AllowShortEnumsOnASingleLine: false AllowShortFunctionsOnASingleLine: None AllowShortIfStatementsOnASingleLine: false AllowShortLambdasOnASingleLine: None AlwaysBreakAfterDefinitionReturnType: false AlwaysBreakTemplateDeclarations: Yes BraceWrapping: AfterCaseLabel: false AfterClass: false AfterControlStatement: false AfterEnum: false AfterFunction: false AfterNamespace: false AfterStruct: false AfterUnion: false AfterExternBlock: false BeforeCatch: true BeforeElse: true BeforeLambdaBody: false BeforeWhile: true SplitEmptyFunction: true SplitEmptyRecord: true SplitEmptyNamespace: true BreakBeforeBraces: Custom BreakConstructorInitializers: AfterColon BreakInheritanceList: AfterColon ColumnLimit: 400 ContinuationIndentWidth: 4 IncludeCategories: - Regex: ^<.* Priority: 1 - Regex: ^".* Priority: 2 - Regex: .* Priority: 3 IncludeIsMainRegex: ([-_](test|unittest))?$ IndentCaseLabels: true IndentWidth: 4 IndentRequires: false KeepEmptyLinesAtTheStartOfBlocks: false MaxEmptyLinesToKeep: 2 NamespaceIndentation: All PointerAlignment: Left AlignArrayOfStructures : Left SpaceAfterTemplateKeyword: false SpaceBeforeCtorInitializerColon: false SpaceBeforeParens: Never SpaceBeforeRangeBasedForLoopColon: false SpacesInContainerLiterals: false Standard: Auto TabWidth: 4 UseTab: Always ...
解决方案
你需要在clang-format配置中添加或修改以下选项:
AllowShortArraysOnASingleLine: false
这个选项会强制禁止将短数组的初始化内容放在同一行,无论数组元素数量多少,都会保持每个元素单独换行的格式。
你的ColumnLimit设置为400,远大于当前数组初始化的行宽,clang-format会默认尝试将内容压缩到一行内。配合AllowShortArraysOnASingleLine: false就能彻底强制数组初始化的换行格式。
修改后的配置加入该选项后,clang-format就会保留你想要的换行格式了。
内容的提问来源于stack exchange,提问作者user3001150
相关产品推荐
相关产品推荐

