首页 > 解决方案 > 通过 clang-format 对几乎精确的结构进行不同的格式化

问题描述

我想使用 clang-format 来自动格式化我的源代码,但我无法理解它在类似结构下的行为。请参阅以下 C++ 代码:

static someStructLongName structInstanceLong{
    Method1LongLong,       
    structInstanceMethod1, 
    structInstanceMethod1,
    structInstanceMethod3,
    structInstanceMethod4,
    structInstanceMethod5,
    structInstanceMethod6,
    structInstanceMethod7,
    structInstanceMethod8
};

static someStructLongName structInstanceLong{
    Method1,
    structInstanceMethod1,
    structInstanceMethod1,
    structInstanceMethod3,
    structInstanceMethod4,
    structInstanceMethod5,
    structInstanceMethod6,
    structInstanceMethod7,
    structInstanceMethod8
};

当我使用我的自定义配置文件(附在帖子底部)使用 clang-format 对其进行格式化时,我得到以下输出:

static someStructLongName structInstanceLong{Method1LongLing,       structInstanceMethod1, structInstanceMethod1,
                                             structInstanceMethod3, structInstanceMethod4, structInstanceMethod5,
                                             structInstanceMethod6, structInstanceMethod7, structInstanceMethod8};

static someStructLongName structInstanceLong{Method1,
                                             structInstanceMethod1,
                                             structInstanceMethod1,
                                             structInstanceMethod3,
                                             structInstanceMethod4,
                                             structInstanceMethod5,
                                             structInstanceMethod6,
                                             structInstanceMethod7,
                                             structInstanceMethod8};

第二个选项是首选,但我不明白哪个规则是造成这种行为的原因,所以要改变什么来强制所有结构都得到这个结果。

有趣的事情 - 当我使用这个在线工具时: https ://zed0.co.uk/clang-format-configurator/ 加载我的配置和上面的代码一切看起来都像预期的那样。

在 clang-format 自定义配置下面:

---
AccessModifierOffset: '-4'
AlignEscapedNewlinesLeft: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: 'NonAssignment'
BraceWrapping:
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     true
  BeforeElse:      true
  IndentBraces:    false
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: 'false'
BreakConstructorInitializersBeforeComma: 'true'
ColumnLimit: '120'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'true'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
ExperimentalAutoDetectBinPacking: 'false'
IndentCaseLabels: 'false'
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PointerAlignment: Left
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInParentheses: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never

...

标签: c++clang-format

解决方案


我使用了一个名为 clang-format configurator 的工具,这将像您的第二个选项一样格式化您的代码

BasedOnStyle:铬

AlignEscapedNewlinesLeft: '真'

AlignTrailingComments:“真”

AllowAllParametersOfDeclarationOnNextLine: '假'

AllowShortBlocksOnASingleLine: 'true'

AllowShortFunctionsOnASingleLine:全部

BreakBeforeBinaryOperators:'真'

ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'

缩进宽度:'1'

IndentWrappedFunctionNames: 'true'

KeepEmptyLinesAtTheStartOfBlocks:'假'

SpaceBeforeAssignmentOperators:“假”

...


推荐阅读