c++ - 通过 clang-format 对几乎精确的结构进行不同的格式化
问题描述
我想使用 clang-format 来自动格式化我的源代码,但我无法理解它在类似结构下的行为。请参阅以下 C++ 代码:
static someStructLongName structInstanceLong{
Method1LongLong,
structInstanceMethod1,
structInstanceMethod1,
structInstanceMethod3,
structInstanceMethod4,
structInstanceMethod5,
structInstanceMethod6,
structInstanceMethod7,
structInstanceMethod8
};
static someStructLongName structInstanceLong{
Method1,
structInstanceMethod1,
structInstanceMethod1,
structInstanceMethod3,
structInstanceMethod4,
structInstanceMethod5,
structInstanceMethod6,
structInstanceMethod7,
structInstanceMethod8
};
当我使用我的自定义配置文件(附在帖子底部)使用 clang-format 对其进行格式化时,我得到以下输出:
static someStructLongName structInstanceLong{Method1LongLing, structInstanceMethod1, structInstanceMethod1,
structInstanceMethod3, structInstanceMethod4, structInstanceMethod5,
structInstanceMethod6, structInstanceMethod7, structInstanceMethod8};
static someStructLongName structInstanceLong{Method1,
structInstanceMethod1,
structInstanceMethod1,
structInstanceMethod3,
structInstanceMethod4,
structInstanceMethod5,
structInstanceMethod6,
structInstanceMethod7,
structInstanceMethod8};
第二个选项是首选,但我不明白哪个规则是造成这种行为的原因,所以要改变什么来强制所有结构都得到这个结果。
有趣的事情 - 当我使用这个在线工具时: https ://zed0.co.uk/clang-format-configurator/ 加载我的配置和上面的代码一切看起来都像预期的那样。
在 clang-format 自定义配置下面:
---
AccessModifierOffset: '-4'
AlignEscapedNewlinesLeft: 'true'
AllowAllParametersOfDeclarationOnNextLine: 'true'
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: 'false'
AllowShortLoopsOnASingleLine: 'false'
AlwaysBreakBeforeMultilineStrings: 'false'
AlwaysBreakTemplateDeclarations: 'true'
BinPackParameters: 'false'
BreakBeforeBinaryOperators: 'NonAssignment'
BraceWrapping:
AfterClass: false
AfterControlStatement: false
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterStruct: false
AfterUnion: false
BeforeCatch: true
BeforeElse: true
IndentBraces: false
BreakBeforeBraces: Custom
BreakBeforeTernaryOperators: 'false'
BreakConstructorInitializersBeforeComma: 'true'
ColumnLimit: '120'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'false'
ConstructorInitializerIndentWidth: '4'
ContinuationIndentWidth: '4'
Cpp11BracedListStyle: 'true'
DerivePointerAlignment: 'false'
DisableFormat: 'false'
ExperimentalAutoDetectBinPacking: 'false'
IndentCaseLabels: 'false'
IndentWidth: '4'
IndentWrappedFunctionNames: 'false'
KeepEmptyLinesAtTheStartOfBlocks: 'false'
Language: Cpp
MaxEmptyLinesToKeep: '1'
NamespaceIndentation: All
PointerAlignment: Left
SpaceBeforeAssignmentOperators: 'true'
SpaceBeforeParens: ControlStatements
SpaceInEmptyParentheses: 'false'
SpacesInAngles: 'false'
SpacesInCStyleCastParentheses: 'false'
SpacesInParentheses: 'false'
Standard: Cpp11
TabWidth: '4'
UseTab: Never
...
解决方案
我使用了一个名为 clang-format configurator 的工具,这将像您的第二个选项一样格式化您的代码
BasedOnStyle:铬
AlignEscapedNewlinesLeft: '真'
AlignTrailingComments:“真”
AllowAllParametersOfDeclarationOnNextLine: '假'
AllowShortBlocksOnASingleLine: 'true'
AllowShortFunctionsOnASingleLine:全部
BreakBeforeBinaryOperators:'真'
ConstructorInitializerAllOnOneLineOrOnePerLine: 'true'
缩进宽度:'1'
IndentWrappedFunctionNames: 'true'
KeepEmptyLinesAtTheStartOfBlocks:'假'
SpaceBeforeAssignmentOperators:“假”
...
推荐阅读
- html - 如何在它们之间添加并排图像?
- apache - 在子域上安装 SSL 错误 - vhost 会与 Apache 中的其他 HTTPS VirtualHosts 冲突
- nunit - 使用 NUnit,(如何)我可以测试设置 Thread.CurrentThread.Name 的代码吗?
- java - 多重结果?MySQL
- amazon-web-services - S3 使用 x-amz-website-redirect-location 标头重定向到 codebuild 徽章
- python - 即使使用 insertNode (BST) 更新节点后,结果也是 NoneType
- javascript - 用于运行查询可视化的时间序列水平条形图
- java - 在 AbstractJavaSamplerClient 中使用用户参数
- google-sheets - 数据透视表每天添加信息
- php - 事务处理后如何在 Laravel 中执行回调函数