You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Clang-Format实现嵌套数组元素逐行换行格式化?

实现嵌套std::vector初始化的层级化逐行格式化(Clang-Format配置)

需求与现状

期望的嵌套vector初始化格式(层级分明,逐行换行):

std::vector<std::vector<std::vector<int>>> input = {
        {
            {1, 3, 5, 7}, 
            {10, 11, 16, 20}, 
            {23, 30, 34, 60}
        }, 
        {
            {1, 3, 5, 7}, 
            {10, 11, 16, 20}, 
            {23, 30, 34, 60}
        }
    };

Clang-Format 18默认格式化后的效果(可读性差):

std::vector<std::vector<std::vector<int>>> input = {{{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}, {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}};

添加AlignArrayOfStructures: Left后的效果(仍未达到预期):

std::vector<std::vector<std::vector<int>>> input = {
        {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}},
        {{1, 3, 5, 7}, {10, 11, 16, 20}, {23, 30, 34, 60}}
    };

关键配置调整

需要在现有配置基础上添加/修改以下项,实现层级化换行:

  • BreakListOfLists: true:强制嵌套初始化列表的每个层级单独换行
  • BraceWrapping -> AfterInitializerList: true:让初始化列表的左大括号后换行,保证结构分层
  • AllowShortArraysOnASingleLine: Never:禁止短容器初始化列表挤在同一行

调整后的完整配置文件

BreakBeforeBraces: Custom
BraceWrapping:
  AfterClass:      true
  SplitEmptyFunction: false
  AfterControlStatement: true
  AfterFunction:   true
  AfterNamespace:  true
  AfterObjCDeclaration: true
  AfterStruct:     true
  AfterUnion:      true
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false
  AfterCaseLabel:  true
  AfterInitializerList: true # 新增:初始化列表大括号后换行
InsertNewlineAtEOF: true
IncludeBlocks: Merge
#Clang11?: IndentCaseBlocks: true
IndentCaseLabels: true
#Clang11: IndentExternBlock: true
IndentPPDirectives: None
IndentWidth: 4
AllowShortFunctionsOnASingleLine: Empty
AllowShortArraysOnASingleLine: Never # 新增:禁止短数组单行显示
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
PenaltyBreakAssignment: 16
PenaltyBreakBeforeFirstCallParameter: 2
PenaltyBreakString: 64
PenaltyExcessCharacter: 0
PenaltyReturnTypeOnItsOwnLine: 0
PointerAlignment: Right
ReflowComments: true
SortIncludes: false
IncludeCategories:
  - Regex: '"*PlatformIO.h"'
    Priority: -5
  - Regex: '"*TextIOTypes.h"'
    Priority: -4
  - Regex: '<[Ww]indows.h>'
    Priority: -3
  - Regex: '<*\/.h>'
    Priority: -2
  - Regex: '"*\/.h"'
    Priority: 1

SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: false
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: false
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
UseCRLF: false
UseTab: Never
IndentAccessModifiers: true
AlignArrayOfStructures: Left
AlignConsecutiveAssignments: AcrossEmptyLinesAndComments
ColumnLimit: 80
RemoveBracesLLVM: false
RemoveEmptyLinesInUnwrappedLines: true
RemoveSemicolon: true
BreakListOfLists: true # 新增:嵌套列表强制换行

配置说明

  1. BreakListOfLists: true:识别嵌套的容器初始化列表结构,为每个层级的子容器添加换行,直接实现三层vector的分层显示。
  2. BraceWrapping.AfterInitializerList: true:确保最外层初始化列表的左大括号后自动换行,让整体结构更清晰。
  3. AllowShortArraysOnASingleLine: Never:避免将子vector的初始化内容(如{1,3,5,7})与其他子vector挤在同一行,保证每个子容器单独占一行。

内容的提问来源于stack exchange,提问作者Natsch28

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.16 14:53:11