clang-format:对齐开括号后的参数列表

3

这篇文章提出了一个类似的问题,关于当参数过多时如何修改格式。

我很喜欢rust-fmt的样式。有没有办法用clang-format实现这个样式呢?

例如:使用AlignAfterOpenBrackets: AlwaysBreak

return_t foo(
    some_t param_1, some_t param_2, some_t param_3, 
    some_t param_4) {
    // function body
}

例如 2:期望的格式

return_t foo(
    some_t param_1, 
    some_t param_2, 
    some_t param_3, 
    some_t param_4
) {
    // function body
}
1个回答

6

clang-format AlignAfterOpenBracket 新增了一个选项 - BlockIndent (于17/1/22发布) 用于此目的。

参见https://reviews.llvm.org/rG966f24e5a62a

[clang-format] Add a BlockIndent option to AlignAfterOpenBracket

This style is similar to AlwaysBreak, but places closing brackets on new lines.

For example, if you have a multiline parameter list, clang-format currently only supports breaking per-parameter, but places the closing bracket on the line of the last parameter.

Function(
  param1,
  param2,
  param3);

A style supported by other code styling tools (e.g. rustfmt) is to allow the closing brackets to be placed on their own line, aiding the user in being able to quickly infer the bounds of the block of code.

Function(
  param1,
  param2,
  param3
);
这个特性预计会在 clang-format-14 版本中发布。
与此同时,可以尝试从LLVM 每夜构建中获取。

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