【问题标题】:How to tell clang-format do not make short lambda expression on a single line?如何告诉 clang-format 不要在一行上制作简短的 lambda 表达式?
【发布时间】:2018-12-17 23:00:13
【问题描述】:
#include <iostream>

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

我想以这种方式保留我的简短 lambda 表达式。但是 clang-format 总是在一行中。

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-format 总是在一行中。有时一个简短的函数只有一行,clang-format也使它成为一行。

【问题讨论】:

    标签: clang-format


    【解决方案1】:

    我不知道这个选项是什么时候添加的,至少从 10.0 版本开始有AllowShortLambdasOnASingleLine。见official docs

    在您的情况下,您需要选项的值为InlineEmptyNone

    【讨论】:

      猜你喜欢
      • 2022-09-27
      • 2018-07-16
      • 2014-05-16
      • 2015-09-07
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      • 2022-01-23
      • 2017-10-30
      相关资源
      最近更新 更多