【问题标题】:How can clang-format don't put if statements into one line?clang-format 如何不将 if 语句放在一行中?
【发布时间】:2017-04-23 08:04:36
【问题描述】:

就像下面的代码一样,我使用 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 语句包装成单行的规则?

我的问题不是那个问题(Clang format splits if statement body into multiple lines),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

【问题讨论】:

  • AllowShortBlocksOnASingleLine?这是您可能的重复:Clang format splits if statement body into multiple lines
  • 您也可以在代码块之前使用// clang-format off 命令,然后在代码块之后使用// clang-format on,这样您的代码块就不会被clang 格式化。因此,您自己格式化并将其从自动格式化中排除。这是您可能的重复(第二个答案):lang-format line breaks
  • @FirstStep AllowShortBlocksOnASingleLine 不起作用,并且每个 if 语句的 cmets-way 都过于冗长,我会继续寻找解决方案

标签: c++ clang-format


【解决方案1】:

您需要使用ColumnLimit。文档here。更多方法here

【讨论】:

  • ColumnLimit 是一个全局属性。它无法阻止手动换行的短线组合并保持长线不变。
猜你喜欢
  • 2018-06-23
  • 1970-01-01
  • 2021-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-16
  • 2012-12-11
  • 2019-02-09
相关资源
最近更新 更多