问题描述
如果我的函数的参数对于列宽而言太长,则会按预期将它们全部放在新行上。但是,这似乎有时会破坏我的指针对齐:
格式化之前:
struct ConnectionResult *connect_to_server(struct Folders *start_folder,struct Config *config,struct CTypet *conn_type)
格式化后:
struct ConnectionResult *connect_to_server(struct Folders *start_folder
struct Config * config,struct CTypet * conn_type);
似乎它想更喜欢对齐实际的变量名,而忽略指针对齐规则。这导致我的函数有时与其他函数的样式不一致。我的.clang_format
是
AlignAfterOpenBracket: Align
AlignConsecutiveMacros: 'true'
AlignConsecutiveAssignments: 'true'
AlignConsecutiveDeclarations: 'true'
AlignTrailingComments: 'true'
BreakBeforeBraces: Allman
IndentCaseLabels: 'true'
IndentWidth: '4'
Language: Cpp
Pointeralignment: Right
SortIncludes: 'true'
SpaceBeforeParens: ControlStatements
SpaceBeforeRangeBasedForLoopColon: 'true'
SpaceInEmptyParentheses: 'false'
BinPackParameters: 'false'
BinPackArguments: 'false'
UseTab: Never
ColumnLimit: 80
请注意,这种配置有时还会以非常难看的方式长时间中断循环:
之前:
for(size_t current_conn = initial_conn; current_connection <= max_connections; current_conn += connection_offset) { ...}
格式化后
for (size_t current_conn = initial_conn; current_conn <= max_connections;
current_conn += connection_offset)
{
...
}
实际上,我想在自己的行上查看for循环的每个参数。
对于使用Clang格式的C语言,我还比较陌生,我不确定如何解决此问题。是否有一种让我的clang格式在两种情况下都能正常工作的好方法?
解决方法
简短的回答:不,clang-format
不会做任何一个。
是AlignConsecutiveDeclarations
使参数声明对齐,而PointerAlignment
控制声明中*
和&
周围的间距。但是看来AlignConsecutiveDeclarations
会忽略PointerAlignment
,而PointerAlignment: Right
实际上只是意味着“在*
或&
之前添加一个空格”。
不幸的是,似乎没有一个documented。
对于for循环,您似乎需要一个名为AllowBinPackForLoop
的假想新样式选项,该选项默认为true(以实现向后兼容性),但为false则允许for循环为就像您在询问是否需要多条线一样进行拆分。但是据我所知,没有那样的东西。