.clang-format 有没有办法在一行函数之前中断?

Is there a way with .clang-format to break before one line function?

我在 documentation 中找不到任何内容,甚至 BreakBeforeBraces: Allman 格式的单行函数我已经拆分为

void foo() { bar(); }

我想要类似于

的东西
void foo()
{
    bar();
}

我想要它来组织代码和统一性,因为这是每个多行函数的样子。

你能帮帮我吗?

  • 是的,你可以做到。 最简单的方法是设置
BreakBeforeBraces: Stroustrup
  • 其次,您可以通过在 SplitEmptyFunction 中设置 true 来完成此操作。举个例子
"BraceWrapping":
        "AfterClass":             false
        "AfterControlStatement":  false
        "AfterEnum":              false
        "AfterFunction":          false
        "AfterNamespace":         false
        "AfterObjCDeclaration":   false
        "AfterStruct":            false
        "AfterUnion":             false  
        "BeforeCatch":            false
        "BeforeElse":             false
        "IndentBraces":           false
        "SplitEmptyFunction":     true <-- set this as true
        "SplitEmptyRecord":       true
        "SplitEmptyNamespace":    true

如果是输出将是

void foo()
{
    bar();
}

但如果它是 false 输出将是

void foo(){
    bar();
}

Source

编辑 -: 将你的 clang 文件编辑成这样

BasedOnStyle:  WebKit
TabWidth:        4
IndentWidth:     4
UseTab:          Always
ColumnLimit: 100

DisableFormat:   false
Standard: Cpp11

AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false

BraceWrapping: {
    AfterClass: 'true'
    AfterControlStatement: 'true'
    AfterEnum : 'true'
    AfterFunction : 'true'
    AfterNamespace : 'true'
    AfterStruct : 'true'
    AfterUnion : 'true'
    BeforeCatch : 'true'
    BeforeElse : 'true'
    IndentBraces : 'false'
    AfterExternBlock : 'true'
    SplitEmptyFunction : 'false'
    SplitEmptyRecord : 'false'
    SplitEmptyNamespace : 'true'
}

BreakAfterJavaFieldAnnotations: true
BreakBeforeInheritanceComma: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakStringLiterals: true

CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
SpaceBeforeCpp11BracedList: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: false
FixNamespaceComments: true
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
JavaScriptQuotes: Double
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000

PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceAfterTemplateKeyword: true
SpaceBeforeInheritanceColon: true

SortUsingDeclarations: true
SortIncludes: true

ReflowComments: false

IncludeBlocks: Preserve
IndentPPDirectives: AfterHash

Source

回答问题:

使用 .clang 格式文件无法实现此特定行为。对所有希望在这里找到方法的人表示抱歉,我希望至少能为您节省一些时间。

最近的:

BreakBeforeBraces: Allman
ColumnLimit: '0'

这将使您的格式化函数保持正确,并且如果它们被拉伸到至少 2 行,它们的格式也会正确。

要在单独的行上有一个简短的函数体,请将其添加到 .clang-format 文件中:

AllowShortFunctionsOnASingleLine: Empty