如何告诉clang-format不要在一行上做短的lambda表达式?

时间:2018-07-10 07:28:53

标签: clang-format

#include <iostream>

int main()
{
    auto func = []() { 
        std::cout << "hello, world" << std::endl; 
    };
    func();
}

我想以这种方式保留我的短lambda表达式。但是clang格式总是将其放在一行上。

auto func = []() { std::cout << "hello, world" << std::endl; };

我已经阅读了文档,但是找不到正确的设置。这是我的配置:

BasedOnStyle:   LLVM
Language:   Cpp
AccessModifierOffset:   -4
AlignAfterOpenBracket:  Align
AlignConsecutiveAssignments:    true
AlignConsecutiveDeclarations:   true
AlignEscapedNewlines: Left
AlignOperands:  true
AlignTrailingComments:  true
AllowAllParametersOfDeclarationOnNextLine:  true
AllowShortBlocksOnASingleLine:  false
AllowShortCaseLabelsOnASingleLine:  false
AllowShortFunctionsOnASingleLine:   Empty
AllowShortIfStatementsOnASingleLine:    true
AllowShortLoopsOnASingleLine:   false
AlwaysBreakAfterDefinitionReturnType:   None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings:  false
AlwaysBreakTemplateDeclarations:    false
BinPackArguments:   false
BinPackParameters:  false
BraceWrapping:   
  AfterClass:   true
  AfterControlStatement: true   
  AfterEnum: true   
  AfterFunction:    true
  AfterNamespace:   false
  AfterObjCDeclaration: false
  AfterStruct:  true
  AfterUnion:   false
  BeforeCatch:  true
  BeforeElse:   true
  IndentBraces: false
BreakBeforeBinaryOperators: NonAssignment
BreakBeforeBraces:  Custom
BreakBeforeTernaryOperators:    true
BreakConstructorInitializersBeforeComma:    true
ColumnLimit:    120
CommentPragmas: '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth:  4
ContinuationIndentWidth:    4
Cpp11BracedListStyle:   false
DerivePointerAlignment: false
DisableFormat:  false
ExperimentalAutoDetectBinPacking:   false
ForEachMacros:  [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories: 
  - Regex:  '^"(llvm|llvm-c|clang|clang-c)/'
    Priority:   2
  - Regex:  '^(<|"(gtest|isl|json)/)'
    Priority:   3
  - Regex:  '.*'
    Priority:   1
IndentCaseLabels:   true
IndentWidth:    4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks:   true
MacroBlockBegin:    ''
MacroBlockEnd:  ''
MaxEmptyLinesToKeep:    1
NamespaceIndentation:   Inner
ObjCBlockIndentWidth:   4
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList:    true
PenaltyBreakBeforeFirstCallParameter:   19
PenaltyBreakComment:    300
PenaltyBreakFirstLessLess:  120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine:  60
PointerAlignment:   Right
ReflowComments: true
SortIncludes:   true
SortUsingDeclarations: true
SpaceAfterCStyleCast:   false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens:  ControlStatements
SpaceInEmptyParentheses:    false
SpacesBeforeTrailingComments:   1
SpacesInAngles: false
SpacesInContainerLiterals:  true
SpacesInCStyleCastParentheses:  false
SpacesInParentheses:    false
SpacesInSquareBrackets: false
Standard:   Cpp11
TabWidth:   4
UseTab: Never

我的clang格式版本是7.0.0。如果lambda表达式具有多于两行,则效果很好。但是当它只有一行时。 clang格式始终使它成一行。有时,短函数仅占一行,而clang格式也使它成一行。

1 个答案:

答案 0 :(得分:0)

我不知道何时添加了此选项,至少从10.0版本开始就有AllowShortLambdasOnASingleLine。参见official docs

根据您的情况,您需要将选项的值设置为InlineEmptyNone