使用.clang-format,有没有一种方法可以在单行函数之前进行换行?

9

我在文档中没有找到任何内容,即使BreakBeforeBraces: Allman格式化已经将我的单行函数拆分为多行:

void foo() { bar(); }

我想要与此类似的格式:

void foo()
{
    bar();
}

我希望你能帮助我做到代码的组织和统一,因为这就是每个多行函数的样子。请问你能帮忙吗?

更新:如果您设置ColumnLimit: '0',它将保留您已经格式化的函数,并在此函数中任何位置设置换行符时正确地格式化它们。这是一个巨大的进步,但我仍希望它可以完全自动化... - notADev
3个回答

15

要将短函数体放在单独的一行上,请将以下内容添加到.clang-format文件中:

AllowShortFunctionsOnASingleLine: Empty

4
  • 是的,您可以做到这一点。 最简单的方法是设置
BreakBeforeBraces: Stroustrup
  • 其次,您可以通过在SplitEmptyFunction中设置true来完成此操作。例如:
"BraceWrapping":
        "AfterClass":             false
        "AfterControlStatement":  false
        "AfterEnum":              false
        "AfterFunction":          false
        "AfterNamespace":         false
        "AfterObjCDeclaration":   false
        "AfterStruct":            false
        "AfterUnion":             false  
        "BeforeCatch":            false
        "BeforeElse":             false
        "IndentBraces":           false
        "SplitEmptyFunction":     true <-- set this as true
        "SplitEmptyRecord":       true
        "SplitEmptyNamespace":    true


如果它是真的,输出将会是:
void foo()
{
    bar();
}

但如果它是假的,输出将会是什么。
void foo(){
    bar();
}

编辑 -: 将您的clang文件编辑成这样

BasedOnStyle:  WebKit
TabWidth:        4
IndentWidth:     4
UseTab:          Always
ColumnLimit: 100

DisableFormat:   false
Standard: Cpp11

AccessModifierOffset: -4
AlignAfterOpenBracket: true
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: false
AlignOperands:   true
AlignTrailingComments: false
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: false
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
BinPackArguments: false
BinPackParameters: false

BraceWrapping: {
    AfterClass: 'true'
    AfterControlStatement: 'true'
    AfterEnum : 'true'
    AfterFunction : 'true'
    AfterNamespace : 'true'
    AfterStruct : 'true'
    AfterUnion : 'true'
    BeforeCatch : 'true'
    BeforeElse : 'true'
    IndentBraces : 'false'
    AfterExternBlock : 'true'
    SplitEmptyFunction : 'false'
    SplitEmptyRecord : 'false'
    SplitEmptyNamespace : 'true'
}

BreakAfterJavaFieldAnnotations: true
BreakBeforeInheritanceComma: false
BreakBeforeBinaryOperators: None
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: true
BreakStringLiterals: true

CommentPragmas:  '^ IWYU pragma:'
CompactNamespaces: false
ConstructorInitializerAllOnOneLineOrOnePerLine: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
SpaceBeforeCpp11BracedList: false
DerivePointerAlignment: false
ExperimentalAutoDetectBinPacking: false
ForEachMacros:   [ foreach, Q_FOREACH, BOOST_FOREACH ]
IndentCaseLabels: false
FixNamespaceComments: true
IndentWrappedFunctionNames: false
KeepEmptyLinesAtTheStartOfBlocks: true
MacroBlockBegin: ''
MacroBlockEnd:   ''
JavaScriptQuotes: Double
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBlockIndentWidth: 4
ObjCSpaceAfterProperty: true
ObjCSpaceBeforeProtocolList: true
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakString: 1000

PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PointerAlignment: Left
SpaceAfterCStyleCast: false
SpaceBeforeAssignmentOperators: true
SpaceBeforeParens: Never
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles:  false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceAfterTemplateKeyword: true
SpaceBeforeInheritanceColon: true

SortUsingDeclarations: true
SortIncludes: true

ReflowComments: false

IncludeBlocks: Preserve
IndentPPDirectives: AfterHash

Source


我在复制你的代码时出现了错误:格式化失败: /fake_path/.vscode-server/extensions/ms-vscode.cpptools-0.26.2/bin/../LLVM/bin/clang-format -style=file -fallback-style=LLVM -assume-filename=/fake_path/test.cpp YAML:4:16: 错误:在标记化时无法识别字符。 "BraceWrapping":{ ^ Error reading /fake_path/.clang-format: Invalid argument编辑:已经修复(在您的帖子中)。 - notADev
谢谢你,但它仍然没有实现我想要的效果,它让这个单行函数保持为一行,就像我的第一个解决方案一样。当我插入一个换行符时,它确实会正确执行,但是它仍然留在同一行上,不进行任何更改。 - notADev
不,文档中指出在最终格式化中它仍将保持在一行中。 - notADev
这个回答是否解决了你的问题:https://dev59.com/nl0b5IYBdhLWcg3wM-0c - Kalana
谢谢,我已经找到了这个,但它没有为我提供足够的答案。感谢您的努力。有一点我必须接受,就是根本没有办法... - notADev
显示剩余2条评论

-1
回答问题:

不行

使用.clang-format文件无法实现这种特定的行为。对于所有希望在这里找到方法的人,很抱歉,我希望至少能为你们节省一些时间。

最接近的方法:

BreakBeforeBraces: Allman
ColumnLimit: '0'

这将保持您的格式化函数正确,并在它们至少跨越2行时正确地格式化它们。


5
这不正确。可以使用"AllowShortFunctionsOnASingleLine"样式选项来实现所需的格式设置。将其设置为除"All"以外的任何值即可。请参阅http://clang.llvm.org/docs/ClangFormatStyleOptions.html。 - Eric Backus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接