我在文档中没有找到任何内容,即使BreakBeforeBraces: Allman
格式化已经将我的单行函数拆分为多行:
void foo()
{
bar();
}
我想要与此类似的格式:
void foo()
{
bar();
}
我希望你能帮助我做到代码的组织和统一,因为这就是每个多行函数的样子。请问你能帮忙吗?
我在文档中没有找到任何内容,即使BreakBeforeBraces: Allman
格式化已经将我的单行函数拆分为多行:
void foo()
{
bar();
}
我想要与此类似的格式:
void foo()
{
bar();
}
要将短函数体放在单独的一行上,请将以下内容添加到.clang-format
文件中:
AllowShortFunctionsOnASingleLine: Empty
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
格式化失败: /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使用.clang-format文件无法实现这种特定的行为。对于所有希望在这里找到方法的人,很抱歉,我希望至少能为你们节省一些时间。
最接近的方法:
BreakBeforeBraces: Allman
ColumnLimit: '0'
这将保持您的格式化函数正确,并在它们至少跨越2行时正确地格式化它们。
ColumnLimit: '0'
,它将保留您已经格式化的函数,并在此函数中任何位置设置换行符时正确地格式化它们。这是一个巨大的进步,但我仍希望它可以完全自动化... - notADev