Clang 格式将方法调用和语句拆分为多行

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

我在 Windows 上使用 clang-format 版本 18。

在大多数情况下,我们希望方法调用中的所有参数都位于同一行,但在复杂的情况下,我们希望它们位于不同的行并对齐。问题在于 clang-format 基于行长度的“复杂”。我们希望能够根据参数的数量来分割行。例如,如果方法调用有 5 个参数,我们希望它们分成单独的行,即使它们位于一行上也不会超过行长度。但是具有两个参数的方法会将它们全部放在同一行。

复合语句也有同样的情况。我们想要这样的声明 a = b || c || d || e; 根据行的复杂程度而不是行的长度来划分行。

我不确定当前是否可以使用 clang-format,但也许我错过了一些东西。

如果不可能,有人可以向我指出某种论坛来提出 clang-format 的增强请求吗?谢谢

编辑: 我尝试将columnLimit设置为0。文档说“列限制为0意味着没有列限制。在这种情况下,clang-format将尊重语句中输入的换行决定,除非它们与其他规则相矛盾。”。这两句话互相矛盾。

将列限制设置为 0 会导致这组行:

  bool result = jobControls->AnyOutputFilesPresent(m_panelExcitations) ||
                jobControls->AnyOutputFilesPresent(m_saGraphs) ||
                jobControls->AnyOutputFilesPresent(m_audioSignals);

改为这一行

  bool result = jobControls->AnyOutputFilesPresent(m_panelExcitations) || jobControls->AnyOutputFilesPresent(m_saGraphs) || jobControls->AnyOutputFilesPresent(m_audioSignals);

这是我的配置文件:

#
#Standard options
#
BasedOnStyle: Google
EmptyLineBeforeAccessModifier: Always
EmptyLineAfterAccessModifier: Never
IndentAccessModifiers: false
IndentWidth: 2
Language: Cpp
LineEnding: LF
PointerAlignment: Left
QualifierAlignment: Left
SpaceBeforeRangeBasedForLoopColon: true
SpaceBeforeSquareBrackets: false
SpaceInEmptyParentheses: false
#SpacesBeforeTrailingComments: 3
SpacesInAngles: Never
SpacesInContainerLiterals: false
SpacesInParens: Never
UseTab: Never

#
# Bracing options
#
BreakBeforeBraces: Custom
BraceWrapping:
  AfterCaseLabel: false
  AfterClass: false
  AfterEnum: false
  AfterFunction: true
  AfterNamespace: true
  AfterStruct: false
  AfterUnion: true
  AfterExternBlock: true
  BeforeCatch: true
  BeforeElse: true
  BeforeLambdaBody: false
  BeforeWhile: true
  IndentBraces: false
Cpp11BracedListStyle: false

#
# spacing options
#
SpaceAfterCStyleCast: true
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon : false
SpaceBeforeCpp11BracedList: true
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: Custom
SpaceBeforeParensOptions:
  AfterControlStatements: true
  AfterForeachMacros: true
  AfterFunctionDeclarationName: false
  AfterOverloadedOperator: false
  BeforeNonEmptyParentheses: false

#
# Indentation options
#
AlignOperands: DontAlign
#AlignOperands: Align
#AlignArrayOfStructures: Right
AlignAfterOpenBracket: AlwaysBreak
#AlignAfterOpenBracket: true
AllowAllArgumentsOnNextLine: false
AllowAllParametersOfDeclarationOnNextLine: false
BinPackArguments: false
BinPackParameters: false
ContinuationIndentWidth: 4
IndentCaseBlocks: false
IndentCaseLabels: true
LambdaBodyIndentation: Signature
NamespaceIndentation: All

#
# Line breaking/packing/wrapping options
#
AllowShortEnumsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: Never
AllowShortLambdasOnASingleLine : Empty
AllowShortLoopsOnASingleLine: false
AlwaysBreakTemplateDeclarations: No
PackConstructorInitializers: Never
BreakBeforeBinaryOperators: true
BreakBeforeConceptDeclarations: Never
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeColon
ColumnLimit: 0
SeparateDefinitionBlocks: Leave

#
# comment options
#
AlignTrailingComments:
  Kind: Leave
#  Kind: Always
#  OverEmptyLines: 3
ReflowComments: false

AlignEscapedNewlines: Left
RemoveSemicolon: true
MaxEmptyLinesToKeep: 3
KeepEmptyLinesAtEOF: false

#PenaltyExcessCharacter: 10
#PenaltyBreakOpenParenthesis: 10000
#PenaltyIndentedWhitespace: 5000
PenaltyBreakAssignment: 20000

IncludeBlocks: Regroup
SortIncludes: CaseSensitive
IncludeCategories:
  - Regex:           '^<Q.*$'
    Priority:        3
    SortPriority:    3
    CaseSensitive:   false
  - Regex:           '^<.*$'
    Priority:        2
    SortPriority:    2
    CaseSensitive:   false
  - Regex: '".*"'
    Priority: 4
    SortPriority: 4
c++ clang clang-format
1个回答
0
投票

事实证明 BinPackArguments:false 与 ColumnLimit: 0 冲突。这让我觉得这是一个错误。

BinPackArguments:false 表示将所有函数调用参数放在一行或全部放在单独的行上。

ColumnLimit:0 表示不根据列位置移动。

这里没有函数调用参数。线条不应重新对齐。

此外,即使 BinPackArguments:false 会影响行,单独保留行也可以满足这两个设置。

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