clang-format 如何不将 if 语句放入一行?

问题描述 投票:0回答:2

就像下面的代码一样,我使用 clang-format 来自动格式化我的代码

if(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]
   || fabs(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]) < 1.0)
{
    *beatsCont -=1;
}

无论我设置什么 .clang-formt 文件,它总是这样格式化:

if(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1] || fabs(detectBeats[*beatsCont-2] > detectBeats[*beatsCont-1]) < 1.0)
{
    *beatsCont -=1;
}

如何设置规则不将 if 语句包装到 oneline 中?

我的问题不是那个问题(Clang 格式将 if 语句主体分成多行),b/c 我的 if 语句被包装,而不是主体

这是我的 .clang 格式文件

AccessModifierOffset : -4
AllowAllParametersOfDeclarationOnNextLine : false
AlignEscapedNewlinesLeft : false
AlignOperands:   true
AlignTrailingComments : true
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: false
AllowShortIfStatementsOnASingleLine : true
AllowShortLoopsOnASingleLine: true
BinPackArguments : false
BinPackParameters : false
BreakBeforeBraces : Linux
ColumnLimit: 0
CommentPragmas: '^ *\/\/'
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
IndentWidth : 4
KeepEmptyLinesAtTheStartOfBlocks : false
Language : Cpp
MaxEmptyLinesToKeep : 2
ObjCBlockIndentWidth : 2
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList : false
PointerAlignment: Right
ReflowComments:  true
SortIncludes: true
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators : true
SpaceBeforeParens : ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments : 1
SpacesInAngles:  false
SpacesInContainerLiterals : false
SpacesInParentheses : false
SpacesInSquareBrackets: false
Standard: Cpp11
UseTab : Never
c++ clang-format
2个回答
1
投票

您需要使用

ColumnLimit
。医生这里。更多方法这里


0
投票

如果有人像我一样尝试几个小时才能找到答案,这里是配置:

BreakBeforeBinaryOperators:非赋值

以下是有关更多信息的文档:https://clang.llvm.org/docs/ClangFormatStyleOptions.html#breakbeforebinaryoperators

© www.soinside.com 2019 - 2024. All rights reserved.