Clang格式“AlwaysBreakAfterReturnType:None”不起作用

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

我想使用clang-format,但它始终以returnType之后的新行开头。我阅读了文档并尝试过

“AlwaysBreakAfterReturnType:None”

但是接缝没有效果。我在QT创建者的ubuntu 17.10中使用了clang-format 6.0。

是:

int
    main() {
    ...
}

预期:

int main() {
    ...
}

版本:clang-format 6.0,clang-format配置文件:

BasedOnStyle: Mozilla
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments : true
AlignEscapedNewlines: Right
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: InlineOnly
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false
BraceWrapping:
    AfterClass: true
    AfterControlStatement: false
    AfterEnum: false
    AfterFunction: false
    AfterNamespace: false
    AfterObjCDeclaration: false
    AfterStruct: false
    AfterUnion: false
    AfterExternBlock: false
    BeforeCatch: true
    BeforeElse: true
    SplitEmptyFunction: false
    SplitEmptyRecord: false
    SplitEmptyNamespace: false
BreakBeforeBinaryOperators: All
BreakBeforeBraces: Custom
BreakBeforeInheritanceComma: true
BreakBeforeTernaryOperators: true
BreakConstructorInitializers: BeforeComma
BreakStringLiterals : false
ColumnLimit: 120
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: true
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: false
DerivePointerAlignment: true
DisableFormat: false
ExperimentalAutoDetectBinPacking: true
FixNamespaceComments: true
IndentCaseLabels: false
IndentPPDirectives: AfterHash
IndentWidth: 4
IndentWrappedFunctionNames: true
KeepEmptyLinesAtTheStartOfBlocks: false
Language: Cpp
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
ReflowComments: true
SortIncludes: true
SortUsingDeclarations: true
SpaceAfterCStyleCast: true
SpaceAfterTemplateKeyword : false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 3
SpacesInAngles: false
SpacesInCStyleCastParentheses: true
SpacesInContainerLiterals: true
SpacesInParentheses: true
SpacesInSquareBrackets: false
Standard: Cpp11
TabWidth: 4
UseTab: Never

有任何想法吗?

clang-format
3个回答
0
投票

我无法在您提供的最小示例中重现您的问题作为评论:

$ cat .clang-format 
 BasedOnStyle: Mozilla
 AlwaysBreakAfterReturnType: None
$ cat main.c 
int main() {
        return 0;
}
$ clang-format main.c 
int
main()
{
  return 0;
}
$ clang-format --version
clang-format version 3.8.0-2ubuntu4 (tags/RELEASE_380/final)

实际上,你还设置了IndentWrappedFunctionNames,其行为正是你获得的行为。如果您不想要此行为,请不要设置IndentWrappedFunctionNames


0
投票

您应该仔细阅读文档:

RTBS_None(在配置中:无)自动中断返回类型。 PenaltyReturnTypeOnItsOwnLine被考虑在内。

所以“无”并不意味着它在返回类型之后永远不会破坏,它将考虑其他设置,如BinPackParametersBraceWrapping等,以及他们的处罚。

要在返回类型不太可能或“几乎”关闭后进行破坏,您可以将PenaltyReturnTypeOnItsOwnLine设置为非常高的值,例如:

PenaltyReturnTypeOnItsOwnLine: 1000000

请注意,我并不熟悉惩罚数字的确切含义,它们之间的关系以及如何计算最终的换行数。你必须在别处找到这些信息(clang-format的源代码?)。


0
投票

只是尝试将不推荐使用的属性“AlwaysBreakAfterDefinitionReturnType”分配给“无”。

这对我很有用。

AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
© www.soinside.com 2019 - 2024. All rights reserved.