【发布时间】:2016-02-12 21:54:48
【问题描述】:
我正在寻找 clang-format 设置以防止该工具删除换行符。
例如,我将 ColumnLimit 设置为 120,这就是我重新格式化一些示例代码时发生的情况。
之前:
#include <vector>
#include <string>
std::vector<std::string> get_vec()
{
return std::vector<std::string> {
"this is a test",
"some of the lines are longer",
"than other, but I would like",
"to keep them on separate lines"
};
}
int main()
{
auto vec = get_vec();
}
之后:
#include <vector>
#include <string>
std::vector<std::string> get_vec()
{
return std::vector<std::string>{"this is a test", "some of the lines are longer", "than other, but I would like",
"to keep them on separate lines"};
}
int main()
{
auto vec = get_vec();
}
我想要的是该工具会中断超过 120 个字符的行,但不会仅仅因为它们少于 120 个字符而决定合并行。
有这样的选择吗?文档中的任何内容都没有让我印象深刻。
【问题讨论】:
-
对于您的特定示例,设置
AllowShortFunctionsOnASingleLine: None将起作用。 -
好吧,这会阻止它展开
main,但我更关心一般的向量初始化。如果向量在另一个(更长的)函数中以这种方式初始化,它仍然会被解包。 -
我想知道调整各种“惩罚”选项是否会有所帮助,但它们似乎都是与断线相关的惩罚,而不是“破坏”它们。
-
你能举一个上面的设置不能按预期工作的例子吗?该工具不再缩短您的功能,并且应该尊重列限制。向量初始化也应该起作用。
-
我更新了问题中的示例。使用
AllowShortFunctionsOnASingleLine: None,main不再展开,但正如您所见,向量初始化仍然混乱。
标签: c++ clang code-formatting clang-format