Format 有没有一种方法可以在一行函数之前使用.clang格式中断?

Format 有没有一种方法可以在一行函数之前使用.clang格式中断?,format,clang,clang-format,Format,Clang,Clang Format,我在中找不到任何东西,即使是breakbeforebrases:Allman格式的单行函数,我已经将其拆分为 void foo(){bar();} 我想要类似的东西 void foo() { bar(); } 我想用它来组织代码和统一性,因为这就是每个多行函数的外观 你能帮帮我吗? 是的,你能做到最简单的方法是设置 其次,您可以在SplitEmptyFunction中设置true。例如 但如果它是false,则输出将是 void foo() { bar(); } voi

我在中找不到任何东西,即使是
breakbeforebrases:Allman
格式的单行函数,我已经将其拆分为

void foo(){bar();}

我想要类似的东西

void foo()
{
    bar();
}
我想用它来组织代码和统一性,因为这就是每个多行函数的外观

你能帮帮我吗?

  • 是的,你能做到最简单的方法是设置
  • 其次,您可以在
    SplitEmptyFunction
    中设置
    true
    。例如
但如果它是false,则输出将是

void foo()
{
    bar();
}
void foo(){
    bar();
}

编辑-:编辑叮当声文件,使其看起来像这样

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

要回答这个问题:

使用.clang格式文件无法实现此特定行为。对不起,所有希望在这里找到一种方法的人,我希望我至少能为你们节省一些时间

最近的:

BreakBeforeBraces: Allman
ColumnLimit: '0'

这将使格式化的函数保持正确,如果它们至少在两行上,则可以正确格式化。

要在单独的行上有一个短函数体,请将其添加到
。clang format
文件:

AllowShortFunctionsOnASingleLine: Empty

更新:如果您设置了
ColumnLimit:'0'
,它将让您已经格式化的函数独立运行,并且如果您在此函数中的任何位置设置了换行符,它将正确格式化这些函数。这是一个巨大的进步,但是我希望它100%自动执行…我在复制代码时遇到一个错误:
格式化失败:/fake_path/.vscode server/extensions/ms vscode.cpptools-0.26.2/bin/./LLVM/bin/clangformat-style=file-fallback style=LLVM-假设filename=/fake_path/test.cpp YAML:4:16:错误:标记化时无法识别字符。“BraceWrapping”:{^Error reading/fake_path/.clang格式:无效参数
Edit:已修复(在您的帖子中)谢谢,但它仍然没有达到我想要的效果,它将单行函数保留在一行上,就像我的第一个解决方案一样。当我插入换行符时,它正确地执行了操作,但在同一行上保留了它,因为这只是我告诉的第一步I’我仍然在线这是否回答了您的问题这是不正确的。可以让您实现所需格式的样式选项是
allowShortFunction sonaSingleLine
。将其设置为除
All
之外的任何选项。请参阅。
BreakBeforeBraces: Allman
ColumnLimit: '0'
AllowShortFunctionsOnASingleLine: Empty