通过clang格式对几乎精确的结构进行不同的格式化

时间:2018-11-15 13:22:23

标签: c++ 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格式对其进行格式化时,得到以下输出:

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格式的自定义配置:

---
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

...

1 个答案:

答案 0 :(得分:0)

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

BasedOnStyle:铬

AlignEscapedNewlinesLeft:'true'

AlignTrailingComments:'true'

AllowAllParametersOfDeclarationOnNextLine:'false'

AllowShortBlocksOnASingleLine:'true'

AllowShortFunctionsOnASingleLine:全部

BreakBeforeBinaryOperators:'true'

ConstructorInitializerAllOnOneLineOrOnePerLine:'true'

IndentWidth:'1'

IndentWrappedFunctionNames:'true'

KeepEmptyLinesAtTheStartOfBlocks:'false'

SpaceBeforeAssignmentOperators:'false'

...