簡體   English   中英

如果它們太長,clang 格式會堆疊所有 if 語句參數

[英]clang-format stack all if-statement arguments if they are too long

我有一個if語句,它有幾個or ed 參數。 為了便於閱讀,我將它們垂直堆疊如下。

    if (health.flag_a || 
       health.flag_b ||
       health.flag_c ||
       health.flag_d ||
       health.flag_e ||
       health.flag_f ||
       health.flag_g ||
       health.flag_h ||
       health.flag_i ||
       health.flag_k) {
    do_something();
    return false;
}

clang-format將其轉換為:

if (health.flag_a || health.flag_b || health.flag_c || health.flag_d || health.flag_e || health.flag_f || health.flag_g || health.flag_h ||
    health.flag_i || health.flag_k) {
    do_something();
    return false;
}

如果它們不適合一行,我希望clang-format像上面一樣堆疊它們。 我希望BinPackArguments這樣做,但我沒有任何運氣。

這能實現嗎?

請參閱下面的我的.clang-format

謝謝你的幫助。

---
Language:        Cpp
# BasedOnStyle:  Google
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: true
AlignConsecutiveDeclarations: true
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Inline
AllowShortIfStatementsOnASingleLine: true
AllowShortLoopsOnASingleLine: true
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: true
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:   
  AfterClass:      false
  AfterControlStatement: false
  AfterEnum:       false
  AfterFunction:   false
  AfterNamespace:  false
  AfterObjCDeclaration: false
  AfterStruct:     false
  AfterUnion:      false
  BeforeCatch:     false
  BeforeElse:      false
  IndentBraces:    false
BreakBeforeBinaryOperators: None
BreakBeforeBraces: Attach
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
ColumnLimit:     145
CommentPragmas:  '^ IWYU pragma:'
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DerivePointerAlignment: true
DisableFormat:   false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IncludeCategories: 
  - Regex:           '^<.*\.h>'
    Priority:        1
  - Regex:           '^<.*'
    Priority:        2
  - Regex:           '.*'
    Priority:        3
IndentCaseLabels: true
IndentWidth:     4
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: false
MacroBlockBegin: ''
MacroBlockEnd:   ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 2
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: false
PenaltyBreakBeforeFirstCallParameter: 1
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
ReflowComments:  true
SortIncludes:    true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 2
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
Standard:        Auto
TabWidth:        4
UseTab:          ForIndentation
...

不是直接的,但您可以使用BreakBeforeBinaryOperators實現類似的BreakBeforeBinaryOperators 嘗試將此值設置為NonAssignment 這將使您的|| 但是,每個附加條件之前,而不是在它們之后。

我發現的唯一解決方案是// clang-format on注釋// clang-format on使用// clang-format off// clang-format on 例如

// clang-format off
if (health.flag_a || 
   health.flag_b ||
   health.flag_c ||
   health.flag_d ||
   health.flag_e ||
   health.flag_f ||
   health.flag_g ||
   health.flag_h ||
   health.flag_i ||
   health.flag_k) {
do_something();
return false;
}
// clang-format on

是的,這根本不是解決方案,但至少它可以防止您的堆疊條件被clang-format重新格式化。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM