.clang格式是否可以在一行功能前中断?

时间:2019-12-11 12:36:35

标签: format clang clang-format

我在documentation中找不到任何内容,即使BreakBeforeBraces: Allman格式的单行函数也已经分解了

void foo() { bar(); }

我想要类似的东西

void foo()
{
    bar();
}

我希望这样做是为了组织代码和保持一致,因为这是每个多行函数的外观。

你能帮我吗?

3 个答案:

答案 0 :(得分:2)

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

AllowShortFunctionsOnASingleLine: Empty

答案 1 :(得分:1)

要回答问题:

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

最近:

BreakBeforeBraces: Allman
ColumnLimit: '0'

这将使您的格式化函数正确无误,并在至少两行上拉动时正确格式化它们。

答案 2 :(得分:0)

  • 是的,您可以这样做。 最简单的方法是设置
BreakBeforeBraces: Stroustrup
  • 第二,您可以通过在true中设置SplitEmptyFunction来完成此操作。例如
"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

如果为 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