如何设置 clang 格式以将返回类型和原型保持在同一行

问题描述 投票:0回答:1

我正在发现 clang-format。我将 ColumnLimit 设置为 120。一般来说,这效果很好,意味着在不同的行上设置函数参数。

void myLongFunctionNameMoreThan120Col(int foo,
                                      float barbar,
                                      float* aLongPointerName,
                                      float* anotherVeryLongPointerName);

但是对于长返回类型,函数名位于返回类型之外的另一行:

LONGTYPE
myLongFunctionNameMoreThan120Col(int foo, float barbar, float* aLongPointerName, float* anotherVeryLongPointerName);

如何告诉 clang-format 始终像第一种情况一样运行?

我尝试使用更长的变量名来强制采用格式。解决方案并不令人满意,我不应该修改我的变量名称。

LONGTYPE myLongFunctionNameMoreThan120Col(int foo,
                                          float barbarTotoLong,
                                          float* aLongPointerName,
                                          float* anotherVeryLongPointerName);
c format clang-format
1个回答
0
投票

有关样式选项的文档指定了您需要了解的有关任何特定选项的所有信息。

您正在寻找的选项是

AlwaysBreakAfterReturnType: None
。值
None
的含义描述为:

返回类型后自动断行。 PenaltyReturnTypeOnItsOwnLine 已被考虑在内。

因此,您还可以使用

PenaltyReturnTypeOnItsOwnLine
控制所需的行为。

我假设 clang-format 将尝试格式化代码,以使格式化代码的总惩罚尽可能小。因此,您需要将

PenaltyReturnTypeOnItsOwnLine
设置为相对于其他惩罚选项值较高的值。

© www.soinside.com 2019 - 2024. All rights reserved.